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
4
Question by fpiesche · Mar 21, 2012 at 12:01 PM · 2dguispriterectdrawtexture

texCoords rect for GUI.DrawTextureWithTexCoords

Hi,

I'm trying to write a simple sprite animation system for 2D on the GUI layer and am running into problems with GUI.DrawTextureWithTexCoords. I have a sprite sheet that's 32x256px at 32x32 per sprite (ie. 1x8 sprites), but no matter what I set the texCoords Rect to I can't seem to get any of them to display.

Rects I've tried for texCoord:

  • (0, 0, 32, 32)

  • (0, 32, 32 ,32)

  • (0, -32, 32, 32)

  • (0, 0, 256, 32)

All of these give me the same result - if alphaBlend is true, I will always get a white square at near-full transparency, if alphaBlend is false, I will just get a white square.

Replacing DrawTextureWithTexCoords with DrawTexture gives me the full spritesheet squashed down to 32x32, but works fine otherwise.

Has someone successfully used GUI.DrawTextureWithTexCoords and can explain how the texCoords Rect works?

Thanks a lot :)

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

3 Replies

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

Answer by lnsiu · Mar 21, 2012 at 03:19 PM

I had the same problem, turns out the texture coordinates are normalized. So in your case you would have 1/8, 2/8, 3/8... positions. i.e a percentage.

 GUI.DrawTextureWithTexCoords(new Rect(0,0,32,32), theTexture, new Rect(0f,0f,0.125f,1f),true);
Comment
Add comment · Show 3 · 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 fpiesche · Mar 21, 2012 at 07:35 PM 0
Share

That is indeed perfect. Thanks a lot!

avatar image Mentalist4006 · Jun 15, 2012 at 05:21 PM 1
Share

I have been trying to solve this problem; the documentation was not very clear. Thank you so much!

avatar image Martín Coll · Jul 10, 2013 at 02:25 PM 0
Share

can't up vote, so +1

avatar image
4

Answer by amirabiri · Apr 21, 2012 at 03:49 PM

It's also worth noting that the texCoord Y-axis is opposite to the destination.

In other words to draw the bottom-left corner, you must use

 GUI.DrawTextureWithTexCoords(
     new Rect(pos.left, pos.top, dstWidth, dstHeight),
     texture
     new Rect(0f, 0f, srcWidth, srcHeight));
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
avatar image
0

Answer by Azzgil · Feb 13, 2017 at 08:14 AM

Maybe someone still needs this:

Rect texCoord(x, y, timesx, timesy)

Here: (x, y) is a position to START drawing texture FROM. Texture is drawn left-to-right and up-to-down.alt text

(timesx, timesy) is a thing that says how many times Unity must draw the texture. In the example below (tx, ty) = (2.5, 1.5) (and (x, y), obviously = (0,0)) alt text

And here is an example that uses both pairs of parameters:

UP: in the comments

PS Sorry for images, I'm not a good artist actually


q31hcdkuiwa.jpg (28.2 kB)
q31hcdkuiwa-копия.jpg (32.2 kB)
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 Azzgil · Feb 12, 2017 at 02:48 PM 0
Share

Well, only 2 pictures are allowed, and I can't attach more in comment somehow. So I leave a link to the 3d picture here http://imgur.com/rjxvPsT

avatar image aklgupta · Jul 17, 2021 at 02:22 PM 0
Share

It's worth mentioning that the Wrap Mode in the import setting of the texture should be set appropriately for this to work.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot see full view of Screen in Unity preview 2 Answers

Sprite Editor : how to use Sprites created by SpriteEditor via script ( not via inspector ) 0 Answers

Sprite Rect Moves Between Frames 0 Answers

Any reason to use Unity's GUI paradigm if I'm working with sprite sheets and don't need GUI button presses? 1 Answer

Scale Car Speedometer with Screen Size (made by Andeeee) 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