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 Cobradabest · Feb 01, 2013 at 12:19 AM · c#guihud

Is there anyway to only show a part of a GUI image during the game? (C#)

I used to make games using XNA, and one of the cool things about it was that for healthbars, you didn't need to create tonnes of images for each value of health.

Instead, you just have one image, and you can make it so that you can only show part of an image, and make it get smaller or bigger in game by using a Rect in the draw script, you do this by on the draw function, you can put in a rectangle, and it will only show what is in the rectangle

For example, a health bar, you can make it as a big or small as you like, depending on the health, the rect that the game has to show is the same height as the health bar, but the width is the changes depending on the health, the lower your health, the smaller the bar.

Is there anyway to do this with Unity?

I want to use a GUITexture for it, because the healthbar I've made had a gradient going from red to green, and the end of it is slanted, so a box wouldn't be the best thing to use, I don't want to scale it either, because I think that would ruin it.

UPDATE: Here's my code:

         health = (masterScript.health / masterScript.maxHealth);
         GUI.DrawTextureWithTexCoords(new Rect(gameObject.transform.position.x, 
             gameObject.transform.position.y, 1,1)
             , texture, new Rect(0,0, 1, health), false);

I'm getting a "Null Reference Exception" error for the "GUI.DrawTextureWithTexCoords" line, what am I doing wrong?

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 robertbu · Feb 01, 2013 at 12:33 AM 0
Share

Untested, but I think Graphics.DrawTexture() might do the job.

1 Reply

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

Answer by gfvfubb · Feb 01, 2013 at 12:49 AM

http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTextureWithTexCoords.html

If you google this method you'll see a lot of people complaining. The 'texture coordinates' are normalized float values that range around ~ 0 - 1.0f and they represent percents of the sprite image horizontally and vertically I think. Google for more. It does work like you want though.

Comment
Add comment · Show 9 · 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 Cobradabest · Feb 01, 2013 at 12:55 AM 0
Share

So it does the rect as a percentage? That's perfect! I wanted to do percentage, and thinking it would be hard to do, it seems that won't be the case!

One more question, seeing as how without any code, it shows up in full at all times, if I added this code into a script, and attached it to my GUI, would it autmatically no longer show that way and just draw with the script? Or would I have to do something to disable the way it draws now?

avatar image gfvfubb · Feb 01, 2013 at 12:58 AM 0
Share

I'm pretty sure texcoord is percentage float for horizontal and vertical but I'm not sure. I've only seen references to this in forums in passing and I ended up using another method so I'm not totally sure but lots of people said it works like you want.

You'll need to disable the way it draws now. This method is it's own drawing function that is intended to be run independently and without any support. Simply attach some sort of update code to texcoords somewhere and it should change to suit it.

avatar image Cobradabest · Feb 01, 2013 at 11:41 AM 0
Share

Sorry, I'm a newbie at Unity... How do I do that? Do I disable the GUITexture component, and just use the script?

avatar image Bunny83 · Feb 01, 2013 at 12:32 PM 0
Share

Yes, GUI.DrawTextureWithTexCoords actually draws the texture. You don't need the GUITexture component.

avatar image Cobradabest · Feb 01, 2013 at 12:41 PM 0
Share

Okay. What if I wanted pixel inset on my GUI? How would I do that?

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

12 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

Related Questions

Problems with GUI and Resolutions. (C#) 1 Answer

"NullReferenceException" while trying to draw a texture (C#) 1 Answer

How To Make GUI Buttons Load/Quit 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# How to Drag and Scale with Mouse Window 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