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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

AssetReferenceUIRestrictions are not properly made to be customizable

Discussion in 'Addressables' started by jverbeek_ka, Apr 25, 2022.

  1. jverbeek_ka

    jverbeek_ka

    Joined:
    Mar 7, 2022
    Posts:
    2
    Hey there!
    Whlie trying to implement a AssetReferenceUIRestriction to limit the dropdown for addressable asset references, I've realized that the already present AssetReferenceUILabelRestriction attribute does not actually do anything.

    I'm using it like this:

    Code (CSharp):
    1.  
    2. [AssetReferenceUILabelRestriction(new[] {"test"})]
    3. public AssetReferenceGameObject Template;
    4.  
    By looking at the source code, it turns out why:

    Code (CSharp):
    1.    /// <summary>
    2.     /// Used to restrict an AssetReference field or property to only allow items wil specific labels.  This is only enforced through the UI.
    3.     /// </summary>
    4.     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    5.     public sealed class AssetReferenceUILabelRestriction : AssetReferenceUIRestriction
    6.     {
    7.         /// <summary>
    8.         /// Stores the labels allowed for the AssetReference.
    9.         /// </summary>
    10.         public string[] m_AllowedLabels;
    11.  
    12.         /// <summary>
    13.         /// Stores the allowed labels formatted as a string.
    14.         /// </summary>
    15.         public string m_CachedToString;
    16.  
    17.         /// <summary>
    18.         /// Creates a new AssetReferenceUILabelRestriction object.
    19.         /// </summary>
    20.         /// <param name="allowedLabels">The labels allowed for the AssetReference.</param>
    21.         public AssetReferenceUILabelRestriction(params string[] allowedLabels)
    22.         {
    23.             m_AllowedLabels = allowedLabels;
    24.         }
    25.  
    26.         /// <inheritdoc/>
    27.         public override bool ValidateAsset(Object obj)
    28.         {
    29.             return true;
    30.         }
    31.  
    32.         /// <inheritdoc/>
    33.         public override bool ValidateAsset(string path)
    34.         {
    35.             return true;
    36.         }
    37. }
    This is 2021.2.11f1 with com.unity.addressables@1.19.18.

    ValidateAsset does just return true. I'm implementing my own version of this right now and it works, so looks like someone just forgot to actually implement this?

    Cheers!
     
  2. unity_shane

    unity_shane

    Unity Technologies

    Joined:
    Jun 3, 2021
    Posts:
    102
    @jverbeek_ka Oops. Yeah this was added a long time ago and I guess was just not very commonly used so we never even realized that it didn't work properly. Implementing your own is perfectly fine - I'll make a ticket for us to fix this so that it actually works. Thanks for pointing this out!
     
  3. jverbeek_ka

    jverbeek_ka

    Joined:
    Mar 7, 2022
    Posts:
    2
    @unity_shane Hey, thanks for the reply! After a fair bit amount of investigation, I found out that this is indeed wanted behaviour, as actual behaviour is implemented in socalled surrogates, so the actual logic is happening in the editor module only, and the game module side of things does only implement the "skeleton" of the attribute.

    I DO have feedback though, it's unfortunate that

    Code (CSharp):
    1.  
    2. internal virtual bool ValidateAsset(IReferenceEntryData entryData)
    3. {
    4.        return data.ValidateAsset(entryData?.AssetPath);
    5. }
    6.  
    in class AssetReferenceUIRestrictionSurrogate is marked internal, as we do have no chance of creating our custom UI restriction classes without involving workarounds (which I did for now). I get that it's meant to prevent people from implementing this in their game module as it references UnityEditor for the IReferenceEntryData, but it also does prevent us from doing exactly what it's meant for - so please consider adding a warning instead and making it public.