- Home /
show delegate in Inspector
RotationDrag.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public delegate void OnPageChanged(int curPage);
public class RotationDrag : MonoBehaviour
{
public GameObject m_Template;
public OnPageChanged m_OnPageChanged;
}
RotationDragEditor.cs
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(RotationDrag))]
[CanEditMultipleObjects]
public class RotationDragEditor : Editor {
public override void OnInspectorGUI()
{
RotationDrag tScript = (RotationDrag)target;
tScript.m_Template = EditorGUILayout.ObjectField("Template", tScript.m_Template, tScript.m_Template.GetType(), true) as GameObject;
//here how to show m_OnPageChanged like the button's Onclick of ugui
}
}
this is my code.
RotationDrag.cs has much member. It's have lots work way by different member value.
so i write a RotationDragEditor.cs to control the member's name in Inspector to show how this value work.
Now, i'd like a typesetting like the button's OnClick event of ugui.
How can i do?
thx.
Answer by saucerman · Jan 06, 2017 at 04:47 AM
ok.
no one.
finally, i solved this problem.
DelegateTest.cs using UnityEngine; using UnityEngine.Events;
public class DelegateTest : MonoBehaviour
{
public UnityEvent m_OnPageChanged = new UnityEvent();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
DelegateTestEditor.cs
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(DelegateTest))]
[CanEditMultipleObjects]
public class DelegateTestEditor : Editor
{
SerializedProperty m_OnClickProperty;
void OnEnable()
{
m_OnClickProperty = serializedObject.FindProperty("m_OnPageChanged");
}
public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(m_OnClickProperty);
serializedObject.ApplyModifiedProperties();
}
}
Hello, the approach you've find can be done without custom editor.
By just not using your custom editor. The public UnityEvent
field you've got is already recognized by the inspector.
Btw it's common practice to define your own event classes that derive from UnityEvent, helps with type checking and semantics. UnityEvent accept some template params for parameter types, so like
[Serializable] public class MyEvent : UnityEvent<string,int> { }
if you want to assign delegates that take a string and int parameter.
Check out https://docs.unity3d.com/ScriptReference/Events.UnityEvent_2.html for example.
If you prefer, you can simplify even more btw by getting rid of your "= new UnityEvent()" initializer and instead using null propagation (ok on events) e.g. "onClick?.Invoke(params)".
Your answer
