|
8 | 8 | using System.IO; |
9 | 9 | using System.Text; |
10 | 10 | using Object = UnityEngine.Object; |
| 11 | +#if UNITY_6000_3_OR_NEWER |
| 12 | +using EntityId = UnityEngine.EntityId; |
| 13 | +#else |
| 14 | +using EntityId = System.Int32; |
| 15 | +#endif |
11 | 16 |
|
12 | 17 | namespace AssetUsageDetectorNamespace |
13 | 18 | { |
@@ -76,7 +81,7 @@ public class Parameters |
76 | 81 |
|
77 | 82 | // An optimization to search an object only once (key is a hash of the searched object) |
78 | 83 | private readonly Dictionary<string, ReferenceNode> searchedObjects = new Dictionary<string, ReferenceNode>( 4096 ); |
79 | | - private readonly Dictionary<int, ReferenceNode> searchedUnityObjects = new Dictionary<int, ReferenceNode>( 32768 ); // Unity objects use their instanceIDs as key which is more performant |
| 84 | + private readonly Dictionary<EntityId, ReferenceNode> searchedUnityObjects = new(32768); // Unity objects use their EntityIds as key |
80 | 85 |
|
81 | 86 | // Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function) |
82 | 87 | private readonly List<object> callStack = new List<object>( 64 ); |
@@ -330,9 +335,9 @@ public SearchResult Run( Parameters searchParameters ) |
330 | 335 | // Initialize data used by search functions |
331 | 336 | InitializeSearchFunctionsData( searchParameters ); |
332 | 337 |
|
333 | | - // Initialize the nodes of searched asset(s) |
334 | | - foreach( Object obj in objectsToSearchSet ) |
335 | | - searchedUnityObjects.Add( obj.GetInstanceID(), PopReferenceNode( obj ) ); |
| 338 | + // Initialize the nodes of searched asset(s) |
| 339 | + foreach (Object obj in objectsToSearchSet) |
| 340 | + searchedUnityObjects.Add(obj.GetEntityId(), PopReferenceNode(obj)); |
336 | 341 |
|
337 | 342 | // Progressbar values |
338 | 343 | int searchProgress = 0; |
@@ -660,8 +665,8 @@ private void CalculateUnusedObjects( List<SearchResultGroup> searchResult, out H |
660 | 665 | usedObjectPathsSet.Add( assetPath ); |
661 | 666 | else |
662 | 667 | { |
663 | | - for( Transform parent = ( (GameObject) obj ).transform.parent; parent != null; parent = parent.parent ) |
664 | | - usedObjectPathsSet.Add( parent.gameObject.GetInstanceID().ToString() ); |
| 668 | + for (Transform parent = ((GameObject)obj).transform.parent; parent != null; parent = parent.parent) |
| 669 | + usedObjectPathsSet.Add(parent.gameObject.GetEntityId().ToString()); |
665 | 670 | } |
666 | 671 | } |
667 | 672 | } |
@@ -732,14 +737,14 @@ private void CalculateUnusedObjects( List<SearchResultGroup> searchResult, out H |
732 | 737 | { |
733 | 738 | if( !searchedTopmostGameObject ) |
734 | 739 | { |
735 | | - if( obj is GameObject ) |
736 | | - unusedMainObjectNodes[obj.GetInstanceID().ToString()] = node; |
737 | | - else |
738 | | - currentSearchResultGroup.AddReference( node ); |
| 740 | + if (obj is GameObject) |
| 741 | + unusedMainObjectNodes[obj.GetEntityId().ToString()] = node; |
| 742 | + else |
| 743 | + currentSearchResultGroup.AddReference(node); |
739 | 744 | } |
740 | 745 | else // List child GameObject scene objects under their parent GameObject |
741 | 746 | { |
742 | | - string dictionaryKey = searchedTopmostGameObject.GetInstanceID().ToString(); |
| 747 | + string dictionaryKey = searchedTopmostGameObject.GetEntityId().ToString(); |
743 | 748 | List<ReferenceNode> unusedSubObjectNodesAtPath; |
744 | 749 | if( !unusedSubObjectNodes.TryGetValue( dictionaryKey, out unusedSubObjectNodesAtPath ) ) |
745 | 750 | unusedSubObjectNodes[dictionaryKey] = unusedSubObjectNodesAtPath = new List<ReferenceNode>( 2 ); |
@@ -1016,14 +1021,14 @@ private ReferenceNode SearchObject( object obj ) |
1016 | 1021 | { |
1017 | 1022 | if( assetsToSearchSet.Count == 0 ) |
1018 | 1023 | { |
1019 | | - searchedUnityObjects.Add( unityObject.GetInstanceID(), null ); |
| 1024 | + searchedUnityObjects.Add(unityObject.GetEntityId(), null); |
1020 | 1025 | return null; |
1021 | 1026 | } |
1022 | 1027 |
|
1023 | 1028 | assetPath = AssetDatabase.GetAssetPath( unityObject ); |
1024 | 1029 | if( excludedAssetsPathsSet.Contains( assetPath ) || !AssetHasAnyReference( assetPath ) ) |
1025 | 1030 | { |
1026 | | - searchedUnityObjects.Add( unityObject.GetInstanceID(), null ); |
| 1031 | + searchedUnityObjects.Add(unityObject.GetEntityId(), null); |
1027 | 1032 | return null; |
1028 | 1033 | } |
1029 | 1034 | } |
@@ -1085,24 +1090,24 @@ private ReferenceNode SearchObject( object obj ) |
1085 | 1090 | result = null; |
1086 | 1091 | } |
1087 | 1092 |
|
1088 | | - // Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null) |
1089 | | - // and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something |
1090 | | - // or we are at the root of the search; i.e. currentDepth == 0) |
1091 | | - if( !( obj is ValueType ) && ( result != null || unityObject != null || currentDepth == 0 ) ) |
1092 | | - { |
1093 | | - if( !searchingSourceAsset ) |
1094 | | - { |
1095 | | - if( obj is Object ) |
1096 | | - searchedUnityObjects.Add( unityObject.GetInstanceID(), result ); |
1097 | | - else |
1098 | | - searchedObjects.Add( GetNodeObjectHash( obj ), result ); |
1099 | | - } |
1100 | | - else if( result != null ) |
1101 | | - { |
1102 | | - result.CopyReferencesTo( searchedUnityObjects[unityObject.GetInstanceID()] ); |
1103 | | - PoolReferenceNode( result ); |
1104 | | - } |
1105 | | - } |
| 1093 | + // Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null) |
| 1094 | + // and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something |
| 1095 | + // or we are at the root of the search; i.e. currentDepth == 0) |
| 1096 | + if (!(obj is ValueType) && (result != null || unityObject != null || currentDepth == 0)) |
| 1097 | + { |
| 1098 | + if (!searchingSourceAsset) |
| 1099 | + { |
| 1100 | + if (obj is Object) |
| 1101 | + searchedUnityObjects.Add(unityObject.GetEntityId(), result); |
| 1102 | + else |
| 1103 | + searchedObjects.Add(GetNodeObjectHash(obj), result); |
| 1104 | + } |
| 1105 | + else if (result != null) |
| 1106 | + { |
| 1107 | + result.CopyReferencesTo(searchedUnityObjects[unityObject.GetEntityId()]); |
| 1108 | + PoolReferenceNode(result); |
| 1109 | + } |
| 1110 | + } |
1106 | 1111 |
|
1107 | 1112 | return result; |
1108 | 1113 | } |
@@ -1228,46 +1233,46 @@ private bool AssetHasAnyReferenceInternal( string assetPath ) |
1228 | 1233 | return false; |
1229 | 1234 | } |
1230 | 1235 |
|
1231 | | - // If object was already searched, return its ReferenceNode |
1232 | | - private bool TryGetReferenceNode( object nodeObject, out ReferenceNode referenceNode ) |
1233 | | - { |
1234 | | - if( nodeObject is Object ) |
1235 | | - { |
1236 | | - if( searchedUnityObjects.TryGetValue( ( (Object) nodeObject ).GetInstanceID(), out referenceNode ) ) |
1237 | | - return true; |
1238 | | - } |
1239 | | - else if( searchedObjects.TryGetValue( GetNodeObjectHash( nodeObject ), out referenceNode ) ) |
1240 | | - return true; |
1241 | | - |
1242 | | - referenceNode = null; |
1243 | | - return false; |
1244 | | - } |
1245 | | - |
1246 | | - // Get reference node for object |
1247 | | - private ReferenceNode GetReferenceNode( object nodeObject ) |
1248 | | - { |
1249 | | - ReferenceNode result; |
1250 | | - if( nodeObject is Object ) |
1251 | | - { |
1252 | | - int hash = ( (Object) nodeObject ).GetInstanceID(); |
1253 | | - if( !searchedUnityObjects.TryGetValue( hash, out result ) || result == null ) |
1254 | | - { |
1255 | | - result = PopReferenceNode( nodeObject ); |
1256 | | - searchedUnityObjects[hash] = result; |
1257 | | - } |
1258 | | - } |
1259 | | - else |
1260 | | - { |
1261 | | - string hash = GetNodeObjectHash( nodeObject ); |
1262 | | - if( !searchedObjects.TryGetValue( hash, out result ) || result == null ) |
1263 | | - { |
1264 | | - result = PopReferenceNode( nodeObject ); |
1265 | | - searchedObjects[hash] = result; |
1266 | | - } |
1267 | | - } |
1268 | | - |
1269 | | - return result; |
1270 | | - } |
| 1236 | + // If object was already searched, return its ReferenceNode |
| 1237 | + private bool TryGetReferenceNode(object nodeObject, out ReferenceNode referenceNode) |
| 1238 | + { |
| 1239 | + if (nodeObject is Object unityObject) |
| 1240 | + { |
| 1241 | + if (searchedUnityObjects.TryGetValue(unityObject.GetEntityId(), out referenceNode)) |
| 1242 | + return true; |
| 1243 | + } |
| 1244 | + else if (searchedObjects.TryGetValue(GetNodeObjectHash(nodeObject), out referenceNode)) |
| 1245 | + return true; |
| 1246 | + |
| 1247 | + referenceNode = null; |
| 1248 | + return false; |
| 1249 | + } |
| 1250 | + |
| 1251 | + // Get reference node for object |
| 1252 | + private ReferenceNode GetReferenceNode(object nodeObject) |
| 1253 | + { |
| 1254 | + ReferenceNode result; |
| 1255 | + if (nodeObject is Object unityObject) |
| 1256 | + { |
| 1257 | + EntityId hash = unityObject.GetEntityId(); |
| 1258 | + if (!searchedUnityObjects.TryGetValue(hash, out result) || result == null) |
| 1259 | + { |
| 1260 | + result = PopReferenceNode(nodeObject); |
| 1261 | + searchedUnityObjects[hash] = result; |
| 1262 | + } |
| 1263 | + } |
| 1264 | + else |
| 1265 | + { |
| 1266 | + string hash = GetNodeObjectHash(nodeObject); |
| 1267 | + if (!searchedObjects.TryGetValue(hash, out result) || result == null) |
| 1268 | + { |
| 1269 | + result = PopReferenceNode(nodeObject); |
| 1270 | + searchedObjects[hash] = result; |
| 1271 | + } |
| 1272 | + } |
| 1273 | + |
| 1274 | + return result; |
| 1275 | + } |
1271 | 1276 |
|
1272 | 1277 | // Fetch a reference node from pool |
1273 | 1278 | private ReferenceNode PopReferenceNode( object nodeObject ) |
|
0 commit comments