Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to implement custom AssetReferenceUIRestriction?

Discussion in 'Addressables' started by Peter77, Aug 12, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,403
    I want to implement a custom AssetReferenceUIRestriction. However, my
    ValidateAsset
    method is never called.

    The code should output a message to the Console window when I drag&drop an asset on the AssetReference in the Inspector.

    However, as mentioned, this method is not called, thus no message printed.

    Code (CSharp):
    1. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    2. sealed public class AssetReferenceUITypeRestriction : AssetReferenceUIRestriction
    3. {
    4.     public override bool ValidateAsset(UnityEngine.Object obj)
    5.     {
    6.         Debug.Log(obj.name);
    7.         return true;
    8.     }
    9.  
    10.     public override bool ValidateAsset(string path)
    11.     {
    12.         Debug.Log(path);
    13.         return true;
    14.     }
    15. }
    I then use this attribute inside my MonoBehaviour:
    Code (CSharp):
    1. sealed public class Appearance : MonoBehaviour
    2. {
    3.     [AssetReferenceUITypeRestriction()]
    4.     [SerializeField] AssetReference m_Material = null;
    5. }
    What do I have to do, to make Unity call my custom code?

    I've looked at the AssetReferenceUILabelRestriction implementation, but the ValidateAsset methods return true always, which is also really odd, because it actually does restrict the AssetReference to the specified labels.

    PS: Or is there a "Type Restriction" attribute already? I want to restrict the AssetReference to certain asset types only, such as to Materials only.

    EDIT: I found AssetReferenceGameObject, but it seems there is no AssetReferenceMaterial?
     
    Last edited: Aug 12, 2020
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    563
    public class AssetReferenceMaterial : AssetReferenceT<Material> {}


    I believe this is what you're looking for?
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,403
    That indeed does work. Thank you!

    Code (CSharp):
    1. [System.Serializable]
    2. public class AssetReferenceMaterial : UnityEngine.AddressableAssets.AssetReferenceT<Material>
    3. {
    4.     public AssetReferenceMaterial(string guid) : base(guid) { }
    5. }