- Home /
NGUI UIButton Event question
Hi,
I have created 4 buttons which represent 4 direction arrows, Left, Right, Forward and Backwards.
I have a OnPress(bool isPress) function which checks which of the button were pressed and I send information about my x and y axis.
This is the function:
private static int x = 0;
private static int y = 0;
private string currentArrow = "none";
private Vector2 currentPos = Vector2.zero;
void OnPress(bool isDown)
{
Debug.Log("Called " + gameObject.name + " press state: "+isDown);
if (isDown)
{
switch (gameObject.name)
{
case "Up":
y = 1;
x = 0;
break;
case "Down":
y = -1;
x = 0;
break;
case "Right":
x = 1;
y = 0;
break;
case "Left":
x = -1;
y = 0;
break;
default:
Debug.Log("Error in name");
break;
}
currentArrow = gameObject.name;
currentPos = gameObject.transform.position;
}
else
{
x = 0;
y = 0;
}
SetValues(x, y);
}
This works just fine,
However, when my mouse is pressing a button, and I want it to move while it is pressed, to another collider, and activate it, I can't since I didn't release the last button.
Any idea on how to do it?
I've tried to send messages by myself to OnPress with a false parameter to do it manually, but it still doesn't work right..
I've also tried to use OnHover, but I work with an android device, so it's irrelevant...
So you want a sort of drag and drop functionality? Have you checked out NGUIs "Example X - Character" scene which has a drag an drop example?
Not exactly my intentions, I want to move my finger to these 4 arrows, without releasing my touch input, and that NGUI will accept it as an event as soon as I enter a collider.
I can implement this with Unity's regular GUI, but it's a bit frustrating...
can NGUI handle my request?
Oh, then I understand what you want. NGUI can do this. Check out my answer below.
Answer by GameVortex · Jan 15, 2014 at 09:07 AM
To enable this functionality, go to the UICamera and uncheck the value "Sticky Press". This enables press events for multiple GameObjects instead of just the original pressed one. It is explained better in the comments for the value in the UICamera script.
Great, thanks! it works now. One question, let's assume I want to Sticky function to work with other objects, what do I do? create two seperate cameras?
It depends. If the 4 arrow buttons are available at the same time as the other Objects then I think you need to use two cameras yes, but I would not recommend adding multiple cameras only for that.
If the 4 arrow are in its own menu/panel and not visible when the other buttons are visible then you can activate and deactivate Sticky Press through code when you change your panels/menus.
Also, for future support questions about NGUI I recommend the **NGUI forum** where the developer Aren$$anonymous$$ook is very active and helpful.
Your answer
Follow this Question
Related Questions
Android Button With Raycast 3 Answers
Android: Search button event 0 Answers
NGUI: Button don't detect input if parent change position on-screen? 1 Answer
how do i use GUI buttons with a android device(tablet) 2 Answers
Button Touch for Android 2 Answers