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 /
This question was closed Nov 29, 2014 at 09:12 AM by Kiwasi for the following reason:

Problem is outdated. This question has become irrelevant due to the introduction of UI in Unity 4.6. Leaving on sight for historical reasons.

avatar image
1
Question by Thet Naing Swe · Oct 14, 2010 at 08:27 AM · scaleguitexture

GUI Texture scale and position according to the actual screen resolution..

Hi Everyone, how can I scale and position the GUITexture according to the current screen resolution. I don't want to use the built in GUI function which already include the scaling parameter.

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 TSSTUDIOS · Nov 23, 2013 at 03:45 PM 0
Share

omg would be so much easier if unity had an left right anchor seeting on gui textures

12 Replies

  • Sort: 
avatar image
6

Answer by MrPhil · Nov 06, 2010 at 05:35 PM

This will center the texture based on the screen resolution and the image's aspect ratio (won't stretch it.)

private GUITexture myGUITexture;

void Awake() { myGUITexture = this.gameObject.GetComponent("GUITexture") as GUITexture; }

// Use this for initialization void Start() { // Position the billboard in the center, // but respect the picture aspect ratio int textureHeight = guiTexture.texture.height; int textureWidth = guiTexture.texture.width; int screenHeight = Screen.height; int screenWidth = Screen.width;

 int screenAspectRatio = (screenWidth / screenHeight);
 int textureAspectRatio = (textureWidth / textureHeight);

 int scaledHeight;
 int scaledWidth;
 if (textureAspectRatio <= screenAspectRatio)
 {
     // The scaled size is based on the height
     scaledHeight = screenHeight;
     scaledWidth = (screenHeight * textureAspectRatio);
 }
 else
 {
     // The scaled size is based on the width
     scaledWidth = screenWidth;
     scaledHeight = (scaledWidth / textureAspectRatio);
 }
 float xPosition = screenWidth / 2 - (scaledWidth / 2);
 myGUITexture.pixelInset = 
     new Rect(xPosition, scaledHeight - scaledHeight, 
     scaledWidth, scaledHeight);

}

Comment
Add comment · Show 5 · 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 Taffer · Apr 22, 2011 at 02:36 PM 0
Share

Your last line should be: myGUITexture.pixelInset = new Rect( xPosition, (float)( screenHeight - scaledHeight ) / 2.0f, scaledWidth, scaledHeight );

avatar image Taffer · Apr 22, 2011 at 03:10 PM 0
Share

$$anonymous$$y values seem to work properly if I set the position of the GUITexture to 0,0,0 and scale to 0,0,0 in the editor...

avatar image samz · Feb 19, 2013 at 11:05 AM 0
Share

how do i manipulate this for other places in the screen other than the center?

avatar image clunk47 · Aug 13, 2013 at 06:09 PM 0
Share

Thank you for this. +1

avatar image Chubba · Jan 09, 2014 at 01:07 AM 0
Share

Gentlemen, we are the presence of a hero

avatar image
2

Answer by IJM · Oct 14, 2010 at 09:19 AM

You can do this:

GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), aTexture);
Comment
Add comment · Show 4 · 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 SirGive · Oct 14, 2010 at 11:04 AM 0
Share

I believe for position, you would want Screen.width/2, Screen.height/2.

avatar image IJM · Oct 14, 2010 at 04:03 PM 0
Share

This is for fullscreen texture. If you want to show a logo or something similar.It will work as long as you have the same aspect ratio for texture and for screen. (4:3,16:9,16:10...)

avatar image Thet Naing Swe · Oct 14, 2010 at 04:41 PM 0
Share

This is the function that has to be called from OnGUI()... is there any calculation for the GUITexture Transform to make it exactly looks like.. so I don't have to calculate this everytime as OnGUI functions are expensive..

avatar image Zynigma · Feb 12, 2014 at 03:46 AM 0
Share

Thank you so much, IJ$$anonymous$$. This helped me a lot. I was using GUI.Label, but that always kept the aspect ratio of the image. GUI.DrawTexture stretched just how I wanted it too.

avatar image
2

Answer by yoyo · Oct 14, 2010 at 06:03 PM

Attach a GUITexture to a game object and set the game object's transform to (0,0,0) for position, rotation, and scale. Now the pixel inset properties of the GUITexture control the screen position of the texture in pixels. (0,0) is the lower left, and (Screen.width, Screen.height) is the upper right.

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 Thet Naing Swe · Oct 16, 2010 at 11:24 AM 0
Share

Thanks.. I will try that way

avatar image
2

Answer by Jameslewood · Feb 10, 2014 at 07:51 AM

Why don't you want to use the built-in scaling function?

For anyone else searching for the most simple solution, you can create a script with the following:

 // 
 public Texture m_texture;
 
 //
 void OnGUI()
 {
     // 
     GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), m_texture, ScaleMode.ScaleToFit, true);
 }


Then in the editor, just drag and drop the fullscreen texture into the script. It will scale to fit and won't be cut off anywhere.

Comment
Add comment · Show 4 · 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 Novack · Apr 08, 2014 at 07:36 PM 0
Share

Works like a charm, thanks!

avatar image msaldana · Jun 11, 2014 at 04:40 PM 0
Share

I loved this way, cleaner and faster

avatar image magicman9836 · Jun 15, 2014 at 10:35 PM 0
Share

I tried it in both C# and in Javascript. I either get parsing error or I get an expected character ;. PLEASE HElP $$anonymous$$E

avatar image JayTDS · Jul 15, 2014 at 10:35 PM 0
Share

$$anonymous$$agicman, I think maybe you are miss an "}" at the end of your script to end the '$$anonymous$$onoBehaviour {'

avatar image
-1

Answer by psycocrusher · Mar 30, 2013 at 01:48 AM

This is what i use, replace the 570 and 380s with the resolution you're currently working on, somtimes it causes a little streching on the textures.

 function Start () {
 
     var DivideX= 570/guiTexture.pixelInset.x;
     var DivideY= 380/guiTexture.pixelInset.y;
     var DivideWidth=570/guiTexture.pixelInset.width;
     var DivideHeight=380/guiTexture.pixelInset.height;
     
     guiTexture.pixelInset = Rect(Screen.width/DivideX,Screen.height/DivideY,Screen.width/DivideWidth,Screen.height/DivideHeight);
     
 }
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
  • 1
  • 2
  • 3
  • ›

Follow this Question

Answers Answers and Comments

14 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

Related Questions

GUI.DrawTexture Artifacts 1 Answer

Scale GUITexture to screen resolutions 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

How do I scale a GUITexture from the sides instead of in the center? 1 Answer

Scaling guitexture gameobject 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