1
votes

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 :)

1
so do you need something like OnClick event of UI.Button objects in the editor? (But on your custom gameobject)dreamend
OnClick is a UnityEvent type object. You can just use that and do the invokation wherever you want it to fire (on collision? then call it in OnCollission).Draco18s no longer trusts SE
@dreamend Yes. You are correct. I want to add my custom editor tool for events like onClick or onCollision or onTrigger on gameobjects. Similar to what Unity already provides by default for UI.Button objects(for clicks)rsinha
@Draco18s I didn't get it. How can I get this integrated through editor scripts?rsinha
@rsinha See dreamend's answer. Its just a UnityEvent object.Draco18s no longer trusts SE

1 Answers

2
votes

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();
    }

}