frideal
2/5/2018 - 8:48 AM

Unity ExposedReference Example

Unity ExposedReference Example


## Unity Exposed Reference Example

--------------------------------------MoveTarget.cs
namespace ScriptableObjectExample
{
    public class MoveTarget : MonoBehaviour
    {

    }
}

--------------------------------------TimeLineClip.cs
namespace ScriptableObjectExample
{
    public class TimeLineClip : ScriptableObject
    {
        public string clipName;
        public float startTime;
        public float duration;
    }
}

--------------------------------------PlayAnimationClip.cs
namespace ScriptableObjectExample
{
    [CreateAssetMenuAttribute(fileName = "New Play Animation Clip", menuName = "Example/PlayAnimationClip")]
    public class PlayAnimationClip : TimeLineClip
    {
        public AnimationClip clip;
        public ExposedReference<GameObject> target;
        public ExposedReference<MoveTarget> moveTarget;

        private void OnEnable()
        {
        }
    }
}


--------------------------------------TimeLineManager.cs
namespace ScriptableObjectExample
{
    public class TimeLineManager : MonoBehaviour, IExposedPropertyTable
    {
        public PlayAnimationClip playAniClip;

        public List<PropertyName> listPropertyName;
        public List<UnityEngine.Object> listReference;

        public void ClearReferenceValue(PropertyName id)
        {
            int index = listPropertyName.IndexOf(id);
            if (index != -1)
            {
                listReference.RemoveAt(index);
                listPropertyName.RemoveAt(index);
            }
        }

        public Object GetReferenceValue(PropertyName id, out bool idValid)
        {
            int index = listPropertyName.IndexOf(id);
            if (index != -1)
            {
                idValid = true;
                return listReference[index];
            }
            idValid = false;
            return null;
        }
        public void SetReferenceValue(PropertyName id, Object value)
        {
            int index = listPropertyName.IndexOf(id);
            if (index != -1)
            {
                listReference[index] = value;
            }
            else
            {
                listPropertyName.Add(id);
                listReference.Add(value);
            }
        }
    }
}


-------------------------------------- Asssets/Editor/TimeLineManagerInspector.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace ScriptableObjectExample
{
    [CustomEditor(typeof(TimeLineManager))]
    public class TimeLineManagerInspector : Editor
    {
        private SerializedProperty playAniClip;

        private SerializedObject assetSo;
        private void OnEnable()
        {
            playAniClip = serializedObject.FindProperty("playAniClip");
            SetAssetSo();
        }

        private void SetAssetSo()
        {
            if (playAniClip.objectReferenceValue != null)
            {
                assetSo = new SerializedObject(playAniClip.objectReferenceValue, serializedObject.targetObject);
                Debug.Log("Create new SerializedObject");
            }
            else
            {
                assetSo = null;
            }
        }

        public override void OnInspectorGUI()
        {
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(playAniClip, new GUIContent("Target Asset"));

            if (EditorGUI.EndChangeCheck())
            {
                SetAssetSo();
            }

            if (assetSo != null)
            {
                SerializedProperty sp = assetSo.GetIterator();
                bool enterChild = true;
                while (sp.NextVisible(enterChild))
                {
                    enterChild = false;
                    EditorGUILayout.PropertyField(sp, true);
                }
                assetSo.ApplyModifiedProperties();
            }
            serializedObject.ApplyModifiedProperties();
        }
    }
}