- Home /
Button OnClick inspector - how do I do this?
The Unity 4.6 UI Button has a neat OnClick inspector where you can specify a GameObject, a Script, and a Function to call along with parameters - whether they're primitive types you can place in, or MonoBehaviour references. You can assign Properties too.
This is absolutely incredible.
Does anybody know how I can replicate such a thing in the inspector? Is there a way to "borrow" this functionality? Or is the source code for it available somewhere so it may be replicated?
Thank you so much for any help or response.
Not sure about source code. But when defining an RPC you have to type in the functions name with a string. Go check that out maybe. Sorry I couldn't be anymore of a help.
Answer by emayberry · Mar 17, 2015 at 03:54 PM
Hopefully you've found your answer, but for anybody else looking around this is actually very simple to do. The inspector is capable of displaying any event that inherits from UnityEvent, so you can either add an existing event type or make your own as follows:
using UnityEngine;
using UnityEngine.Events;
public class EventTest : MonoBehaviour {
[Serializable]
public class MyEventType : UnityEvent { }
public MyEventType OnEvent;
}
cool it works great! now how exactly do i use the OnEvent variable and make it trigger all the functions thats added inside it?
To call any of the events you need to call the Invoke() method of the UnityEvent.`OnEvent.Invoke();`
Answer by FezAstron · Feb 03, 2015 at 03:07 AM
Perhaps you're thinking about Event Trigger that you can add as a component. It has all the features an Interactive UI would need like PointerEnter(aka MouseEnter) or PointerUp(aka MouseUp). Just add it through the inspector by searching for "Event System". Now I may be thinking something completely different than what you're asking so correct me if I'm wrong. =)
Answer by tcoursow · Dec 13, 2016 at 04:41 PM
You could also extend from the Unity Button class and only use your button type in your project like this:
[AddComponentMenu("UI/UI Button", 30)]
public class UIButton : UnityEngine.UI.Button
{
public void Click()
{
onClick.Invoke();
}
}
Now "Click" should show up in the inspector
Your answer
![](https://koobas.hobune.stream/wayback/20220613182259im_/https://answers.unity.com/themes/thub/images/avi.jpg)