- Home /
How do I scale GUI object for retina display if they are not in OnGUI?
Currently my scene has GUITextures as object, created from the GameObject menu, not dynamically generated in OnGui.
The problem with this is that the code for bumping assets up to retina size, only works on GUIText / GUITextures generated in OnGui and not GUIText / GUITextures that are from the GameObjects menu.
How can I make it so any GUI element in my scene will use the Matrix4x4.identity.Scale command?
GUI Essentials is a GUI solution that automatically scales GUITextures and GUIText to any resolution. You can even keep aspect ratio. It's really easy to use and is mobile friendly. Try it out.
Answer by Waz · Jul 15, 2011 at 08:20 AM
GUITexture is resolution-independent provided you use the Transform to position and scale instead of the pixelInset rectangle.
I'm using iTween to move them about, so they need to be dropped in as game objects in the hierarchy and not built in code.
Yes, that's fine. In the inspector are you using the Pixel Inset to position them, or the Transform fields?
ah I see, so pixel inset is a no no for the re-scaling code below? If I do move them around using transform, I'm also wondering, how can I then make them double the size?
care to elaborate a little? I'm not really sure what the code for that might be for this. I get an error because Scale is not part of GameObject.
Answer by hawken-2 · Jul 15, 2011 at 08:29 AM
heres the retina code that works when GUI objects are built inside it
function OnGUI() {
var screenScale: float = Screen.width / 320.0;
var scaledMatrix: Matrix4x4 = Matrix4x4.identity.Scale(Vector3(screenScale,screenScale,screenScale));
GUI.matrix = scaledMatrix;
GUI.Label( Rect( 0, 0, 480, 20 ), "test");
}
this doesn't work for me. Objects are the wrong size and position on a retina display. Does it only work with certain GUI code? Should it work with something like this?
GUI.BeginGroup(Rect(Screen.width/2- (usernamePanelBG.width/2)/2,Screen.height/2-(usernamePanelBG.height/2)/2,(usernamePanelBG.width/2),(usernamePanelBG.height/2)+100 ));
This doesn't work for me either - just makes everything huge within the GUI
oops. post my reply as an answer. Guess it is in a way. Anyway, this is what I ended up using
var scaleChanger:int=2; if(Screen.width == 1024) { //if not retina scale all gui by 50% GUIUtility.ScaleAroundPivot(Vector2.one * 0.5, Vector2.zero); scaleChanger=1; }
Answer by BillyMFT · Jun 30, 2013 at 07:20 AM
I ended up doing this
var scaleChanger:int=2; if(Screen.width == 1024) { //if not retina scale all gui by 50% GUIUtility.ScaleAroundPivot(Vector2.one * 0.5, Vector2.zero); scaleChanger=1; }
scaleChanger was used to correct some weird positioning issues on non-retina displays. eg
GUI.BeginGroup(Rect(Screen.width/scaleChanger- (postingBG.width)/2,Screen.height/scaleChanger-(postingBG.height)/2,(postingBG.width),(postingBG.height) ));
If you're targeting ios / android, I recommend using Screen.dpi ins$$anonymous$$d of Screen.width. It's a bit more future-proof.
Answer by thinkyhead_ · Jan 12, 2015 at 12:42 AM
All my GUITexture
and GUIText
elements are built and positioned around a 1024 x 768 display and they are intended to scale in proportion to the screen rather than to stay at the same size across all screens. This allows me to keep using pixelInset
and to avoid messing with Transform scaling, which simplifies things somewhat. With my base resolution at 1024x768 it's pretty easy to re-scale everything for other resolutions, including Retina displays. In my Globals
class I define this class method:
public static void AdjustGUISizes(float ratio) {
if (ratio != 1f) {
foreach (GUITexture b in FindObjectsOfTypeAll(typeof(GUITexture))) {
Rect i = b.pixelInset;
i.x *= ratio; i.y *= ratio; i.width *= ratio; i.height *= ratio;
b.pixelInset = i;
}
foreach (GUIText tx in FindObjectsOfTypeAll(typeof(GUIText))) {
float fontRatio = ratio; if (fontRatio < 0.85f) fontRatio = 0.85f;
tx.fontSize = (int)(tx.fontSize * fontRatio);
tx.pixelOffset *= ratio;
}
}
}
And then I call it like so:
pixelRatio = Screen.width / 1024.0f;
Globals.AdjustGUISizes(pixelRatio);
Note that all of this applies to old-style GUI elements, which are now deprecated in favor of the new Canvas-based GUI system. And furthermore, this isn't set up (as it is) for dynamic scaling as the dimensions change (such as you get with a resizable window). Plus, while FindObjectsOfTypeAll
is useful for this, it is slow. It would be faster to cache the list of elements and work from that if dynamic resize is needed.
So consider this as a helpful note for maintainers of legacy code, or for historic interest.
Your answer

Follow this Question
Related Questions
Problem with centering text 1 Answer
iOS development and GUI for it 1 Answer
size of GUI pics too big 1 Answer
Issue with GUI.TextArea keyboard on iPhone 0 Answers
Detecting Input with GUI Textures? 1 Answer