- Home /
NGUI Button Click Callback from external script.
I simply do not have the option to check if the button is clicked, was clicked, or being clicking using OnClick function within a MonoBehaviour attached to a button, instead I need something like buttonComponent button = GameObject.Find("button").GetComponent();
if(button.pressed) { BUTTON WAS PRESSED... CLICKED.. SAME THIng }
maybe those are not the right class names for NGUI but I never actually used it, just looking online for tutorials before I purchase it are not telling me if thats possible or not, and I simply dont wanna inest $90 for a GUI Tool that wont even do something as simple as that.. :{
I found a tutorial that lets me do something like button.text = "... NEW TEXT";
so surely it has some kind of boolean to check if its pressed or being clicked
Answer by whydoidoit · Jul 14, 2012 at 11:03 AM
You would just write your own handler for that - I do something similar for events that are fired when a button is pressed (which is not normal NGUI either).
var isPressed = false;
function OnPress(pressed : boolean) {
isPressed = pressed;
}
Or in my case (C#):
public event Action Clicked = delegate {};
void OnClick() {
Clicked();
}
i have so many buttons. so please explain how to differntiate functions.
You know there's a free version of NGUI you can try? And NGUI support is best asked on the NGUI forum.
You use UIButton$$anonymous$$essage scripts to send different Send$$anonymous$$essage calls, or you attach an event script like the one I put above (in C#) and do
button1.GetComponent< SomeEventScriptYouWrote>().Clicked += SomeHandler;
Hi @whydoidoit,
I'm trying to use your approach to deal with a OnSelectionChange of PopUplist from NGUI but Unity tells me:
The type or namespace name `Action' could not be found
Any idea of why is this jappening?
You need to declare it as System.Action or add a using System; to the top of the file.
Answer by mrekuc · Aug 08, 2014 at 04:31 PM
If you attach the same script to all your buttons then just do something like this..
void OnClick()
{
string button = gameObject.name;
switch(button)
{
case "Button Name":
if(Debug.isDebugBuild) Debug.Log("My Button was Clicked");
break;
}
}
Create a new case for each button you haves name and it will trigger each independently.
Your answer
Follow this Question
Related Questions
Best way to load a level depending on which game object is clicked 1 Answer
OnMouseUp interfering with NGUI OnClick Button 1 Answer
Button.onClick.AddListener(() => Attack()); isn't running function math correctly 1 Answer
onclick.addlistener only works once 0 Answers
My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer