Search Unity

AssetReference dropdown very slow when using custom asset reference

Discussion in 'Addressables' started by VoodooDetective, Jul 4, 2020.

  1. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    I wrote an AssetReferenceAudioClip and the drop down is exceedingly slow. Is there any way for me to speed it up or is this just a symptom of the validation?

    Code (CSharp):
    1. using System;
    2. #if UNITY_EDITOR
    3. using UnityEditor;
    4. #endif
    5. using UnityEngine;
    6. using UnityEngine.AddressableAssets;
    7.  
    8. namespace Addressable
    9. {
    10.     /// <summary>
    11.     /// AudioClip AssetReference.
    12.     /// </summary>
    13.     [Serializable]
    14.     public class AssetReferenceAudioClip : AssetReferenceT<AudioClip>
    15.     {
    16.         public AssetReferenceAudioClip(string guid) : base(guid) { }
    17.  
    18. #if UNITY_EDITOR
    19.         public override bool ValidateAsset(string path)
    20.         {
    21.             Type mainAssetTypeAtPath = AssetDatabase.GetMainAssetTypeAtPath(path);
    22.             if (typeof(AudioClip).IsAssignableFrom(mainAssetTypeAtPath))
    23.             {
    24.                 AudioClip clip = AssetDatabase.LoadAssetAtPath(path, typeof(AudioClip)) as AudioClip;
    25.                 return clip != null;
    26.             }
    27.             return false;
    28.         }
    29. #endif
    30.     }
    31. }
    32.  
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    You are loading every single audio clip, of course that's going to be slow. Why are you even trying to load the asset just to check its type twice?
     
  3. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    I guess I must have coded this wrong. What I'd like to do is restrict the type of asset that can be assigned to this asset reference to only be sound files. How would that be achieved?
     
  4. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Code (CSharp):
    1. return typeof(AudioClip).IsAssignableFrom(mainAssetTypeAtPath);
    Does that not work?
     
    VoodooDetective likes this.
  5. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    Oh... Thanks! I must have copy pasted that from somewhere. That's so dumb. Thanks!