Trying to plan out some courswork that’s entirely made of UI, no 3D shizzle going on. Im using unity to try and learn its GUI functionality in preperation for a project in a few months. I do realise its not ideal for this.
I need/want a button that i can left or right click on to increase or decrease a value and the issue is the right click since it only returns true/false.
I have read Right-click button using GUI class? - Questions & Answers - Unity Discussions
but thats 3 years old and one of the things they do is save the button as a variable for later reference. That is now impossible because GUI.Button is a function not a type.
The answer says to use a GUILayout but the overload they use on the .Button call don’t exist anymore.
I have tried searching the unityGUI forums but it crashes everytime i hit search no matter what I search for. I even tried boobies.
So, im left with a question but no answer.
How, with Unity 4’s GUI do I get a button that I can say if they right click button x do this if they left click button x do that?
Thank You All,
Hope there is a solution cause I got 3 courseworks to do and all are using unity and need UI.
P.S. Whats up with the forum search crashes?
You can use after this GUILayoutUtility.GetLastRect() to get the Rect of your Button.
After this, you just have to check your input positions to check if this rect contains your mouse.
Here is the code
public void OnGUI() {
Rect myRect;
// If you use layout
GUILayout.Button("MyButton");
myRect = GUILayoutUtility.GetLastRect();
// If you use GUI
myRect = new Rect(x,y,width,height)
GUI.Button(myRect, "MyButton");
// Check Left Click
if (Input.GetMouseButtonDown(0) myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
//Actions on Left Click
}
// Check Right Click
if (Input.GetMouseButtonDown(1) myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
//Actions on Right Click
}
}
Code not tested and for Unity 3. I don’t know if it works on Unity 4.
Took me a sec (okay, like 10 minutes) to understand what you were doing, but now I c it. Yeah that should do it. The overload you used in GUILayout.Button(“MyButton”); does not exist anymore. they now want you to put butt loads of other variables in as well and i dont know what half of them do.
However, using GUI works exactly the same way. So yes your code works in unity 4 but only the GUI bit, not he GUILayout part.
Figured I should try it before posting. It works. Cheers, Made my day.
Evertime I increment a run of the mill int using Avalion’s method, (on click) it gets called twice.
I did some more checking and found out that this is being called once on the frame it gets clicked, and again the frame after that. I counted frames and outputted the number of the frame on each click, each click got two outputs instead of one.
Ideas?
Did some more testing. Turns out this does not happen if I run the int++ code in Update() rather than OnGUI()
So, can someone tell me why I have to split my code up like that? I dont understand why
if (Input.GetMouseButtonDown(1) myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
I pretty much solved all my issues, but give me a few days to play with eDriven and I might have to hug you.
If this works in unity 4 I might just cry (watching tutorials as I type)
Unity 4 is a must for me because the boss will want us to us its pathfinding and stuff to cut dev time.
P.S. Are you the guy that actually made it?
OnGUI gets called every time it needs to handle an event, which can (and often does) happen more than once per frame. So if you need to have your code sync with the frame count, Update or LateUpdate is the place