Search Unity

How to place GUI elements relative into the screen

Discussion in 'Immediate Mode GUI (IMGUI)' started by polyphobia3d, Nov 11, 2010.

  1. polyphobia3d

    polyphobia3d

    Joined:
    Nov 7, 2010
    Posts:
    13
    Hey guys,

    is it possible to place gui elements such as buttons, boxes or whatever relatively to another element and not absolutely to the screen?

    if i use GUI.Toggle(Rect(25, 25,50,50), "text"), it of cours generates a toggle 25 pixels from 0 with a size of 50x50pixels, but what i would like to do is place it inside a box, centered in that box and what i sure don't want to do, is change that accordingly everytime i change the size of the box.

    same goes to any element that is aligned to anything but the left side of the screen, how do i place something X and Y pixels from the bottom right edge of the screen?

    Is that understandable? :oops:
     
  2. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    Screen.width and Screen.height are your friends.
     
  3. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    A quick example: How to center a toggle on the screen:

    Code (csharp):
    1. GUI.Toggle(new Rect(Screen.width * 0.5) - 25, (Screen.height * 0.5) - 25, 50, 50);