x


[Closed] 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?

more ▼

asked Oct 14 '11 at 05:59 PM

Dávid Debnár gravatar image

Dávid Debnár
730 42 47 54

Do you mean you want all buttons to be center-aligned when stacked vertically?

Oct 14 '11 at 07:23 PM DaveA

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.

Oct 14 '11 at 08:49 PM Dávid Debnár
(comments are locked)
10|3000 characters needed characters left

The question has been closed Oct 16 '11 at 11:32 AM by Dávid Debnár for the following reason:

The question is answered, right answer was accepted


1 answer: sort voted first

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...

more ▼

answered Oct 14 '11 at 09:57 PM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3669
x386
x384
x41
x1

asked: Oct 14 '11 at 05:59 PM

Seen: 695 times

Last Updated: Oct 14 '11 at 10:16 PM