- Home /
Main Menu GUI Button - Animation?
Hey fellow Uniters,
So I've got a predicament here that many others seem to have; however, I've tested every solution from each search result to no avail! I've racked my brain/the internet for days as to a possible solution, but have none - hence my question here.
I've got a basic working Main Menu. I created a string array for the menu options, assign names to the options in the inspector, and then create a for (each item in the string array) - loop to create buttons. Looks alright. I've even got a custom GUIskin assigned that changes text color/texture of the focused option, so I can tell which menu option I'm currently on. (I opted for the "For loop" for my buttons as opposed to a "Selection Grid," as the latter of the two is far less customizable). This is all navigable by keyboard - I can key up/down and pick an option and I'd like to keep it that way. I have the GUI.SetNextControlName within the for loop, and a GUI.FocusControl after my selection code, although I'm not really sure they do anything or how they work. But here's where my real problem lies - animation.
Question 1 How can I access the focused menu option through scripting in order to animate it (I'd be using iTween, because...well, if you haven't used it yet, use it!)? I've seen a few people put their button specifications within an "If" statement; however, that appears to only be useful for single button creation. I haven't seen it done in a "for - loop" button creation system. I'd simply like to access the menu item that has keyboard focus and scale it to be just a tad bigger, but GUIskin only offers parameters for texture and text.
Question 2 Once I've established a working animation for each icon, how can I completely eliminate the mouse functions? I would like the mouse to NOT affect the menu whatsoever. That means the GUIskin does not react to mouse hovers or clicks. Since the GUIskin does not appear to have a disable function for those, should I eliminate the GUIskin altogether and assign textures via scripting? If I do that, how could I access the highlighted option (focused, but not yet pressed/activated) in order to change texture, text color, and animate?
Question 3 Submenus. I'm not sure where to start with this, so I'll more than likely make another post. But while we're on the topic - what's the best way of creating a submenu? The only way I've seen it done is enumeration. I have not yet tried anything yet, but I really don't want to dive into it and become even more irritated with Unity's current GUI system. But it's nice to know in advance for planning purposes :)
Thank you in advance!
-Clopay
Answer by Davidovich · Jan 04, 2013 at 09:52 PM
Question 1
You can use GUIUtility.keyboardControl to get the ID of the focussed element, then compare that against the control ID.
Example code in C# (Unityscript version is almost identical)
GUIContent content = new GUIContent("I'm a button!");
int controlId = GUIUtility.GetControlID(content, FocusType.Keyboard);
if (GUIUtility.keyboardControl == controlId) {
Debug.Log("It's got focus - " + controlId);
}
if (GUI.Button(new Rect(10,10,100,30), content)) {
Debug.Log("Clicked the control button");
}
Question 2 For this you can check GUIUtility.hotControl. This will be 0 if nothing has been clicked, or the ID of the element that was clicked
Example in C#
bool guiClick = (GUIUtility.hotControl != 0);
if (GUI.Button(new Rect(10,10,100,30), content) && !guiClick) {
Debug.Log("Pressed the control button");
}
Question 3
The best way I've seen of doing submenus is by using delegates. This is much cleaner than great big switch statements and the like. The first thing that turned up was this: http://forum.unity3d.com/threads/22252-Friday-Treat-GUI-Menu-System-(C-)
There are also a bunch of other resources that I can't find at the moment :(
The implementation of delegates is a bit different between C# and Unityscript, but there was a post on implementing a Unityscript version here: http://answers.unity3d.com/questions/50164/how-can-i-use-function-pointerdelegate-in-pragma-s.html
Hope that helps some!
Your answer
Follow this Question
Related Questions
On Screen Button Controls 0 Answers
Main menu help needed (C#) 2 Answers
Why do my GUI.button Background Colors Not work? 1 Answer
2 Position GUI Texture Rocker Switch 0 Answers
GUI Buttons not Being Displayed 1 Answer