Hi, guys. I am developing some small custom GUI library that will give ability to set up UI visually and will use event approach (functions like OnMouseDown, OnMouseUp, etc.). User can create a new button from prefab and then add script that will override mentioned functions. Everything is perfect except the fact that each button needs a separate script for it. This results in 10 script files for 10 buttons. I tried to use delegates but they are invisible in editor so there is no way to manually choose function for button event. So, maybe somebody has suggestions on it?
maybe you can just use one script for all the button
if(name == "GameStartButton") {
//do the gamestart related stuff here
}
add an if-loop to check which button is which…
The way I have done it in the past is to create a generic event handler that uses gameObject.SendMessage() to fire a user’s custom method. Then use a public string variable to allow the user to enter the name of the method they want to handle the event in the Inspector.
public class genericButton : MonoBehaviour
{
public string eventHandler; // gets set in inspector to method that will handle event
public void OnMouseDown()
{
gameObject.SendMessage(eventHandler);
}
}