Skip to content

Commit fbabb71

Browse files
authored
Merge pull request #4 from innogames/improve-performance-of-ispackedtoapp-implementation
Improve performance of ispackedtoapp implementation
2 parents 185a729 + 2efb2de commit fbabb71

File tree

14 files changed

+80
-94
lines changed

14 files changed

+80
-94
lines changed

AssetRelationsViewer/Editor/AssetRelationsViewerWindow.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private class MergedNode
5252
private Dictionary<string, VisualizationNodeData> _cachedVisualizationNodeDatas = new Dictionary<string, VisualizationNodeData>();
5353
private Dictionary<string, AssetCacheData> _cachedNodes = new Dictionary<string, AssetCacheData>();
5454
private Dictionary<string, int> _cachedSizes = new Dictionary<string, int>();
55+
private Dictionary<string, bool> _cachedPackedInfo = new Dictionary<string, bool>();
5556

5657
private Stack<UndoStep> _undoSteps = new Stack<UndoStep>();
5758

@@ -788,7 +789,7 @@ private VisualizationNodeData AddNodeCacheForNode(string id, string type)
788789

789790
data.Name = typeHandler.GetName(id);
790791
data.IsEditorAsset = nodeHandler.IsNodeEditorOnly(id, type);
791-
data.IsPackedToApp = NodeDependencyLookupUtility.IsNodePackedToApp(id, type, _nodeDependencyLookupContext, new HashSet<string>());
792+
data.IsPackedToApp = NodeDependencyLookupUtility.IsNodePackedToApp(id, type, _nodeDependencyLookupContext, _cachedPackedInfo);
792793

793794
_cachedVisualizationNodeDatas.Add(key, data);
794795
}
@@ -804,6 +805,7 @@ private void InvalidateTreeVisualization()
804805
private void Refresh()
805806
{
806807
_cachedSizes.Clear();
808+
_cachedPackedInfo.Clear();
807809
_cachedVisualizationNodeDatas.Clear();
808810
_cachedNodes.Clear();
809811
InvalidateNodeStructure();

AssetRelationsViewer/Editor/AssetTypeHandler.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ public void InitContext(NodeDependencyLookupContext nodeDependencyLookupContext,
102102
{
103103
_viewerWindow = window;
104104
_filteredNodes = CreateFilter(_filterString);
105-
106-
if (_explorerSyncModePref.GetValue())
107-
{
108-
RegisterOnSelectionChanged();
109-
}
110-
else
111-
{
112-
UnregisterOnSelectionChanged();
113-
}
105+
Selection.selectionChanged += HandleSyncToExplorer;
114106
}
115107

116108
public bool HandlesCurrentNode()
117109
{
118110
return _selectedAsset != null;
119111
}
112+
113+
private void HandleSyncToExplorer()
114+
{
115+
if (_explorerSyncModePref.GetValue())
116+
{
117+
_viewerWindow.OnAssetSelectionChanged();
118+
}
119+
}
120120

121121
private void DisplayFilterOptions()
122122
{
@@ -143,31 +143,10 @@ private void DisplayFilterOptions()
143143
_viewerWindow.InvalidateNodeStructure();
144144
}
145145

146-
AssetRelationsViewerWindow.TogglePref(_explorerSyncModePref, "Sync to explorer:", b =>
147-
{
148-
if (b)
149-
{
150-
RegisterOnSelectionChanged();
151-
}
152-
else
153-
{
154-
UnregisterOnSelectionChanged();
155-
}
156-
});
157-
146+
AssetRelationsViewerWindow.TogglePref(_explorerSyncModePref, "Sync to explorer:");
158147
EditorGUILayout.EndVertical();
159148
}
160149

161-
private void RegisterOnSelectionChanged()
162-
{
163-
Selection.selectionChanged += _viewerWindow.OnAssetSelectionChanged;
164-
}
165-
166-
private void UnregisterOnSelectionChanged()
167-
{
168-
Selection.selectionChanged -= _viewerWindow.OnAssetSelectionChanged;
169-
}
170-
171150
private HashSet<string> CreateFilter(string filter)
172151
{
173152
if (string.IsNullOrEmpty(filter))

NodeDependencyLookup/Editor/Caches/AssetDependencyCache/AssetDependencyCache.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct AssetResolverData
1818
*/
1919
public class AssetDependencyCache : IDependencyCache
2020
{
21-
public const string Version = "1.10";
21+
public const string Version = "1.30";
2222
public const string FileName = "AssetDependencyCacheData_" + Version + ".cache";
2323

2424
private FileToAssetNode[] _fileToAssetNodes = new FileToAssetNode[0];
@@ -42,6 +42,7 @@ public void Initialize(CreatedDependencyCache createdDependencyCache)
4242

4343
public void Load(string directory)
4444
{
45+
EditorUtility.DisplayProgressBar("AssetDependencyCache", "Loading cache", 0);
4546
string path = Path.Combine(directory, FileName);
4647

4748
if (_isLoaded)
@@ -62,6 +63,7 @@ public void Load(string directory)
6263

6364
public void Save(string directory)
6465
{
66+
EditorUtility.DisplayProgressBar("AssetDependencyCache", "Saving cache", 0);
6567
string path = Path.Combine(directory, FileName);
6668

6769
if (!Directory.Exists(directory))

NodeDependencyLookup/Editor/Caches/AssetDependencyCache/AssetDependencyCacheSerializer.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public static byte[] Serialize(FileToAssetNode[] fileToAssetNodes)
5353

5454
CacheSerializerUtils.EncodeString(EOF, ref bytes, ref offset);
5555

56-
Deserialize(bytes);
57-
5856
return bytes;
5957
}
6058

@@ -85,7 +83,6 @@ public static FileToAssetNode[] Deserialize(byte[] bytes)
8583
{
8684
string assetId = CacheSerializerUtils.DecodeString(ref bytes, ref offset);
8785
AssetNode assetNode = new AssetNode(assetId);
88-
8986
int numResolverDatas = CacheSerializerUtils.DecodeShort(ref bytes, ref offset);
9087

9188
for (int j = 0; j < numResolverDatas; ++j)
@@ -94,7 +91,6 @@ public static FileToAssetNode[] Deserialize(byte[] bytes)
9491

9592
data.ResolverId = CacheSerializerUtils.DecodeString(ref bytes, ref offset);
9693
data.Dependencies = CacheSerializerUtils.DecodeDependencies(ref bytes, ref offset);
97-
9894
assetNode.ResolverDatas.Add(data);
9995
}
10096

NodeDependencyLookup/Editor/Caches/AssetDependencyCache/AssetTraverser/AssetSerializedPropertyTraverser.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,8 @@ public override void TraverseObject(string id, Object obj, Stack<PathSegment> st
9494
do
9595
{
9696
SerializedPropertyType propertyType = property.propertyType;
97-
98-
if (propertyType == SerializedPropertyType.Character |
99-
propertyType == SerializedPropertyType.Integer |
100-
propertyType == SerializedPropertyType.Float |
101-
propertyType == SerializedPropertyType.String |
102-
propertyType == SerializedPropertyType.Vector4 |
103-
propertyType == SerializedPropertyType.Vector2 |
104-
propertyType == SerializedPropertyType.Vector3 |
105-
propertyType == SerializedPropertyType.ArraySize
106-
)
97+
98+
if (propertyType != SerializedPropertyType.ObjectReference && propertyType != SerializedPropertyType.Generic)
10799
{
108100
continue;
109101
}

NodeDependencyLookup/Editor/Caches/AssetDependencyCache/AssetTraverser/AssetTraverser.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using UnityEditor;
34
using UnityEditor.SceneManagement;
45
using UnityEngine;
@@ -66,6 +67,7 @@ public void TraverseScene(string id, Object obj, Stack<PathSegment> stack)
6667
public void TraverseGameObject(string id, Object obj, Stack<PathSegment> stack, Object currentPrefab, bool isRoot)
6768
{
6869
var go = obj as GameObject;
70+
bool isPrefabInstance = false;
6971
bool onlyOverriden = false;
7072

7173
#if UNITY_2018_3_OR_NEWER
@@ -76,6 +78,7 @@ public void TraverseGameObject(string id, Object obj, Stack<PathSegment> stack,
7678
if (PrefabUtility.GetCorrespondingObjectFromSource(go))
7779
{
7880
onlyOverriden = true;
81+
isPrefabInstance = true;
7982
var prefabObj = PrefabUtility.GetPrefabInstanceHandle(obj);
8083

8184
if(prefabObj != currentPrefab)
@@ -111,7 +114,9 @@ public void TraverseGameObject(string id, Object obj, Stack<PathSegment> stack,
111114
}
112115
#endif
113116

114-
Dictionary<string, int> componentToCount = new Dictionary<string, int>();
117+
Dictionary<string, int> componentToCount = new Dictionary<string, int>();
118+
119+
List<AddedComponent> addedComponents = isPrefabInstance ? PrefabUtility.GetAddedComponents(go) : null;
115120

116121
foreach (Component component in go.GetComponents<Component>())
117122
{
@@ -120,19 +125,26 @@ public void TraverseGameObject(string id, Object obj, Stack<PathSegment> stack,
120125
continue;
121126
}
122127

128+
bool componentOverriden = onlyOverriden;
129+
130+
if (isPrefabInstance)
131+
{
132+
bool isAddedComponent = addedComponents.Any(addedComponent => addedComponent.instanceComponent == component);
133+
componentOverriden &= !isAddedComponent;
134+
}
135+
123136
string componentName = component.GetType().Name;
124137

125138
if (!componentToCount.ContainsKey(componentName))
126139
{
127140
componentToCount.Add(componentName, 1);
128141
}
129142

130-
int count = componentToCount[componentName]++;
143+
int sameComponentCount = componentToCount[componentName]++;
144+
string segmentName = sameComponentCount > 1 ? $"{componentName}_{sameComponentCount}" : componentName;
131145

132-
string segmentName = count > 1 ? string.Format("{0}_{1}", componentName, count) : componentName;
133-
134146
stack.Push(new PathSegment(segmentName, PathSegmentType.Component));
135-
TraverseObject(id, component, stack, onlyOverriden);
147+
TraverseObject(id, component, stack, componentOverriden);
136148
stack.Pop();
137149
}
138150

NodeDependencyLookup/Editor/Caches/OpenSceneDependencyCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ public int GetOwnFileSize(string id, string type, NodeDependencyLookupContext st
312312
return 0;
313313
}
314314

315-
public bool IsNodePackedToApp(string id, string type)
315+
public bool IsNodePackedToApp(string id, string type, bool alwaysExcluded)
316316
{
317-
return true;
317+
return false;
318318
}
319319

320320
public bool IsNodeEditorOnly(string id, string type)

NodeDependencyLookup/Editor/NodeHandler/AssetNodeHandler.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,20 @@ public int GetOwnFileSize(string id, string type, NodeDependencyLookupContext st
2626
return 0;
2727
}
2828

29-
public bool IsNodePackedToApp(string id, string type)
29+
public bool IsNodePackedToApp(string id, string type, bool alwaysExcluded = false)
3030
{
31-
string path = AssetDatabase.GUIDToAssetPath(id);
32-
33-
if (IsNodeEditorOnly(id, type))
31+
if (alwaysExcluded)
3432
{
35-
return false;
33+
return !IsNodeEditorOnly(id, type);
3634
}
37-
38-
/*if (IsSceneAndPacked(path) || IsInResources(path))
39-
{
40-
return true;
41-
}*/
42-
43-
return false;
35+
36+
string path = AssetDatabase.GUIDToAssetPath(NodeDependencyLookupUtility.GetGuidFromAssetId(id));
37+
return IsSceneAndPacked(path) || IsInResources(path) || id.StartsWith("0000000");
4438
}
4539

4640
public bool IsNodeEditorOnly(string id, string type)
4741
{
48-
string path = AssetDatabase.GUIDToAssetPath(id);
42+
string path = AssetDatabase.GUIDToAssetPath(NodeDependencyLookupUtility.GetGuidFromAssetId(id));
4943
return path.Contains("/Editor/");
5044
}
5145

NodeDependencyLookup/Editor/NodeHandler/FileNodeHandler.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@ public int GetOwnFileSize(string id, string type, NodeDependencyLookupContext st
2626
return NodeDependencyLookupUtility.GetPackedAssetSize(id);
2727
}
2828

29-
public bool IsNodePackedToApp(string id, string type)
29+
public bool IsNodePackedToApp(string id, string type, bool alwaysExcluded)
3030
{
31-
string path = AssetDatabase.GUIDToAssetPath(id);
32-
33-
if (IsNodeEditorOnly(id, type))
34-
{
35-
return false;
36-
}
37-
38-
if (IsSceneAndPacked(path) || IsInResources(path))
31+
if (alwaysExcluded)
3932
{
40-
return true;
33+
return !IsNodeEditorOnly(id, type);
4134
}
42-
43-
return false;
35+
36+
string path = AssetDatabase.GUIDToAssetPath(id);
37+
return IsSceneAndPacked(path) || IsInResources(path) || id.StartsWith("0000000");
4438
}
4539

4640
public bool IsNodeEditorOnly(string id, string type)

NodeDependencyLookup/Editor/NodeHandler/INodeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface INodeHandler
1212
// Returns the filesize if the node. In case of an asset it would be the serialized filesize
1313
int GetOwnFileSize(string id, string type, NodeDependencyLookupContext stateContext);
1414
// Returns if a node is packed to the app or not. Helpful to find out if an asset is actually used in the final game or not
15-
bool IsNodePackedToApp(string id, string type);
15+
bool IsNodePackedToApp(string id, string type, bool alwaysExcluded = false);
1616
// Returns if a node it just used within the editor. For assets this would be case if its in an editor folder
1717
bool IsNodeEditorOnly(string id, string type);
1818
// Returns if the assets contributes to the overall tree size (size of all dependencies together)

0 commit comments

Comments
 (0)