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 HammerBG · Apr 22, 2013 at 04:26 PM · guishaderguitexturealpha-channelfill

GUI: How to "fill" a percentage of a texture?

I am trying to make my GUI more ... lets just say fancy, for the lack of better word :) But i am not sure it can be done in unity. Basically i have a texture that i want to fill up to a specific percentage, depending on the progress of a "upgrade".

In the example image below the "upgrade" is half filled. I am looking for a way to be able to do this, dynamically, via the gui tools or shading (if possible), so i wont need to have every stage of the "upgrade" as a separate image.

Note that only the gray border is "lighting up". :)

alt text

Any help, suggestions, directions, thoughts or comments on the matter would be appreciated :)

What i am thinking for a possible solution is to have both the 0% upgrade and the 100% upgrade images, and depending on the progress merge them at specific point on the Y axis, creating the desired effect. Although this can be a little tricky. Thoughts?

Comment
Add comment · Show 3
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 AlucardJay · Apr 22, 2013 at 04:48 PM 0
Share

Have you seen this very popular answer? http://answers.unity3d.com/questions/14770/creating-a-circular-progressbar-timer.html

avatar image HammerBG · Apr 22, 2013 at 05:36 PM 0
Share

Yes this is pretty much what i need in a nut shell, but the example is applied to a 3d object with mesh renderer. Since there is no material support on the unity gui(or is it?) i dont think this solution applies here :(

avatar image GerryM · Apr 22, 2013 at 07:55 PM 0
Share

In case you got the funds, consider using a procedural material, but you'll need Substance Designer for that.

1 Reply

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

Answer by LesPaul · Apr 22, 2013 at 06:16 PM

Try using the Texture2D.GetPixel() and SetPixel() functions. Put those in a loop that will cover the whole texture. First: check to where the texture should be rewritten to hight-wise. Then check which colors below that line are the color grey you make the borders that need changing using the GetPixel() function. If that pixel is the correct color, change it to the color it should be using the SetPixel() function.

Beware though: this could lag your game out a bit, but you can avoid that by only calling the loop when the bar needs to be changed hight-wise

Comment
Add comment · Show 1 · 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 HammerBG · Apr 22, 2013 at 06:39 PM 1
Share

Yeah, something along your idea. What i have done currently is a bit more optimized. Using GetPixels and SetPixels. Basically calculate what percentage need to be with the "filled" texture and what needs to be with the "empty" texture. Then copy those 2 GetPixels arrays into a third that will be the resulting SetPixels texture and compressing it for faster drawing.

Changing the third texture only when needed of course, not on every OnGUI call.

For the ones interested in the resulting code:

          float fillP=0.2f;// fill percentage
             empty = (Texture2D)Resources.Load("shieldempty");
         full = (Texture2D)Resources.Load("shield");
         
         
         half = new Texture2D(empty.width, empty.height);
         int fillH = $$anonymous$$athf.RoundToInt(empty.height * fillP);
         int emptyH = empty.height - fillH;
         Color[] bottom = full.GetPixels(0, 0, empty.width, fillH);
         Color[] top = empty.GetPixels(0, fillH, full.width, emptyH);
         
         Color[] all = new Color[half.width*half.height];
         Array.Copy(bottom, all, bottom.Length);
         Array.Copy(top, 0, all, bottom.Length, top.Length);
         
         half.SetPixels(all);
         half.Compress(true);
         half.Apply();

And then basically draw the texture when needed. I am currently trying to figure out a way to draw the "border" between the filled and the non filled texture. Probably with a 3rd texture that will hold only the gradient "border" texture.

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

14 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

Related Questions

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Write a Shader for GUI (GUITexture or scripted GUI) 0 Answers

Use the GUITexture to go to the next scene 1 Answer

Toggle GuiTexture 1 Answer

need help croping texture.... something wrong with my guigroup. 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