using System; using System.Text; using Object = UnityEngine.Object; namespace UnityEngine { /// /// Used to restrict an AssetReference field or property to only allow items wil specific labels. This is only enforced through the UI. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] public class AssetReferenceUIRestriction : Attribute { /// /// Validates that the referenced asset allowable for this asset reference. /// /// The Object to validate. /// Whether the referenced asset is valid. public virtual bool ValidateAsset(Object obj) { return true; } /// /// Validates that the referenced asset allowable for this asset reference. /// /// The path to the asset in question. /// Whether the referenced asset is valid. public virtual bool ValidateAsset(string path) { return true; } } /// /// Used to restrict an AssetReference field or property to only allow items wil specific labels. This is only enforced through the UI. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] public sealed class AssetReferenceUILabelRestriction : AssetReferenceUIRestriction { /// /// Stores the labels allowed for the AssetReference. /// public string[] m_AllowedLabels; /// /// Stores the allowed labels formatted as a string. /// public string m_CachedToString; /// /// Creates a new AssetReferenceUILabelRestriction object. /// /// The labels allowed for the AssetReference. public AssetReferenceUILabelRestriction(params string[] allowedLabels) { m_AllowedLabels = allowedLabels; } /// public override bool ValidateAsset(Object obj) { return true; } /// public override bool ValidateAsset(string path) { return true; } /// /// Converts the information about the allowed labels to a formatted string. /// /// Returns information about the allowed labels as a string. public override string ToString() { if (m_CachedToString == null) { StringBuilder sb = new StringBuilder(); bool first = true; foreach (var t in m_AllowedLabels) { if (!first) sb.Append(','); first = false; sb.Append(t); } m_CachedToString = sb.ToString(); } return m_CachedToString; } } }