2
\$\begingroup\$

I am using the Addressables package of Unity.

I am trying to restrict an AssetReference type in the Unity inspector to a specific type. Currently a user can assign any Unity object to an AssetReference, but I would like to guide the user to assign the correct types to the correct variables and (if possible) filter the assets type list to include only the assignable types when the user click on the drop-down arrow

According to the Unity documentation https://docs.unity3d.com/Packages/[email protected]/api/UnityEngine.ResourceManagement.Util.SerializedTypeRestrictionAttribute.html

it seems like there is some sort of annotation available. The documentation is very limited and I have tried:

[UnityEngine.ResourceManagement.Util.SerializedTypeRestrictionAttribute(type=typeof(PlayerMaster))]
public UnityEngine.AddressableAssets.AssetReference playerMaster;

but it made no difference.

I also tried to create my own AssetReference type:

    [Serializable]
    public class Reference : UnityEngine.AddressableAssets.AssetReferenceT<PlayerMaster>, UnityEngine.AddressableAssets.IKeyEvaluator
    {
        public Reference(string guid) : base(guid)
        {
        }
    }

but with this new type I cannot select anything!

The following code snippet at least seems to restrict the list to GameObjects which is an improvement, but the user can still assign the wrong GameObjects easily. I was hoping for a more strict filter, but if it is not possible I will settle with:

public UnityEngine.AddressableAssets.AssetReferenceGameObject playerMaster;
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I know this question is old but it came up as the first result.

In the current version (1.19), the right approach is to create your own type using generics as you mentioned.

    [System.Serializable]
    public class AssetReferencePlayerMaster : AssetReferenceT<PlayerMaster> 
    {
        public AssetReferencePlayerMaster(string guid) : base(guid) { }
    }

This seems to work as it only allows you to assign objects of the specified type. It definitely works for me with ScriptableObjects.

The reason you're not seeing any assets to pick up is probably because they are not marked as addressables yet.

Hope that helps somebody.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .