yasirkula / UnityAssetUsageDetector

Find usages of the selected asset(s) and/or Object(s) in your Unity project, i.e. list the objects that refer to them
MIT License
1.72k stars 117 forks source link

Assets referenced by polymorphism [SerializeReference] not found #33

Closed Staarter closed 1 year ago

Staarter commented 1 year ago

Description of the bug

Assets that are referenced by polymorphism using the tag [SerializeReference] are not found by AUD.

Reproduction steps

[System.Serializable] public class ContainerBase { }

[System.Serializable] public class ContainerGO : ContainerBase { public GameObject go; }

[CreateAssetMenu(fileName ="AUD_SerializeReference", menuName = "AUD_SerializeReference")] public class DebugAUD_SerializeReference : ScriptableObject { [SerializeReference] public List test = new List();

private void OnValidate()
{
    // Add an empty element here: we cannot do it in inspector by default
    test.Add(new ContainerGO());
}

}

[CreateAssetMenu(fileName ="AUD_Default", menuName = "AUD_Default")] public class DebugAUD_Default : ScriptableObject { public List test = new List(); }



- Create the two ScriptableObject 'AUD_Default' & 'AUD_SerializeReference'.
- Add a prefab gameObject into theses two 'test' fields.
- Try to search this prefab using the AUD window
- Only the ScriptableObject 'AUD_Default' will be found as using the prefab.

![image_2023-02-21_182622092](https://user-images.githubusercontent.com/9812442/220416775-836a6af3-8d46-4b74-b696-add4ee35c46a.png)

**Platform specs**

Please provide the following info if this is a Unity 3D repository.

- Unity version: 2020.3.16
- Platform: Windows
- How did you download the plugin: Asset Store (the asset is up to date)

**Additional info**

I know that Unity have made changes about SerializeField in 2021 or 2022, the serialization can be changed, but I am using 2020..
yasirkula commented 1 year ago

Thank you for bringing this to my attention! I'll include the fix in the next release but in the meantime, you can apply the fix yourself by changing these lines: https://github.com/yasirkula/UnityAssetUsageDetector/blob/8072530c2cc666007d79f0a8aae038a4d2d26878/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs#L1421-L1425

As follows:

case SerializedPropertyType.ManagedReference:
    object managedReferenceValue = GetRawSerializedPropertyValue( iterator );
    propertyValue = managedReferenceValue as Object;
    searchResult = SearchObject( PreferablyGameObject( managedReferenceValue ) );
    enterChildren = false;
    break;
Staarter commented 1 year ago

Thank you for your reply! This fix works like a charm, good job.