Search code examples
unity-game-engineunity-editor

Creating similar tool like Game Creator | Unity Editor Customization


The requirement for the project is to create a tool with which I can define simple onClick, onTrigger or onCollision actions on the gameobjects directly through editor scripting.

Unity already provides something on a button for onClick, in which I can select the gameobject, that gives me access to all the scripts and its function on that gameobject and then I simply select a function that gets executed on runtime.

What I need -
OnCollisionTrigger -> It shows in editor two fields
1. Gameobject field - Where I can simply drag any scene gameobject
2. Function list like Toggle Active, Destroy and so on

I have looked at many videos available for editor scripting but none could explain these topics.

I know we have handles in Unity Editor that is really handy for doing quite a lot of stuff. But I am really clueless about how I can implement something like this on editor.

I am not sure if this problem can be done in multiple ways. But I am not getting the starting point where I need to start researching on. Unity Editor scripts are really less documented in videos or in docs. Any help to direct me in the right direction will really help me.

Thanks a lot in advance :)


Solution

  • What you need is UnityEvents. When you create a UI.Button object, it has a UnityEvent named OnClick.

    In the below example, I created an example class, which triggers OnSomethingHappened event (you can name it OnClick or anything else) whenever the SomeFunction called. Because I 'invoked' the event.

    If you drag and drop it to a gameobject you will see OnSomethingHappened event as OnClick event of UI.Button's.

    using UnityEngine;
    using UnityEngine.Events;
    
    public class Example : MonoBehaviour
    {
        public UnityEvent OnSomethingHappened;
    
        private void SomeFunction() {
            OnSomethingHappened.Invoke();
        }
    
    }