Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ful · Jun 03, 2014 at 11:27 PM · androidjavascriptpositionguitextureguitext

Android: How do you position GUITexture and GUIText according to different screen sizes (JS)?

Hello Everyone!

I am about to finish to first android game, and one of my remaining tasks is to position every GUITexture and GUIText according to the screen size (Portrait 2:3 in my case). I have several of these elements positioned in varies different spots around the screen such as in the top-left corner I have a timer, on the bottom-left and bottom-right corner I have two arrows.

Everything looked good when I was testing out the game through the Unity Remote app, however when I built it and installed it in my Tablet I noticed several of the GUITextures and GUIText had shrunken and moved, but my sprites remained in their original size.

I did some research and I do know that in order to position something at the centre of the screen, it is necessary get the Screen.Height and Screen.Width then divide each by 2. I played around with this theory hoping to solve my problem, but I am having difficulties on understanding it. As for the maintaining scale, I have tried the transform.localScale = Vector3.zero, but unfortunately it is not working for me.

So my question is this, would anyone be kindly enough to explain me how do you position GUITexture and GUIText according to different screen sizes, and how do you maintain their original scale?

Thanks in advance!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zharik86 · Jun 04, 2014 at 07:03 AM

For different resolutions of the android (or other screens), it is necessary to scale your GUITextures and GUIText. I will write a small explaining script (I write on Java as this language figures in your tags; there can be small mismatch as I for me the main am CSharp):

  //define here the original resolution 
  var origW: float = 320;
  var origH: float = 640;
 
  function Start() {
   var scaleX: float = Screen.width / origW; //your scale x
   var scaleY: float = Screen.height / origH; //your scale y
   //Find all GUIText object on your scene
   var texts: GUIText[] =  FindObjectsOfType(GUIText) as GUIText[]; 
   for(var myText: GUIText in texts) { //find your element of text
    var pixOff: Vector2 = myText.pixelOffset; //your pixel offset on screen
    var origSizeText: int = myText.fontSize;
    myText.pixelOffset = new Vector2(pixOff.x*scaleX, pixOff.y*scaleY); //new position
    myText.fontSize = origSizeText * scaleX; //new size font
   }
   //Find all GUITexture on your scene
   var textures: GUITexture[] =  FindObjectsOfType(GUITexture) as GUITexture[];
   for(var myTexture: GUITexture in textures) { //find your element of texture
    var pixIns: Rect = myTexture.pixelInset; //your dimention of texture
    //Change size pixIns for our screen
    pixIns.x = pixIns.x * scaleX;
    pixIns.y = pixIns.y * scaley;
    pixIns.width = pixIns.width * scaleX;
    pixIns.height = pixIns.height * scaleY;
    //Sets new rectangle for our texture
    myTexture.pixelInset = pixIns;
   }
  }

That's all. Attach this script to any object, for example, main camera. I hope it to you will help.

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 Ful · Jun 04, 2014 at 09:38 AM 0
Share

Thank you!!! This was exactly what I was looking for!

avatar image vimago · Sep 03, 2014 at 11:56 AM 0
Share

I think this looks nice but I still have a problem:

"error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)"

This is because myText.fontSize is an int while scaleX is a float

 myText.fontSize = origSizeText * scaleX; //new size font


how did you solve that? I did the cast:

 myText.fontSize = origSizeText * (int)scaleX; //new size font

but if I debug myText.fontSize, it is always 0.

-------------------------- EDIT ---------------------------------- SOLVED!!!!

Ok, my first problem was that I had myText.pixelOffset =(0,0), so I've changed manually the Pixel Offset of all my GUITexts to (1,1). That's why I always got myText.fontSize = 0.

Then, I added a float variable "`floatFontSize`" to get the result of "`origSizeText * scaleX`".

And then I set myText.fontSize = (int)$$anonymous$$athf.RoundToInt(floatFontSize);

So I get the closest value of te right fontSize.

Here is my code (is in C#)

     //define here the original resolution 
     private float origW = 640f;
     private float origH = 320f;
 
 void Start(){    
     float scaleX = Screen.width / origW; //your scale x
     float scaleY = Screen.height / origH; //your scale y
     
     //Find all GUIText object on your scene
     GUIText[] texts =  FindObjectsOfType(typeof(GUIText)) as GUIText[]; 
     
     foreach(GUIText myText in texts) { //find your element of text
     
          Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
          int origSizeText = myText.fontSize;
          myText.pixelOffset = new Vector2(pixOff.x*scaleX, pixOff.y*scaleY); //new position
          float floatFontSize = origSizeText * scaleX; //new size font in a float
          myText.fontSize = (int)$$anonymous$$athf.RoundToInt(floatFontSize); // Closest value of fontSize
     
     }
 }






avatar image Meena_Fari · Feb 19, 2019 at 01:48 PM 0
Share

sorry, it may be very late,, but I am bit confused about original resolution,, which original resolution have to give in script public and if we have to put original resolution then how it differs on different devices

avatar image
0

Answer by meat5000 · Jun 04, 2014 at 08:35 AM

Use transform and scale 0 -> 1

Works at any res.

Z component of transform controls depth; ie what appears on top or behind

x=0.5, y=0.5 is centre

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

24 People are following this question.

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

Related Questions

Making High Scores and scores via GUITexts 1 Answer

Pairing and GUITexture with a Timer? 1 Answer

How Do I Drag And drop GUITextures From Set Positions? 1 Answer

Android car controls c# 0 Answers

GUITexture and GUIText on android 2 Answers


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