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 hav_ngs_ru · Oct 18, 2014 at 02:58 PM · guiuvaspect ratiostretch

How I can stretch texture with GUI.DrawTextureWithTexCoords ?

Could someone explain me, how can I draw GUI Texture partially (with given UV-coords) and make texture stretched (with aspect ration different from it`s original)?

GUI.DrawTexture can stretch textures, but cannot draw a part of it. GUI.DrawTextureWithTexCoords can accept the UVs, but cannot stretching options.

My case: I draw animated GUI-elements, and use UVs to draw one frame of spritesheet. It worked fine until I had to animate non-square texture. Besides, the size (and aspect ratio) of the output rect is calculated runtime.

Does someone know any solution?

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

1 Reply

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

Answer by zharik86 · Oct 18, 2014 at 06:10 PM

Using GUI.DrawTextureWithTexCoords() you can draw part of a texture. You need to set correctly coordinate UVs. For example, we should draw only a half of a texture in a certain size. See an example below(write on CSharp):

  public Texture2D myTex = null; //our texture, reference in Inspector
  private Rect myRect; //our UVs coordinate

  void Start() {
   //Initialixation rectangle for half texture;
   myRect = new Rect(0, 0, 0.5f, 0.5f);
  }

  void OnGUI() {
   //Draw our part texture for size, for example, 300x200. It doesn't consider aspect of a texture
   //first element - position, second - texture, third - UVs
   GUI.DrawTextureWithTexCoords(new Rect(20, 20, 300, 200), myTex, myRect);
  }

I hope that it will help you.

Comment
Add comment · Show 6 · 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 hav_ngs_ru · Oct 18, 2014 at 06:41 PM 0
Share

do your mean that if I have a source texture, for example, 300x200, then myRect = new Rect(0, 0, 1f, 0.5f) will stretch a texture vertically to fill all the Rect(20, 20, 300, 200) on screen?

avatar image zharik86 · Oct 19, 2014 at 06:59 AM 0
Share

@hav_ngs_ru You misunderstand textural coordinates. For your texture 300x200 and myRect = new Rect (0, 0, 1f, 0.5f), you receive the picture stretch down in Rect(20, 20, 300, 200). And its initial size will be 300x100. But as we insert it into the size 300x200, the vertical component will be expanded. It is checked for Unity 4.1.2.

avatar image hav_ngs_ru · Oct 26, 2014 at 03:29 PM 0
Share

yes, I not quite correctly formulated question. I actually ment "will stretch a texture vertically to fill all the Rect(20, 20, 300, 200) by half of texture". So, you in fact answered "yes" to my question :) Thank you for answer.

avatar image zharik86 · Oct 26, 2014 at 07:37 PM 0
Share

@hav_ngs_ru If I help you, please, mark this answer:)

avatar image hav_ngs_ru · Oct 31, 2014 at 04:03 PM 0
Share

zharik86, sure, but I'll test your advice first ;)

still haven't test because the request is in backlog yet

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

27 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 avatar image avatar image avatar image

Related Questions

GUI Aspect Ratio? 1 Answer

Impossible to design full screen GUI for all aspect ratios? 2 Answers

DrawTexture without stretching it 0 Answers

Can I configure a Rect Transform to preserve its aspect ratio while stretching? 1 Answer

Stretch Colliders when changing resolutions ? 0 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