-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectDependencyUtil.cs
More file actions
206 lines (174 loc) · 6.63 KB
/
Copy pathObjectDependencyUtil.cs
File metadata and controls
206 lines (174 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.Linq;
using RelationsInspector.Extensions;
namespace RelationsInspector.Backend.AssetDependency
{
using ObjMap = Dictionary<Object, HashSet<Object>>;
using ObjNodeGraph = Dictionary<ObjectNode, HashSet<ObjectNode>>;
public static class ObjectDependencyUtil
{
public static ObjNodeGraph GetReferenceGraph( string sceneFilePath, HashSet<Object> targets )
{
// get the scene's objects
var sceneObjects = UnityEditorInternal
.InternalEditorUtility
.LoadSerializedFileAndForget( sceneFilePath )
.ToHashSet();
// get the root gameObjects
var rootGOs = sceneObjects
.OfType<GameObject>()
.Where( go => go.transform.parent == null );
// build the Object graph
var objGraph = new ObjMap();
var targetArray = targets.ToArray();
foreach ( var rootGO in rootGOs )
{
var rootGOgraph = ObjectGraphUtil.GetDependencyGraph( rootGO, targetArray );
objGraph = ObjectGraphUtil.MergeGraphs( objGraph, rootGOgraph );
}
// convert it to a SceneObjectNode graph, so we can destroy the objects
var nodeGraph = ObjectGraphToObjectNodeGraph( objGraph, obj => GetSceneObjectNode( obj, targets, sceneObjects, sceneFilePath ) );
// destroy the scene Objects
var sceneObjArray = sceneObjects.ToArray();
for ( int i = 0; i < sceneObjArray.Length; i++ )
Object.DestroyImmediate( sceneObjArray[ i ] );
System.GC.Collect();
return nodeGraph;
}
public static ObjNodeGraph GetActiveSceneReferenceGraph( HashSet<Object> targets )
{
var rootGameObjects = ActiveSceneRootGameObjects();
// build the Object graph
var objGraph = new ObjMap();
var targetArray = targets.ToArray();
foreach ( var rootGO in rootGameObjects )
{
var rootGOgraph = ObjectGraphUtil.GetDependencyGraph( rootGO, targetArray );
objGraph = ObjectGraphUtil.MergeGraphs( objGraph, rootGOgraph );
}
// convert it to a SceneObjectNode graph, so we can destroy the objects
return ObjectGraphToObjectNodeGraph( objGraph, obj => GetActiveSceneObjectNode( obj, targets ) );
}
public static IEnumerable<GameObject> ActiveSceneRootGameObjects()
{
#if UNITY_5_3
return UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
#else
var prop = new HierarchyProperty( HierarchyType.GameObjects );
var expanded = new int[ 0 ];
while ( prop.Next( expanded ) )
{
yield return prop.pptrValue as GameObject;
}
#endif
}
public static string GetActiveSceneName( bool excludePath = true )
{
#if UNITY_5_3
string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
#else
string sceneName = EditorApplication.currentScene;
#endif
return excludePath ? sceneName.Split( '/' ).Last() : sceneName;
}
// turn object graph into VisualNode graph (mapping obj -> name)
static ObjNodeGraph ObjectGraphToObjectNodeGraph( ObjMap objGraph, System.Func<Object, ObjectNode> getNode )
{
var referencedObjects = objGraph.Values.SelectMany( o => o ).ToHashSet();
// get all graph objects
var allObjs = objGraph.Keys.Concat( referencedObjects ).ToHashSet();
// map them to VisualNodes
var objToNode = allObjs.ToDictionary( obj => obj, obj => getNode( obj ) );
// convert from Object to SceneObjectNode and flip the edge direction
return referencedObjects.ToDictionary(
x => objToNode[ x ],
x => objGraph
.Where( pair => pair.Value.Contains( x ) )
.Select( pair => objToNode[ pair.Key ] )
.ToHashSet()
);
}
static ObjectNode GetActiveSceneObjectNode( Object obj, HashSet<Object> targets )
{
string label = obj.name;
string tooltip = "";
bool isSceneObj = false;
Object[] objects;
string sceneName = GetActiveSceneName();
var asCycleRep = obj as CycleRep;
if ( asCycleRep != null )
{
label = asCycleRep.name;
if ( targets.Intersect( asCycleRep.members ).Any() )
label += "\nScene " + sceneName;
tooltip = !string.IsNullOrEmpty( label ) ? label : string.Join( "\n", asCycleRep.members.Select( m => m.name ).ToArray() );
// we consider rep as a scene Obj if all its members are scene objs
isSceneObj = asCycleRep.members.All( m => IsSceneObject( m ) );
objects = asCycleRep.gameObject != null ? new[] { asCycleRep.gameObject } : asCycleRep.members.ToArray();
}
else
{
// add scene name. if label has content, put the scene name in a new line
if ( targets.Contains( obj ) )
label += ( ( label == "" ) ? "" : "\n" ) + "Scene " + sceneName;
isSceneObj = IsSceneObject( obj );
objects = new[] { obj };
}
return new ObjectNode( label, tooltip, objects, isSceneObj );
}
// return true if obj is part of a scene
static bool IsSceneObject( Object obj )
{
// scene objects have no asset path
return string.IsNullOrEmpty( AssetDatabase.GetAssetPath( obj ) );
}
static ObjectNode GetSceneObjectNode( Object obj, HashSet<Object> targets, HashSet<Object> sceneObjects, string scenePath )
{
string label = obj.name;
string tooltip = "";
bool isSceneObj = false;
Object[] objects;
string sceneName = System.IO.Path.GetFileNameWithoutExtension( scenePath );
var asCycleRep = obj as CycleRep;
if ( asCycleRep != null )
{
label = asCycleRep.name;
if ( targets.Intersect( asCycleRep.members ).Any() )
label += "\nScene " + sceneName;
tooltip = !string.IsNullOrEmpty( label ) ? label : string.Join( "\n", asCycleRep.members.Select( m => m.name ).ToArray() );
// we consider rep as a scene Obj if all its members are scene objs
isSceneObj = !asCycleRep.members.Except( sceneObjects ).Any();
objects = asCycleRep.gameObject != null ? new[] { asCycleRep.gameObject } : asCycleRep.members.ToArray();
}
else
{
// add scene name. if label has content, put the scene name in a new line
if ( targets.Contains( obj ) )
label += ((label == "") ? "" : "\n") + "Scene " + sceneName;
isSceneObj = sceneObjects.Contains( obj );
objects = new[] { obj };
}
if ( isSceneObj )
objects = new Object[] { }; // todo: get scene object
return new ObjectNode( label, tooltip, objects, isSceneObj );
}
// returns true if the object is a prefab
static bool IsPrefab( Object obj )
{
return PrefabUtility.GetPrefabParent( obj ) == null && PrefabUtility.GetPrefabObject( obj ) != null;
}
// merge two graphs
public static void AddGraph<T>( Dictionary<T, HashSet<T>> graph, Dictionary<T, HashSet<T>> addedGraph ) where T : class
{
foreach ( var pair in addedGraph )
{
if ( !graph.ContainsKey( pair.Key ) )
graph[ pair.Key ] = pair.Value;
else
graph[ pair.Key ].UnionWith( pair.Value );
}
}
}
}