- Home /
A few GUI related questions.
Hello, I am trying to make a grid of 4 by 4 buttons and have successfully made them appear within my game, however, I have no idea how to call other functions when they are pressed. I am using an Array to create the text images obviously, but I am having trouble figuring out how to call each one.
On a different not, if I wanted this grid of buttons to be within a window, what exactly would I need to do? Because currently I cannot stop the window from covering the grid.
Also, I would love it if the buttons in the grid would cause a function to run while they are being hovered over by the players mouse.
If anyone has any suggestions I'd really appreciate it!
Basically using this setup:
var Skin2 : GUIStyle; var GridInt : int = 0; var GridImage : Texture[];
function OnGUI(){ GridInt = GUI.SelectionGrid (Rect (200, 100, 400, 400), GridInt, GridImage, 4, Skin2); }
I cut out the rest of the script, so it wasn't overly cluttered
Thank ~Craze
Eck, I found the answer to the figuring out which button is clicked, but still haven't figured out how to make it like the window.. :/ lame
Answer by Leviathan · Feb 18, 2011 at 01:40 PM
As you have probably figured out by now, your variable "GridInt" always shows the current selection of the grid.
What do you mean by the window "covering" the grid? Is the window on top of the grid instead of underneath?
What do you want to achieve if the user hovers over a button of yours? If you just want to change the color or texture of the button or its text, you best use the parameters for onHover in your variable for the GUIStyle in the inspector.
Another possiblity is to check in the update function, if the mouse is over a specific button in your grid.
To do this you can check the mousePosition with Input.mousePosition and check for each of the Rects of your buttons if it the position of the mouse is in the Rect. See here for the Scripting Reference: http://unity3d.com/support/documentation/ScriptReference/Rect.Contains.html .
There might be a better way to achieve this, but it is the first solution that came to mind for me.
I if I create a window that I want the grid of buttons to exist within, the window prioritizes it self over the grid and covers it. Unfortunately, I have no found a single way to by pass this unfortunate error, $$anonymous$$aybe I am just missing something. Also, thanks for that link on the Rect.Contains tip.
I had a similar problem with one GUI Element covering another one. In my case, I just gathered all calls to GUI.x in one Script. The order in which you use the methods is the order the GUI elements are built with. So you just have to build your window first in that script and build the SelectionGrid afterwards. Alternatively, you can use GUI.depth, to declare a depth level for your GUI elements. I haven't tried that out yet, but someone mentioned it as an answer to a similar question of $$anonymous$$e. See the reference: http://unity3d.com/support/documentation/ScriptReference/GUI-depth.html