- Home /
Find position of SelectionGrid element clicked
How do you find the screenspace position of a SelectionGrid element that is clicked?
I would like to know the answer to this question too :/ any hits yet?
Answer by ckfinite · Jun 20, 2011 at 04:01 PM
It's pretty simple. You just feed the int you get back from the function into the string array you give to the function, like this:
var selGridInt : int = 0;
var selStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];
function OnGUI()
{
selGridInt = GUI.SelectionGrid (Rect (25, 25, 100, 100), selGridInt, selStrings, 2);
Debug.Log(selStrings[selGridInt]);
}
Sorry What I meant and what I think ina meant was the actual rect position that the selected button in the grid is at. Not the position of the grid itself or the index of of the selected option. Sorry for any confusion.
Well, you could divide the number of elements by the width of buttons to get the number of rows, then partition the original rect into that many rows. Then, if the partitioned rects are in the same order as the original setup, you can just feed the index into that. I will give sample code in a bit.
Ok, that's what I have happening now, I'm just going to be making a lot of the grids over the next few days and I was wondering if there was a nice built in way to do it that I hadn't found. I'll just keep my script nice and dynamic :P thanks for the help
Answer by BerggreenDK · Aug 02, 2011 at 10:17 PM
If you have a list of eg. buttons inside your grid, I would take the selection-value from the Grid and multiply it with the pixelheight of the button-style. This might be 25 pixels or something.
Would that work or are your content of different heights?