Hey, is there already a solution for using an AssetReference bound to a Component type? I want to be able to reference an AddressableAsset by a Component and only show those Components in the AssetReference popup. I've come up with this which seems to work ok. Just wondering if something else already exists? Code (CSharp): [Serializable] public class AssetReferenceCustomComponent : AssetReferenceComponent<CustomComponent> { public AssetReferenceCustomComponent(string guid) : base(guid) { } } public abstract class AssetReferenceComponent<TComponent> : AssetReferenceT<TComponent> { public AssetReferenceComponent(string guid) : base(guid) { } public override bool ValidateAsset(Object obj) { var type = obj.GetType(); if (typeof(TComponent).IsAssignableFrom(type)) { return true; } GameObject gameObject = (obj as GameObject); return gameObject ? gameObject.GetComponent<TComponent>() != null : false; } public override bool ValidateAsset(string path) { #if UNITY_EDITOR var type = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(path); if (typeof(TComponent).IsAssignableFrom(type)) { return true; } GameObject gameObject = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path); return gameObject ? gameObject.GetComponent<TComponent>() != null : false; #else return false; #endif } }
We actually added it to the samples repo about 2 weeks ago, but haven't publicized it yet because it isn't finished. There's some clean up we're mostly done with that should be up this week. https://github.com/Unity-Technologies/Addressables-Sample/tree/master/Basic/ComponentReference