Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NinjaRubberBand · Feb 17, 2014 at 04:47 PM · androidguiresolution

Android screen size script not working

I found this script on the internet, that should make the game fit all size phones. But it does not work! Maybe i missed something and thats why im asking here.

 var originalWidth = 640.0;  // define here the original resolution
 var originalHeight = 400.0; // you used to create the GUI contents 
  
 function Start() {
     // calculate scales
     var scaleX : float = Screen.width / originalWidth;
     var scaleY : float = Screen.height / originalHeight;
  
     // find all texts and change the scale
     var texts : TextMesh[] = FindObjectsOfType(TextMesh) as TextMesh[];
     for (var text : TextMesh in texts) {
        text.transform.localScale = new Vector3(text.transform.localScale.x * scaleX, text.transform.localScale.y * scaleY, text.transform.localScale.z);
     }
  
     // find all gui textures and change the scale
     var textures : GuiTexture[] = FindObjectsOfType(GuiTexture) as GuiTexture[];
     for (var texture : GuiTexture in textures) {
        texture.transform.localScale = new Vector3(texture.transform.localScale.x * scaleX, texture.transform.localScale.y * scaleY, texture.transform.localScale.z);
     }
 }
Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Feb 17, 2014 at 04:47 PM 0
Share

What does "does not work" mean?

avatar image NinjaRubberBand · Feb 17, 2014 at 05:09 PM 0
Share

Yeah sorry about not writing that. What i overall want is a script that can make my game fit in any android phone. And in that case i found this script. But it doesen't do any difference. When i change the resolution of the game, the Gui texts isnt where they should be. And what i found this script for is to make all texts and object stay where they are even though the resolution has changed. But the script does not do anything. If you have any solutions please tell me :-)!

1 Reply

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

Answer by zharik86 · Feb 17, 2014 at 06:51 PM

You use two types of a conclusion of the text. TextMesh in fact is the three-dimensional text. Most likely you should use GuiText. Then you should scale your GuiText and GUITexture correctly. I will show on one element, the rest becomes similarly(write on CSharp):

  float origW = 640.0f;
  float origH = 320.0f;

  void Start() {
   float scalex = (float) (Screen.width) / origW; //your scale x
   float scaley = (float) (Screen.height) / origH; //your scale y
   GUIText myText = Gameobject.Find("Mytext"); //find your element
   Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
   int origSizeText = myText.fontSize;
   myText.pixelOffset = new Vector2(pixOff.x*scalex, pixOff.y*scaley);
   myText.fontSize = origSizeText * scalex;
  }

And texture.localScale don't scale your texture. For GUITexture you use pixelInsert, see docs Unity, or ask me. I hope that it will help you.

Comment
Add comment · Show 2 · 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 NinjaRubberBand · Feb 17, 2014 at 07:23 PM 0
Share

Well this doesn't help that much since its a csharp script, and i use javascript. And i don't have enough experience to translate this script to java .. :p And i don't really understand how i need to attach this script

avatar image zharik86 · Feb 17, 2014 at 07:52 PM 0
Share

@NinjaRubberBand Ok, I'm translating for you:

  var origW: float = 640;
  var origH: float = 320;
 
  function Start() {
   var scalex: float = Screen.width / origW; //your scale x
   var scaley: float = Screen.height / origH; //your scale y
   var texts: GUIText[] =  FindObjectsOfType(GUIText) as GUIText[]; 
   for(var myText: GUIText in texts) { //find your element
    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);
    myText.fontSize = origSizeText * scalex;
   }
  }

That's all. Attach this script to any object, for example, main camera. Simple:) And check this answer, if I help you.

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

20 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

Related Questions

Android GUI resize problem 1 Answer

problem with gui??? 0 Answers

Resolution for assets on Android 1 Answer

Resolution Multiplier on Android 1 Answer

GUI Overlay using mobile camera 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