csharp Unity ExposedReference示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Unity ExposedReference示例相关的知识,希望对你有一定的参考价值。
## 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();
}
}
}
以上是关于csharp Unity ExposedReference示例的主要内容,如果未能解决你的问题,请参考以下文章
csharp unity / 2d draggable gameobject