From 6a158cba7ef33d38b5fcfa16e6c9a36c664df945 Mon Sep 17 00:00:00 2001 From: yasirkula Date: Sun, 21 Dec 2025 16:06:47 +0300 Subject: [PATCH] Plugin is now compatible with Unity 6.3 (fixed #46)(fixed #47) --- .../Editor/AssetUsageDetector.cs | 141 +++++++++--------- .../AssetUsageDetectorSearchFunctions.cs | 13 +- .../Editor/SearchRefactoring.cs | 11 +- .../AssetUsageDetector/Editor/SearchResult.cs | 16 +- .../Editor/SearchResultTreeView.cs | 5 + .../AssetUsageDetector/Editor/Utilities.cs | 22 +++ 6 files changed, 125 insertions(+), 83 deletions(-) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 45a03dc..374db0c 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -8,6 +8,11 @@ using System; using System.IO; using System.Text; using Object = UnityEngine.Object; +#if UNITY_6000_3_OR_NEWER +using EntityId = UnityEngine.EntityId; +#else +using EntityId = System.Int32; +#endif namespace AssetUsageDetectorNamespace { @@ -76,7 +81,7 @@ namespace AssetUsageDetectorNamespace // An optimization to search an object only once (key is a hash of the searched object) private readonly Dictionary searchedObjects = new Dictionary( 4096 ); - private readonly Dictionary searchedUnityObjects = new Dictionary( 32768 ); // Unity objects use their instanceIDs as key which is more performant + private readonly Dictionary searchedUnityObjects = new(32768); // Unity objects use their EntityIds as key // Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function) private readonly List callStack = new List( 64 ); @@ -330,9 +335,9 @@ namespace AssetUsageDetectorNamespace // Initialize data used by search functions InitializeSearchFunctionsData( searchParameters ); - // Initialize the nodes of searched asset(s) - foreach( Object obj in objectsToSearchSet ) - searchedUnityObjects.Add( obj.GetInstanceID(), PopReferenceNode( obj ) ); + // Initialize the nodes of searched asset(s) + foreach (Object obj in objectsToSearchSet) + searchedUnityObjects.Add(obj.GetEntityId(), PopReferenceNode(obj)); // Progressbar values int searchProgress = 0; @@ -660,8 +665,8 @@ namespace AssetUsageDetectorNamespace usedObjectPathsSet.Add( assetPath ); else { - for( Transform parent = ( (GameObject) obj ).transform.parent; parent != null; parent = parent.parent ) - usedObjectPathsSet.Add( parent.gameObject.GetInstanceID().ToString() ); + for (Transform parent = ((GameObject)obj).transform.parent; parent != null; parent = parent.parent) + usedObjectPathsSet.Add(parent.gameObject.GetEntityId().ToString()); } } } @@ -732,14 +737,14 @@ namespace AssetUsageDetectorNamespace { if( !searchedTopmostGameObject ) { - if( obj is GameObject ) - unusedMainObjectNodes[obj.GetInstanceID().ToString()] = node; - else - currentSearchResultGroup.AddReference( node ); + if (obj is GameObject) + unusedMainObjectNodes[obj.GetEntityId().ToString()] = node; + else + currentSearchResultGroup.AddReference(node); } else // List child GameObject scene objects under their parent GameObject { - string dictionaryKey = searchedTopmostGameObject.GetInstanceID().ToString(); + string dictionaryKey = searchedTopmostGameObject.GetEntityId().ToString(); List unusedSubObjectNodesAtPath; if( !unusedSubObjectNodes.TryGetValue( dictionaryKey, out unusedSubObjectNodesAtPath ) ) unusedSubObjectNodes[dictionaryKey] = unusedSubObjectNodesAtPath = new List( 2 ); @@ -1016,14 +1021,14 @@ namespace AssetUsageDetectorNamespace { if( assetsToSearchSet.Count == 0 ) { - searchedUnityObjects.Add( unityObject.GetInstanceID(), null ); + searchedUnityObjects.Add(unityObject.GetEntityId(), null); return null; } assetPath = AssetDatabase.GetAssetPath( unityObject ); if( excludedAssetsPathsSet.Contains( assetPath ) || !AssetHasAnyReference( assetPath ) ) { - searchedUnityObjects.Add( unityObject.GetInstanceID(), null ); + searchedUnityObjects.Add(unityObject.GetEntityId(), null); return null; } } @@ -1085,24 +1090,24 @@ namespace AssetUsageDetectorNamespace result = null; } - // Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null) - // and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something - // or we are at the root of the search; i.e. currentDepth == 0) - if( !( obj is ValueType ) && ( result != null || unityObject != null || currentDepth == 0 ) ) - { - if( !searchingSourceAsset ) - { - if( obj is Object ) - searchedUnityObjects.Add( unityObject.GetInstanceID(), result ); - else - searchedObjects.Add( GetNodeObjectHash( obj ), result ); - } - else if( result != null ) - { - result.CopyReferencesTo( searchedUnityObjects[unityObject.GetInstanceID()] ); - PoolReferenceNode( result ); - } - } + // Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null) + // and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something + // or we are at the root of the search; i.e. currentDepth == 0) + if (!(obj is ValueType) && (result != null || unityObject != null || currentDepth == 0)) + { + if (!searchingSourceAsset) + { + if (obj is Object) + searchedUnityObjects.Add(unityObject.GetEntityId(), result); + else + searchedObjects.Add(GetNodeObjectHash(obj), result); + } + else if (result != null) + { + result.CopyReferencesTo(searchedUnityObjects[unityObject.GetEntityId()]); + PoolReferenceNode(result); + } + } return result; } @@ -1228,46 +1233,46 @@ namespace AssetUsageDetectorNamespace return false; } - // If object was already searched, return its ReferenceNode - private bool TryGetReferenceNode( object nodeObject, out ReferenceNode referenceNode ) - { - if( nodeObject is Object ) - { - if( searchedUnityObjects.TryGetValue( ( (Object) nodeObject ).GetInstanceID(), out referenceNode ) ) - return true; - } - else if( searchedObjects.TryGetValue( GetNodeObjectHash( nodeObject ), out referenceNode ) ) - return true; + // If object was already searched, return its ReferenceNode + private bool TryGetReferenceNode(object nodeObject, out ReferenceNode referenceNode) + { + if (nodeObject is Object unityObject) + { + if (searchedUnityObjects.TryGetValue(unityObject.GetEntityId(), out referenceNode)) + return true; + } + else if (searchedObjects.TryGetValue(GetNodeObjectHash(nodeObject), out referenceNode)) + return true; - referenceNode = null; - return false; - } + referenceNode = null; + return false; + } - // Get reference node for object - private ReferenceNode GetReferenceNode( object nodeObject ) - { - ReferenceNode result; - if( nodeObject is Object ) - { - int hash = ( (Object) nodeObject ).GetInstanceID(); - if( !searchedUnityObjects.TryGetValue( hash, out result ) || result == null ) - { - result = PopReferenceNode( nodeObject ); - searchedUnityObjects[hash] = result; - } - } - else - { - string hash = GetNodeObjectHash( nodeObject ); - if( !searchedObjects.TryGetValue( hash, out result ) || result == null ) - { - result = PopReferenceNode( nodeObject ); - searchedObjects[hash] = result; - } - } + // Get reference node for object + private ReferenceNode GetReferenceNode(object nodeObject) + { + ReferenceNode result; + if (nodeObject is Object unityObject) + { + EntityId hash = unityObject.GetEntityId(); + if (!searchedUnityObjects.TryGetValue(hash, out result) || result == null) + { + result = PopReferenceNode(nodeObject); + searchedUnityObjects[hash] = result; + } + } + else + { + string hash = GetNodeObjectHash(nodeObject); + if (!searchedObjects.TryGetValue(hash, out result) || result == null) + { + result = PopReferenceNode(nodeObject); + searchedObjects[hash] = result; + } + } - return result; - } + return result; + } // Fetch a reference node from pool private ReferenceNode PopReferenceNode( object nodeObject ) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index 039c464..92e1087 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -12,6 +12,7 @@ using UnityEngine.U2D; using UnityEngine.Playables; using UnityEditor.U2D; using UnityEditor.Compilation; +using UnityEngine.Rendering; using UnityEngine.Tilemaps; #if ASSET_USAGE_ADDRESSABLES using UnityEngine.AddressableAssets; @@ -643,12 +644,12 @@ namespace AssetUsageDetectorNamespace if( searchTextureReferences && isInPlayMode && !AssetDatabase.Contains( material ) ) { Shader shader = material.shader; - int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader ); + int shaderPropertyCount = shader.GetPropertyCount(); for( int i = 0; i < shaderPropertyCount; i++ ) { - if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv ) + if (shader.GetPropertyType(i) == ShaderPropertyType.Texture) { - string propertyName = ShaderUtil.GetPropertyName( shader, i ); + string propertyName = shader.GetPropertyName(i); Texture assignedTexture = material.GetTexture( propertyName ); if( objectsToSearchSet.Contains( assignedTexture ) ) { @@ -675,12 +676,12 @@ namespace AssetUsageDetectorNamespace ShaderImporter shaderImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( shader ) ) as ShaderImporter; if( shaderImporter != null ) { - int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader ); + int shaderPropertyCount = shader.GetPropertyCount(); for( int i = 0; i < shaderPropertyCount; i++ ) { - if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv ) + if (shader.GetPropertyType(i) == ShaderPropertyType.Texture) { - string propertyName = ShaderUtil.GetPropertyName( shader, i ); + string propertyName = shader.GetPropertyName(i); Texture defaultTexture = shaderImporter.GetDefaultTexture( propertyName ); if( !defaultTexture ) defaultTexture = shaderImporter.GetNonModifiableTexture( propertyName ); diff --git a/Plugins/AssetUsageDetector/Editor/SearchRefactoring.cs b/Plugins/AssetUsageDetector/Editor/SearchRefactoring.cs index 18f5a0f..6591680 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchRefactoring.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchRefactoring.cs @@ -5,6 +5,7 @@ using System.Reflection; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; +using UnityEngine.Rendering; using Object = UnityEngine.Object; namespace AssetUsageDetectorNamespace @@ -289,14 +290,14 @@ namespace AssetUsageDetectorNamespace List nonModifiableTextureNames = new List( 16 ); List nonModifiableTextureValues = new List( 16 ); - int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader ); + int shaderPropertyCount = shader.GetPropertyCount(); for( int i = 0; i < shaderPropertyCount; i++ ) { - if( ShaderUtil.GetPropertyType( shader, i ) != ShaderUtil.ShaderPropertyType.TexEnv ) - continue; + if (shader.GetPropertyType(i) != ShaderPropertyType.Texture) + continue; - string propertyName = ShaderUtil.GetPropertyName( shader, i ); - if( ShaderUtil.IsShaderPropertyNonModifiableTexureProperty( shader, i ) ) + string propertyName = shader.GetPropertyName(i); + if ((shader.GetPropertyFlags(i) & ShaderPropertyFlags.NonModifiableTextureData) != 0) { Texture propertyDefaultValue = shaderImporter.GetNonModifiableTexture( propertyName ); if( propertyDefaultValue == Value && propertyName == Variable ) diff --git a/Plugins/AssetUsageDetector/Editor/SearchResult.cs b/Plugins/AssetUsageDetector/Editor/SearchResult.cs index fc20b23..c289a59 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResult.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResult.cs @@ -9,6 +9,14 @@ using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; using Object = UnityEngine.Object; +#if UNITY_6000_3_OR_NEWER +using EntityId = UnityEngine.EntityId; +#else +using EntityId = System.Int32; +#endif +#if UNITY_6000_2_OR_NEWER +using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem; +#endif namespace AssetUsageDetectorNamespace { @@ -39,7 +47,7 @@ namespace AssetUsageDetectorNamespace } public string label; - public int instanceId; + public EntityId instanceId; public bool isUnityObject, isMainReference; public ReferenceNode.UsedState usedState; @@ -1127,8 +1135,8 @@ namespace AssetUsageDetectorNamespace public bool IsMainReference { get; private set; } // True: if belongs to a scene search result group, then it's an object in that scene. If belongs to the assets search result group, then it's an asset internal object nodeObject; - private int? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise - public Object UnityObject { get { return instanceId.HasValue ? EditorUtility.InstanceIDToObject( instanceId.Value ) : null; } } + private EntityId? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise + public Object UnityObject => instanceId.HasValue ? Utilities.EntityIdToObject(instanceId.Value) : null; private readonly List links = new List( 2 ); public int NumberOfOutgoingLinks { get { return links.Count; } } @@ -1222,7 +1230,7 @@ namespace AssetUsageDetectorNamespace Object unityObject = nodeObject as Object; if( unityObject != null ) { - instanceId = unityObject.GetInstanceID(); + instanceId = unityObject.GetEntityId(); Label = unityObject.name + " (" + unityObject.GetType().Name + ")"; if( AssetUsageDetectorSettings.ShowRootAssetName && unityObject.IsAsset() && !AssetDatabase.IsMainAsset( unityObject ) ) diff --git a/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs b/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs index a612899..e35a7cd 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs @@ -6,6 +6,11 @@ using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEditor.SceneManagement; using UnityEngine; +#if UNITY_6000_2_OR_NEWER +using TreeView = UnityEditor.IMGUI.Controls.TreeView; +using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem; +using TreeViewState = UnityEditor.IMGUI.Controls.TreeViewState; +#endif namespace AssetUsageDetectorNamespace { diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 61f47d6..5dcd697 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -174,6 +174,28 @@ namespace AssetUsageDetectorNamespace return false; } +#if UNITY_6000_3_OR_NEWER + public static EntityId GetEntityId(this Object obj) + { + return obj.GetEntityId(); + } + + public static Object EntityIdToObject(EntityId entityId) + { + return EditorUtility.EntityIdToObject(entityId); + } +#else + public static int GetEntityId(this Object obj) + { + return obj.GetInstanceID(); + } + + public static Object EntityIdToObject(int instanceID) + { + return EditorUtility.InstanceIDToObject(instanceID); + } +#endif + // Returns an enumerator to iterate through all asset paths in the folder public static IEnumerable EnumerateFolderContents( Object folderAsset ) {