Aspect-ratio scale

  • Let’s say you design your interface coordinates for 1920x1200, as the Lerpz tutorial suggests. Then, you scale it as the tutorial says like this:

GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
(Screen.width / 1920.0, Screen.height / 1200.0, 1));

My question is, how do you do it when the aspect ratio changes?

Also, I would like to ask something I have been looking for a few days and I haven’t found. I have a texture with a lot of GUI elements in it, how do I render them separately? More specifically, where do I specify the UV coords inside the texture for the GUI.Box, for example?

You have a few options…
• Many games just stretch-to-fill to the screen - aspect ratio be damned,
• Many games just stretch-to-fit so that the GUI can fit in a (typically centered) square on-screen.

You slice up the texture in photoshop, then draw that.
Take a look here for some GUI skins for inspiration:
http://unity3d.com/support/resources/assets/extra-gui-skins
http://unity3d.com/support/resources/assets/built-in-gui-skin

Have fun!

I see, slicing in photoshop and having a texture for every element…

But what if I really need to draw just a region of the texture? For example, I have a top view of a navigation map, the texture for the map is 2048x2048 and I only want to draw the 512x512 region where the player is, centered on the player… how would I do it?

You would use a BeginGroup…EndGroup to set up a clipping rect, then slide your texture around inside that.

Contrieved. :wink: :wink: :wink:

Seriously, I really think that the system would be far better with UV support, there are a gazillion things in which you need them, the map is just an example… another example would be a digital compass, or the altimeter (I’m doing an airplane game, is it obvious? :slight_smile: ) what do you think?

Nicholas,

I have finally implemented it using the BeginGroup method and in the end I don’t find it that painful, in fact, it’s probably even better than having to set texture coords in the classical way (u = coordX/Texture.width;).

The only problem is probably that inside is more expensive in performace, because the BeginGroup/EndGroup system???