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 You! · Jun 18, 2012 at 06:00 PM · guitexture2dresize

Uncompressed Texture for GUI Doesn't Display

I am using a texture to display text in a specific font and font size on the screen as a part of the GUI. The texture is too big because it covers the main character during game play. I've been trying to resize the texture in the start function, but the texture doesn't display when I do. (The texture does display when I do not try to resize the texture) Here's my code for resizing the texture:

 var xGUITexture : Texture2D;
 
 function Start (){
         xGUITexture.Resize(Screen.width/2,Mathf.RoundToInt(Screen.width * 0.3),xGUITexture.format,true);
         xGUITexture.Apply();
     }
 //[...]

Am I using the wrong texture format? What would cause the texture not to appear? (I'm not showing the calls for showing the texture because they are working with the texture when it is not resized).

Comment
Add comment · Show 14
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 Wolfram · Jun 18, 2012 at 06:22 PM 0
Share

GUITexture is a Unity class, you shouldn't name your variables identically to these. Do you have #pragma strict enabled?

avatar image You! · Jun 18, 2012 at 06:26 PM 0
Share

I changed the name of the texture, since I am using the name only as an example. And yes, I do have #pragma strict enabled

avatar image Wolfram · Jun 18, 2012 at 06:27 PM 0
Share

Well, the calls you use to display your texture are important, since we have no way of knowing whether you are using UnityGUI, the actual GUITexture object, or a 3D plane that's textured with a Texture2D to display your GUI.

This may also deter$$anonymous$$e whether resizing the texture is even the right approach (e.g., it will not change the displayed size when using textured 3D objects).

avatar image reptilebeats · Jun 18, 2012 at 06:40 PM 1
Share

do you have to change the texture size, wouldnt be easier to change the scale of the gui

avatar image Wolfram · Jun 18, 2012 at 10:16 PM 1
Share

AFAI$$anonymous$$, it only affects everything that comes after it, and only in that particular call to OnGUI().

Show more comments

1 Reply

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

Answer by Wolfram · Jun 18, 2012 at 10:14 PM

Oh, just noticed this myself.

Reading documentation does help: "After resizing, texture pixels will be undefined."

So texture.Resize() does not rescale your texture, it simply changes the dimensions and provides the memory adaption, but deletes the content.

And since non-uniform scales of a GUI.Label's Rect() are apparently (and unfortunately) ignored, your best option is to leave the texture alone, and scale your GUI:

 function OnGUI(){
     // store original matrix
     var guiMatrix:Matrix4x4=GUI.matrix;
 
     // where the label should be placed
     var labelRect:Rect=Rect(xPos,yPos,xGUItexture.width,xGUItexture.height);
 
     // scale GUI relative to current label size
     var scaleFactor:Vector2=Vector2(
             Screen.width/2 / labelRect.width,
             Mathf.RoundToInt(Screen.width * 0.3) / labelRect.height);
 
     // scale GUI around center of label
     GUIUtility.ScaleAroundPivot(scaleFactor, labelRect.center);
 
     // place label
     GUI.Label(labelRect,xGUItexture);
 
     // reset GUI scaling
     GUI.matrix=guiMatrix;

     // other GUI stuff, un-scaled
     // [...]
 }

As I said, I don't really use UnityGUI, so no guarantees ;-)

Comment
Add comment · Show 7 · 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 You! · Jun 18, 2012 at 10:51 PM 0
Share

Still nothing... I don't know how my code compares, but it looks similar:

 funciton OnGUI(){

 var labelRect : Rect = Rect(Screen.width - question.width,0,question.width,question.height);
     
 var scaleFactor : Vector2 = Vector2($$anonymous$$athf.RoundToInt(Screen.width/2/labelRect.width),$$anonymous$$athf.RoundToInt(Screen.width * 0.3 / labelRect.height));

 var pivot : Vector2 = Vector2(Screen.width,0);

 GUIUtility.ScaleAroundPivot(scaleFactor,pivot);
         
 GUI.Label(labelRect,question);
 }
avatar image Wolfram · Jun 19, 2012 at 01:55 AM 0
Share

I assume you removed the code in Start() messing with the texture?

What exactly happens, don't you see the texture at all, is it black, is it unchanged in size? Please elaborate. Your code seems fine, from what I can see.

avatar image Wolfram · Jun 19, 2012 at 01:58 AM 1
Share

One thing I see, change the Screen.width/2 to Screen.width/2.0, so it will be a float. Otherwise the result is 0 if labelRect.width is > Screen.width/2.

avatar image You! · Jun 19, 2012 at 02:14 AM 0
Share

First, to the questions, the texture does not display at all, and the Start function and all of its contents have been eli$$anonymous$$ated.

Second, I tried the suggestion,but nothing changed (but I'm definitely keeping the change, though).

avatar image Wolfram · Jun 19, 2012 at 02:17 AM 0
Share

Then experiment around a bit with scaleFactor and pivot (make them modifyable in the inspector), starting from neutral values ((1,1) for scale, screen center for pivot), or comment out that line completely, to figure out where the problem lies.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way to resize textures on the GUI? 3 Answers

Resize a sprite by giving it more transparent pixels 0 Answers

My OnGUI() Won't show the Button elements :( 0 Answers

Draw crosshair INSTEAD of cursor. 1 Answer

Capture screenshot to texture, memory issue 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