- Home /
how to a make a touch button open a menu while finger is down?
Hey every body! i have a Touch Gui Question.
basicly i want to have 3 buttons on my screen. then when i hold my finger on one it opens another menu then with out taking my finger off i slide it to the option i want to select then release thus selecting the option. i've made a very rough idea of what im after below.
so im looking for someone to point me in the right direction of how to do this. an example would be great (i use JS) but i would be more than happy just to be told "hey look into menu.hold.GUIbutton " (i know thats not real but if i knew the real one this question would be shorter.)
thanks in advance for all your help guys! you are the best!
-Mike
Answer by robertbu · Jul 19, 2013 at 09:31 PM
With GUI you can use Rect.Contains():
#pragma strict
private var rectControl = Rect(0,50,100,50);
private var rectSlave1 = Rect(125,25,100,50);
private var rectSlave2 = Rect(125,100,100,50);
private var display = false;
function OnGUI() {
GUI.Box(rectControl, "master");
var e = Event.current;
if (e.type == EventType.MouseDown && rectControl.Contains(e.mousePosition))
display = true;
if (display) {
GUI.Box(rectSlave1, "slave1");
GUI.Box(rectSlave2, "slave2");
}
if (e.type == EventType.MouseUp) {
if (display) {
if (rectSlave1.Contains(e.mousePosition)) {
Debug.Log("Selected button 1");
}
else if (rectSlave2.Contains(e.mousePosition)) {
Debug.Log("Selected button 2");
}
}
display = false;
}
}
You can replace the GUI.Box() calls with GUI.DrawTexture() to get the textures like you have in your drawing.
this works great for my computer but how would i set this up with a touch based controls? could i somehow use touchPhase.began and touchPhase.End?
I don't use GUI for my interface work. I use EZGUI. So my understanding of GUI is not as strong as I would like. According to what I've read, you should be able to execute the above code on a touch screen without change. Unity will convert touch to the mouse events for GUI. If I had my $$anonymous$$ac here, I'd test it to be sure. Some of the posts indicated that GUI was processor intensive for touch screens, but those posts were old, so I don't know if that is still true.
If for some reason you end up using the Touch interface, be aware it returns Screen coordinates and GUI is in GUI coordinates.
this code worked on my unity remote android tablet. thansk so much!
so i tried to add textures to this and for the life of me i cant get it to work. i've replaced all the BUI.box wit GUI.Textures, i also changed the rectCont (and the others) to a texture(something i read online that i needed to do if i dont i get the error No appropriate version of 'UnityEngine.GUI.DrawTexture' for the argument list '(UnityEngine.Rect, String)' was found.) once i did that tho i get 'Contains' is not a member of 'UnityEngine.Texture'.
Here is the above script changed to use textures. I just change the GUI.Box() calls to GUI.DrawTexture() calls. You need to drag and drop your textures onto the three variables in the inspector. It works fine in the editor:
#pragma strict
var tex$$anonymous$$aster : Texture;
var texSlave1 : Texture;
var texSlave2 : Texture;
private var rectControl = Rect(0,50,100,50);
private var rectSlave1 = Rect(125,25,100,50);
private var rectSlave2 = Rect(125,100,100,50);
private var display = false;
function OnGUI() {
GUI.DrawTexture(rectControl, tex$$anonymous$$aster);
var e = Event.current;
if (e.type == EventType.$$anonymous$$ouseDown && rectControl.Contains(e.mousePosition))
display = true;
if (display) {
GUI.DrawTexture(rectSlave1, texSlave1);
GUI.DrawTexture(rectSlave2, texSlave2);
}
if (e.type == EventType.$$anonymous$$ouseUp) {
if (display) {
if (rectSlave1.Contains(e.mousePosition)) {
Debug.Log("Selected button 1");
}
else if (rectSlave2.Contains(e.mousePosition)) {
Debug.Log("Selected button 2");
}
}
display = false;
}
}
Your answer
Follow this Question
Related Questions
GUI texture touch input problem 1 Answer
How would I implement a GUI texture that acts as a slider? 0 Answers
Look Stick for FPS Games on Mobile Platforms 1 Answer
Mouse and Oculus Touch Controller Inputs 0 Answers
How do I get a GUI Texture Button to act as a Input Key such as a T button on a key bored or so? 1 Answer