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 Baalhug · Dec 11, 2013 at 11:45 PM · guitexturetiledrawtexture

Tiling textures issue

Hi, guys:

I need to create a graphical display for a variable in my game. This variable (energy) is decreasing over time so i want a colored label indicating the level of energy. The variable is controlled by a script (charscript) in the char. So i created a texture bitmap which is a green-red gradient. This variable goes from 100 (total green) to 1 (total red) so i've been watching around and i've come to this:

 public Texture2D gradbitmap;
 public charscript cs;
 public float energygradient;
 
 cs = (charscript)GameObject.Find("Char").GetComponent<charscript>();
 
 OnGUI(){
    energygradient=cs.energy;
    GUI.DrawTextureWithTexCoords(new Rect(500,80,20,20), gradbitmap, new Rect(0f,0f,energygradient,1f));
 }

The problem is energygradient will draw a percent of the bitmap into the label, so if it is 0.1 it will draw 10% of the left side of the bitmap, -0.1 10% of the right side of the bitmap, 0.2 20% of the left side of the map and so on. But the variable decreases from 100 to 1. I want the label shows only 10% of the bitmap, but i need it to "move" along the bitmap, first 10%, then from 1% to 11%, 2% to 12% ... and finally 90% to 100%. I mean to create a "window" on the gradient texture so it looks like the GUI object becomes green to red in a soft way.

I can't find a math solution for this. Do you know any other way to achieve it?

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 Baalhug · Dec 12, 2013 at 12:19 AM 0
Share

I think the only way to have this working by maths relies on TexCoords. It is defined by a Rect(x1,y1,x2,y2). All these coordinates are between 0-1, but i'm trying different values for x1 and x2 with no good results, and i cant find documentation about it. I first thought Rect(energygradient,0f,energygradient+0.1,1f) would do it, but it isnt.

1 Reply

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

Answer by Baalhug · Dec 12, 2013 at 09:09 AM

Well, i have found he way to do it. It seems the texture coordinates (TexCoords) in DrawTextureWithTexCoords(position, texture, TexCoords) are as follow:

Rect(FromX, FromY, ToX, ToY)

They are all in values from 0 to 1, so 0.1 is 10% of the texture, 0.5 is 50% and so on. ToX and ToY are the percent amount of bitmap to display, and FromX, FromY are the percent of the texture where it starts to print.

I used:

energygradient=cs.energy/100; GUI.DrawTextureWithTexCoords(new Rect(500,80,20,20), gradbitmap, new Rect(energygradient,0f, 0.1f, 1f));

So energygradient will have a value from 0 to 1 too (actually from 1 to 0 as it begins in 100 and decreases to 1). This way at the beginning it will be:

Rect (1f,0f,0.1f,1f)

In the middle:

Rect (0.5f,0f,0.1f,1f)

And finally:

 Rect (0.01f, 0f, 0.1f, 1f)
         |    |     |    |
         |    |     |    -> End of texture Y (100%)
         |    |     ------> End of texture X (10%)
         |    ------------> Start of texture Y (0%)
         -----------------> Start of texture X (1%)

It will always begin to print based on the actual energy and will display 10% more of the texture. Vertical coordinates are always full, from 0 to 1 (0% to 100%)

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

17 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

Related Questions

GUI.DrawTexture not working? (nothing shows up) 1 Answer

inventory system - change GUI.DrawTexture texture 3 Answers

PackTextures and DrawTexture 1 Answer

Tile and offset a Texture2D? 2 Answers

Help with destroying guiRect? 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