- Home /
How to add UnityEvent using Custom Inspector?
Hi, I'm new to custom inspectors so please be patient. Let's say my code looks like this:
public class ExampleClass : MonoBehaviour
{
public UnityEvent myEvent;
}
[CustomEditor(typeof(ExampleClass))]
public class EventEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Add event"))
{
...
}
}
}
So I have 1 UnityEvent and what I'm trying to do is add another one when I press a button in my custom inspector. I've been googling for hours now and I just can't get it to work.
I've tried something like this:
[CustomEditor(typeof(ExampleClass))]
public class EventEditor : Editor
{
SerializedObject _example;
void OnEnable()
{
SerializedObject _example= new SerializedObject((ExampleClass)target);
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Add event"))
{
SerializedProperty sprop = _example.FindProperty("myEvent");
EditorGUILayout.PropertyField(sprop);
_example.ApplyModifiedProperties();
}
}
}
..but no luck :(
Help!
But what kind of event you would like to use? and what is the final aim? if it is not a top secret project, please give us the context so we can try to think of the best solution for your situation.
The final aim is for my game/lvl designer to be able to manipulate objects using unityevents and trigger them as he pleases using colliders. Now he does have one unity event which he uses pretty often but now we need more. So i wanted to give him the ability to create his own with just one button and then figure out how to add public methods which would invoke these events... but i'm stuck on step one :)
Answer by stepan-stulov · Aug 11, 2015 at 09:56 AM
Seems like someone solved it:
http://answers.unity3d.com/questions/802786/46-how-do-i-draw-unityevent-in-custom-inspector.html
Thank you for your reply but it doesn't do anything. Here is what my code looks right now:
[CustomEditor(typeof(ExampleClass))]
public class EventEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Add event"))
{
SerializedProperty sprop = serializedObject.FindProperty("myEvent");
EditorGUIUtility.LookLikeControls();
EditorGUILayout.PropertyField(sprop);
serializedObject.Apply$$anonymous$$odifiedProperties();
}
}
}
When I press the button, really nothing happens. Unless I put some debug.log just so I know the button works...
Let me simply describe what you are doing:
You are showing the event field in the inspector only in one frame (milliseconds long) when the button gets clicked. Then you make no modifications to anything whatsoever. Then you ask to apply these (which) modifications.
Are you trying to add a new event on button click? Then do that on the button click, but move the actually rendering of the event out. I assume what you want is to show the event all the time unconditionally, not just for 2 milliseconds in the frame. Here is some pseudo-code of what I mean:
if (GUILayout.Button("Add event"))
{
// add event to some list
}
foreach (UnityEvent unityEvent in unityEvents)
EditorGUILayout.PropertyField(sprop);
If that is the task then I have a much more simple solution for. Simply declare a serializable field whose type is list of events. Unity already has ui logic for manipulating the lists without any extra editor code:
[SerializeField] private List<UnityEvent> _events;