Skip to content

Commit 6a158cb

Browse files
committed
Plugin is now compatible with Unity 6.3 (fixed #46)(fixed #47)
1 parent 652dbe2 commit 6a158cb

6 files changed

Lines changed: 128 additions & 86 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
using System.IO;
99
using System.Text;
1010
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
1116

1217
namespace AssetUsageDetectorNamespace
1318
{
@@ -76,7 +81,7 @@ public class Parameters
7681

7782
// An optimization to search an object only once (key is a hash of the searched object)
7883
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
8085

8186
// Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function)
8287
private readonly List<object> callStack = new List<object>( 64 );
@@ -330,9 +335,9 @@ public SearchResult Run( Parameters searchParameters )
330335
// Initialize data used by search functions
331336
InitializeSearchFunctionsData( searchParameters );
332337

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));
336341

337342
// Progressbar values
338343
int searchProgress = 0;
@@ -660,8 +665,8 @@ private void CalculateUnusedObjects( List<SearchResultGroup> searchResult, out H
660665
usedObjectPathsSet.Add( assetPath );
661666
else
662667
{
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());
665670
}
666671
}
667672
}
@@ -732,14 +737,14 @@ private void CalculateUnusedObjects( List<SearchResultGroup> searchResult, out H
732737
{
733738
if( !searchedTopmostGameObject )
734739
{
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);
739744
}
740745
else // List child GameObject scene objects under their parent GameObject
741746
{
742-
string dictionaryKey = searchedTopmostGameObject.GetInstanceID().ToString();
747+
string dictionaryKey = searchedTopmostGameObject.GetEntityId().ToString();
743748
List<ReferenceNode> unusedSubObjectNodesAtPath;
744749
if( !unusedSubObjectNodes.TryGetValue( dictionaryKey, out unusedSubObjectNodesAtPath ) )
745750
unusedSubObjectNodes[dictionaryKey] = unusedSubObjectNodesAtPath = new List<ReferenceNode>( 2 );
@@ -1016,14 +1021,14 @@ private ReferenceNode SearchObject( object obj )
10161021
{
10171022
if( assetsToSearchSet.Count == 0 )
10181023
{
1019-
searchedUnityObjects.Add( unityObject.GetInstanceID(), null );
1024+
searchedUnityObjects.Add(unityObject.GetEntityId(), null);
10201025
return null;
10211026
}
10221027

10231028
assetPath = AssetDatabase.GetAssetPath( unityObject );
10241029
if( excludedAssetsPathsSet.Contains( assetPath ) || !AssetHasAnyReference( assetPath ) )
10251030
{
1026-
searchedUnityObjects.Add( unityObject.GetInstanceID(), null );
1031+
searchedUnityObjects.Add(unityObject.GetEntityId(), null);
10271032
return null;
10281033
}
10291034
}
@@ -1085,24 +1090,24 @@ private ReferenceNode SearchObject( object obj )
10851090
result = null;
10861091
}
10871092

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+
}
11061111

11071112
return result;
11081113
}
@@ -1228,46 +1233,46 @@ private bool AssetHasAnyReferenceInternal( string assetPath )
12281233
return false;
12291234
}
12301235

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+
}
12711276

12721277
// Fetch a reference node from pool
12731278
private ReferenceNode PopReferenceNode( object nodeObject )

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using UnityEngine.Playables;
1313
using UnityEditor.U2D;
1414
using UnityEditor.Compilation;
15+
using UnityEngine.Rendering;
1516
using UnityEngine.Tilemaps;
1617
#if ASSET_USAGE_ADDRESSABLES
1718
using UnityEngine.AddressableAssets;
@@ -643,12 +644,12 @@ private ReferenceNode SearchMaterial( object obj )
643644
if( searchTextureReferences && isInPlayMode && !AssetDatabase.Contains( material ) )
644645
{
645646
Shader shader = material.shader;
646-
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
647+
int shaderPropertyCount = shader.GetPropertyCount();
647648
for( int i = 0; i < shaderPropertyCount; i++ )
648649
{
649-
if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv )
650+
if (shader.GetPropertyType(i) == ShaderPropertyType.Texture)
650651
{
651-
string propertyName = ShaderUtil.GetPropertyName( shader, i );
652+
string propertyName = shader.GetPropertyName(i);
652653
Texture assignedTexture = material.GetTexture( propertyName );
653654
if( objectsToSearchSet.Contains( assignedTexture ) )
654655
{
@@ -675,12 +676,12 @@ private ReferenceNode SearchShader( object obj )
675676
ShaderImporter shaderImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( shader ) ) as ShaderImporter;
676677
if( shaderImporter != null )
677678
{
678-
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
679+
int shaderPropertyCount = shader.GetPropertyCount();
679680
for( int i = 0; i < shaderPropertyCount; i++ )
680681
{
681-
if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv )
682+
if (shader.GetPropertyType(i) == ShaderPropertyType.Texture)
682683
{
683-
string propertyName = ShaderUtil.GetPropertyName( shader, i );
684+
string propertyName = shader.GetPropertyName(i);
684685
Texture defaultTexture = shaderImporter.GetDefaultTexture( propertyName );
685686
if( !defaultTexture )
686687
defaultTexture = shaderImporter.GetNonModifiableTexture( propertyName );

Plugins/AssetUsageDetector/Editor/SearchRefactoring.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityEditor;
66
using UnityEditor.SceneManagement;
77
using UnityEngine;
8+
using UnityEngine.Rendering;
89
using Object = UnityEngine.Object;
910

1011
namespace AssetUsageDetectorNamespace
@@ -289,14 +290,14 @@ protected override bool ChangeValue( Object newValue, out bool setContextDirty )
289290
List<string> nonModifiableTextureNames = new List<string>( 16 );
290291
List<Texture> nonModifiableTextureValues = new List<Texture>( 16 );
291292

292-
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
293+
int shaderPropertyCount = shader.GetPropertyCount();
293294
for( int i = 0; i < shaderPropertyCount; i++ )
294295
{
295-
if( ShaderUtil.GetPropertyType( shader, i ) != ShaderUtil.ShaderPropertyType.TexEnv )
296-
continue;
296+
if (shader.GetPropertyType(i) != ShaderPropertyType.Texture)
297+
continue;
297298

298-
string propertyName = ShaderUtil.GetPropertyName( shader, i );
299-
if( ShaderUtil.IsShaderPropertyNonModifiableTexureProperty( shader, i ) )
299+
string propertyName = shader.GetPropertyName(i);
300+
if ((shader.GetPropertyFlags(i) & ShaderPropertyFlags.NonModifiableTextureData) != 0)
300301
{
301302
Texture propertyDefaultValue = shaderImporter.GetNonModifiableTexture( propertyName );
302303
if( propertyDefaultValue == Value && propertyName == Variable )

Plugins/AssetUsageDetector/Editor/SearchResult.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
using UnityEngine;
1010
using UnityEngine.SceneManagement;
1111
using Object = UnityEngine.Object;
12+
#if UNITY_6000_3_OR_NEWER
13+
using EntityId = UnityEngine.EntityId;
14+
#else
15+
using EntityId = System.Int32;
16+
#endif
17+
#if UNITY_6000_2_OR_NEWER
18+
using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem<int>;
19+
#endif
1220

1321
namespace AssetUsageDetectorNamespace
1422
{
@@ -39,7 +47,7 @@ public class SerializableLinkDescriptions
3947
}
4048

4149
public string label;
42-
public int instanceId;
50+
public EntityId instanceId;
4351
public bool isUnityObject, isMainReference;
4452
public ReferenceNode.UsedState usedState;
4553

@@ -1127,8 +1135,8 @@ public Link( ReferenceNode targetNode, List<string> descriptions, bool isWeakLin
11271135
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
11281136

11291137
internal object nodeObject;
1130-
private int? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise
1131-
public Object UnityObject { get { return instanceId.HasValue ? EditorUtility.InstanceIDToObject( instanceId.Value ) : null; } }
1138+
private EntityId? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise
1139+
public Object UnityObject => instanceId.HasValue ? Utilities.EntityIdToObject(instanceId.Value) : null;
11321140

11331141
private readonly List<Link> links = new List<Link>( 2 );
11341142
public int NumberOfOutgoingLinks { get { return links.Count; } }
@@ -1222,7 +1230,7 @@ public void InitializeRecursively()
12221230
Object unityObject = nodeObject as Object;
12231231
if( unityObject != null )
12241232
{
1225-
instanceId = unityObject.GetInstanceID();
1233+
instanceId = unityObject.GetEntityId();
12261234
Label = unityObject.name + " (" + unityObject.GetType().Name + ")";
12271235

12281236
if( AssetUsageDetectorSettings.ShowRootAssetName && unityObject.IsAsset() && !AssetDatabase.IsMainAsset( unityObject ) )

Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
using UnityEditor.IMGUI.Controls;
77
using UnityEditor.SceneManagement;
88
using UnityEngine;
9+
#if UNITY_6000_2_OR_NEWER
10+
using TreeView = UnityEditor.IMGUI.Controls.TreeView<int>;
11+
using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem<int>;
12+
using TreeViewState = UnityEditor.IMGUI.Controls.TreeViewState<int>;
13+
#endif
914

1015
namespace AssetUsageDetectorNamespace
1116
{

Plugins/AssetUsageDetector/Editor/Utilities.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ public static bool HasAnyPrefabOverrides( this GameObject gameObject )
174174
return false;
175175
}
176176

177+
#if UNITY_6000_3_OR_NEWER
178+
public static EntityId GetEntityId(this Object obj)
179+
{
180+
return obj.GetEntityId();
181+
}
182+
183+
public static Object EntityIdToObject(EntityId entityId)
184+
{
185+
return EditorUtility.EntityIdToObject(entityId);
186+
}
187+
#else
188+
public static int GetEntityId(this Object obj)
189+
{
190+
return obj.GetInstanceID();
191+
}
192+
193+
public static Object EntityIdToObject(int instanceID)
194+
{
195+
return EditorUtility.InstanceIDToObject(instanceID);
196+
}
197+
#endif
198+
177199
// Returns an enumerator to iterate through all asset paths in the folder
178200
public static IEnumerable<string> EnumerateFolderContents( Object folderAsset )
179201
{

0 commit comments

Comments
 (0)