Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hawken-2 · Jul 15, 2011 at 08:18 AM · guiiosretina

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dylan92 · Jul 11, 2013 at 07:20 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hawken-2 · Jul 15, 2011 at 08:25 AM 0
Share

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.

avatar image Waz · Jul 15, 2011 at 08:37 AM 0
Share

Yes, that's fine. In the inspector are you using the Pixel Inset to position them, or the Transform fields?

avatar image hawken-2 · Jul 15, 2011 at 08:43 AM 0
Share

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?

avatar image Waz · Jul 15, 2011 at 10:23 AM 0
Share

Change the scale.

avatar image hawken-2 · Jul 18, 2011 at 08:02 AM 0
Share

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.

Show more comments
avatar image
0

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");
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BillyMFT · Jun 28, 2013 at 06:13 AM 0
Share

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 ));

avatar image oliver-jones · Jun 29, 2013 at 11:33 PM 0
Share

This doesn't work for me either - just makes everything huge within the GUI

avatar image BillyMFT · Jun 30, 2013 at 06:31 AM 0
Share

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; }

avatar image
0

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) ));

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pixel_fiend · Jun 30, 2014 at 06:20 PM 0
Share

If you're targeting ios / android, I recommend using Screen.dpi ins$$anonymous$$d of Screen.width. It's a bit more future-proof.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges