- Home /
The question is answered, right answer was accepted
Easy way to center GUI Textures on a path?
Hey guys! I am making a menu that will have a vertical bar, that will be smaller at the top and wider at the bottom, on the right and I want my GUI Textures to slide along it. BUT. I can make them slide, but I ran into a problem of how to center them at the path. Lets say I have 5 buttons, the middle will be centered and the 2 on the bottom and 2 on the top will just be above it. But what about 4 elements?
Do you mean you want all buttons to be center-aligned when stacked vertically?
Yeah, exactly. I have the moving script done. I just divide the vertical position and asign it to the horizontal one. But I just can't find a way of centering it. Or. I think I just found one while writing. I will just make a var offset and it will offset the whole group of menu buttons from the center of the screen.
Answer by jahroy · Oct 14, 2011 at 09:57 PM
You can calculate the vertical starting point like this:
- calculate the height of n buttons (number of buttons x button height)
- subtract height of all buttons from height of vertical bar
- divide this number by 2
Take the result and start your first button that many pixels from the top.
var sliderRectangle : Rect; var buttonRect : Rect; var buttonHeight : int; var buttonCount : int;
function OnGUI () { var totalButtonHeight : int = buttonCount * buttonHeight;
var startY = sliderRectangle.y;
startY += ( sliderRectangle.height - totalButtonHeight ) / 2;
for ( var i = 0; i < buttonCount; i ++ ) {
buttonRect.y = startY + ( i * buttonHeight );
if ( GUI.Button(buttonRect, "I am button " + i) ) {
print("button " + i + " has been pressed);
}
}
}
Not sure if this will work with your non-rectangle slider...