Search Unity

GUI.Button(new Rect(a, b, c, d): Positioning when testing?

Discussion in 'Scripting' started by Flybye, Jul 31, 2015.

  1. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Hi all,

    So I have a reference object which is 1152x2048. I also have my canvas set to reference resolution of the same size.

    If I put the GUI button at 135, 250, it puts the button around the middle of the play screen. When I export to iOS to test, the button is placed up in the far left corner. So it looks like x,y is being placed in reference to the screen resolution, but what can I do to have it show in the same location during play testing?
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    If you have canvas and use new ui you have option to keep aspect ratio. And if not you can set it at % of screen.width and screen.height like at 10% of width and 50% of height
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why are you using a Canvas and a GUI.Button together? The two are part of different systems, that are pretty much incomparable with each other.

    Use a Button component from the new UI instead. Place the anchors on the corners of the screen for a button that stretches. Or all in the corner to keep the button in position relative to the corner.
     
  4. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Ya you are right. I ended up with a button from the new UI, attached my sprite to it, setup my levelmanager in the On Click, and it works as expected. I tried making that work before but didnt. I think doing this stuff at 4am probably contributed to it not working properly. Lol

    Anyways it all works now and thx!

    So is Gui.Button simply the old way of doing things? Just curious as I have only be been using Unity for a few months.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    GUI.Button is an old way. It's still used for editor scripting.