制作unity扩展编辑器如何优雅的传递自定义参数
在制作Unity扩展编辑器时,可以使用自定义参数来传递数据和信息,以便更好地控制和定制编辑器的行为。以下是一些优雅的传递自定义参数的方法:
- 使用自定义属性
可以创建一个自定义属性,将需要传递的参数作为属性值。然后在编辑器中使用该属性,从而可以轻松地传递参数。例如:
public class CustomEditor : Editor
{
[SerializeField] private int customParameter;
public override void OnInspectorGUI()
{
// 使用 customParameter 参数
}
}
- 使用 ScriptableObject
ScriptableObject 是一个可序列化的对象,可以在运行时创建和修改,并且可以在不同的场景和对象之间共享。可以创建一个自定义 ScriptableObject 类来存储需要传递的参数,然后在编辑器中使用该对象。例如:
[CreateAssetMenu(fileName = "CustomParameters", menuName = "Custom/Custom Parameters")]
public class CustomParameters : ScriptableObject
{
public int customParameter;
}
public class CustomEditor : Editor
{
private CustomParameters customParameters;
private void OnEnable()
{
customParameters = Resources.Load<CustomParameters>("CustomParameters");
}
public override void OnInspectorGUI()
{
// 使用 customParameters.customParameter 参数
}
}
- 使用 SerializedObject
SerializedObject 是一个包装器类,可以在运行时编辑 Unity 对象的序列化数据。可以创建一个 SerializedObject 对象来编辑需要传递的参数,并在编辑器中使用该对象。例如:
public class CustomEditor : Editor
{
private SerializedObject serializedObject;
private SerializedProperty customParameter;
private void OnEnable()
{
serializedObject = new SerializedObject(target);
customParameter = serializedObject.FindProperty("customParameter");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(customParameter);
serializedObject.ApplyModifiedProperties();
// 使用 customParameter.intValue 参数
}
}
无论哪种方法,都可以优雅地传递自定义参数,以实现更好的编辑器控制和定制。
原文地址: https://www.cveoy.top/t/topic/FpC 著作权归作者所有。请勿转载和采集!