Skip to content

Instantly share code, notes, and snippets.

@frideal
Last active October 31, 2023 04:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save frideal/a55961c696acea49d54600c9f66c13ff to your computer and use it in GitHub Desktop.
Save frideal/a55961c696acea49d54600c9f66c13ff to your computer and use it in GitHub Desktop.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment