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 WabiWasabi · Feb 13, 2014 at 05:46 AM · guitextureimageresources

What's the best way to incorporate dynamically loaded textures into GUI controls?

I'd like to load in rectangular image resources via script, and use those images on GUI controls (i.e. drawn via OnGUI.) The problem I've run into is that when the pixel dimensions of the images I'm using aren't both powers of two, Unity scales them using the wrong aspect ratio (probably the powers-of-two aspect ratio of the enclosing Texture2D.)

The best workaround I've found so far is to use GUI.DrawTexture to draw an image on top of a control (e.g. a button), and specifying the aspect ratio that I know to be correct based on the pixel dimension. But this presumes I always know the actual pixel size of the source, which may not always be the case.

I've also tried the following:

  • Using a GUITexture and PixelInset to prevent scaling. But I can't figure out how to manage Z-order to make sure that a particular GUITexture will always draw on top of an OnGUI control. No matter what combination I use for the transform Z position on the GUITexture and GUI.depth in the OnGUI call, the OnGUI controls always render in front of the GUITexture.

  • If the image were in my project's asset db, I could mark it as TextureImporterType.GUI. But I need to support images that I won't know about at compile time, and Resources.Load doesn't seem to support anything analogous (and I don't see any properties on Texture2D that I can set after loading the image to get that behavior.)

Is there a better way to approach this than using GUI.DrawTexture with a specified aspect ratio? If not, is there a way for me to get the real pixel dimensions of the JPG/PNG that I'm loading into a Texture2D so that I can guarantee that my aspect ratio matches this?

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

1 Reply

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

Answer by MakeCodeNow · Feb 14, 2014 at 05:35 AM

You're going to need to use a C#/Mono library to load the jpeg/png yourself. That will give you the real dimensions, and also raw pixels that you can copy into Texture2D. There are a lot of libraries that will read standard image formats, just google a bit and you'll probably find one that works for you.

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

19 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

Related Questions

Blurry Texture Help 5 Answers

How can I prevent my GUI background texture from getting pixelated or stretched? 1 Answer

Progress Bar Parallel To Game Progress - HELP 1 Answer

How can I add a distance cap to this script? 3 Answers

How do I trigger GUI elements 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