// // Copyright (c) 2017 Anthony Marmont. All rights reserved. // Licensed for use under the Unity Asset Store EULA. See https://unity3d.com/legal/as_terms for full license information. // #if UNITY_EDITOR #pragma warning disable using AssetIcons.Editors.Internal; using AssetIcons.Editors.Internal.Drawing; using AssetIcons.Editors.Pipeline; using System; using UnityEditor; using UnityEngine; using UnityEngine.Serialization; namespace AssetIcons.Editors.Preferences { /// /// Used to reference assets inside the Unity project inside the editor. /// /// /// This is used inside the to select assets to render an icon. /// [Serializable] public sealed class PreferencesAssetReference { [FormerlySerializedAs("AssetPath")] [SerializeField] private string assetPath; [FormerlySerializedAs("AssetName")] [SerializeField] private string assetName; [NonSerialized] private string objectAssetPath; [NonSerialized] private string objectAssetName; [NonSerialized] private UnityEngine.Object objectReference; [NonSerialized] private IGraphicDrawer graphicDrawer; /// /// A for the asset referenced by this . /// public IGraphicDrawer GraphicDrawer { get { return graphicDrawer; } } /// /// A path, relative to the Unity project root, used to reference the asset. /// public string AssetPath { get { return assetPath; } set { assetPath = value; } } /// /// The of the asset. /// public string AssetName { get { return assetName; } set { assetName = value; } } /// /// Load or assign assets from the . /// public UnityEngine.Object ObjectReference { get { if (string.IsNullOrEmpty(AssetPath)) { return null; } if (objectReference == null || objectReference.Equals(null) || objectAssetPath != AssetPath || objectAssetName != AssetName) { objectAssetPath = AssetPath; objectAssetName = AssetName; objectReference = GetFromPath(AssetPath, AssetName); if (objectReference != null) { var objectReferenceType = objectReference.GetType(); graphicDrawer = AssetIconsMain.Pipeline.CreateGraphicDrawer(objectReferenceType); if (graphicDrawer != null) { graphicDrawer.SetValue(objectReference); } } else { graphicDrawer = null; } } return objectReference; } set { objectAssetPath = null; objectAssetName = null; objectReference = null; if (value != null || value.Equals(null)) { AssetPath = AssetDatabase.GetAssetPath(value); AssetName = value.name; } } } /// /// Loads an asset from the with the specified name. /// /// A path, relative to the Unity project root, used to reference the asset. /// The of the asset. /// /// This the same logic as an would use to locate an asset, used in editor scripting. /// public static UnityEngine.Object GetFromPath(string assetPath, string assetName) { if (assetPath.EndsWith(".unity")) { return null; } var references = AssetDatabase.LoadAllAssetsAtPath(assetPath); if (references.Length == 0) { return null; } foreach (var reference in references) { if (reference.name == assetName) { return reference; } } return references[0]; } } } #pragma warning restore #endif