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 Atzig · Jul 17, 2014 at 04:14 AM · guiimagefademeter

Fade GUITexture

This is a multi part question. Sorry if this has been asked before, I've searched for awhile and didn't find what I was looking for.

I'm making a simple meter to represent a battery for a flashlight using two GUI textures(one is just the outline of a battery, the other a gradient of red to green, if it helps).

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class HUD : MonoBehaviour {
 
     // various textures to display on screen
     public Texture2D reticle;
     public Texture2D batteryIcon;
     public Texture2D batteryMeter;
 
     public float maxBatteryHealth = 100;        // the max amount of battery health
     public float curBattHealth;                    // the current amount of battery health
     public float batteryDecayRate = 1;            // the rate at which the battery will decay
 
 
     public bool battAreDead = false;            
 
     // Use this for initialization
     void Start () {
 
         curBattHealth = maxBatteryHealth;        // set the current battery health to the max battery health to start
         Screen.showCursor = false;                // Hide the mouse 
     }
 
     void OnGUI()
     {
         // code for the on screen reticle
         float xMin = (Screen.width / 2) - (reticle.width / 2);        // reticle x position
         float yMin = (Screen.height / 2) - (reticle.height / 2);    // reticle y position
         GUI.DrawTexture (new Rect (xMin, yMin, reticle.width, reticle.height), reticle);    // draw the reticle to positions
 
         // Code for the battery icon                
         GUI.DrawTexture (new Rect (1300, 545, batteryIcon.width / 5, batteryIcon.height / 5), batteryIcon);
 
         // Code for the battery meter
         GUI.DrawTexture (new Rect (1300, 545, batteryMeter.width / 5, batteryMeter.height / 5), batteryMeter);
 
     }
 
 
     // Update is called once per frame
     void Update () 
     {
         if (GameObject.Find("flashlight.GetComponent<Flashlight>().flashLightEnabled") == true) // find the flashlight to
                                                                                                 // check if it's on
         {
             curBattHealth -= Time.deltaTime - (batteryDecayRate * 2);    // drain curbattHealth here, the equation is wrong!
 
         }
         else
         {
             // if the flashlight is off, do nothing to the battery
         }
 
         if (curBattHealth <= 0)         
         {
             curBattHealth = 0;
             battAreDead = true;        // if the batteries have no health left they are dead, TO DO: disable the flashlight
         }
     }
 }
 

Now for the questions:

  1. How do I fade the battery meter GUI texture based on the value of curBattHealth?

  2. What's the code for draining the battery over time? The code I have doesn't seem to do anything.

  3. If the flashlight is off, do I need to add code to stop the battery from draining?

  4. If the flashlight is off, how do I store the curBattHealth?

Pretty new to programming myself so any advice/tips etc. would be greatly appreciated.

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 Atzig · Jul 19, 2014 at 05:44 PM 0
Share

No answer? Or has this been asked a bunch of times before and I just missed it?

1 Reply

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

Answer by zharik86 · Jul 19, 2014 at 06:36 PM

Well, I will rewrite your code and I will add comments that you understood responses to your questions:

  using UnityEngine;
  using System.Collections;
 
  public class HUD : MonoBehaviour {
 
   // various textures to display on screen
   public Texture2D reticle;
   public Texture2D batteryIcon;
   public Texture2D batteryMeter;
 
   public float maxBatteryHealth = 100; // the max amount of battery health
   public float curBattHealth; // the current amount of battery health
   public float batteryDecayRate = 1; // the rate at which the battery will decay

   public bool BattAreDead = false;

   // My variable
   public float speedBatt = 10.0f // Speed of fading of the battery 10 unit per second
      
 
   // Use this for initialization
   void Start () {
    curBattHealth = maxBatteryHealth; // set the current battery health to the max battery health to start
    Screen.showCursor = false; // Hide the mouse 
   }
 
   void OnGUI() {
     if (GameObject.Find("flashlight") != null && !battAreDead) { //checks battery object
      // code for the on screen reticle
      float xMin = (Screen.width / 2) - (reticle.width / 2);      // reticle x position
      float yMin = (Screen.height / 2) - (reticle.height / 2);    // reticle y position
      GUI.DrawTexture (new Rect (xMin, yMin, reticle.width, reticle.height), reticle);    // draw the reticle to positions
 
      // Code for the battery icon                
      GUI.DrawTexture (new Rect (1300, 545, batteryIcon.width / 5, batteryIcon.height / 5), batteryIcon);
 
      // Code for the battery meter
      //Fade out battery
      GUI.color = new Color(1.0f, 1.0f, 1.0f, curBattHealth / maxBatteryHealth);
      GUI.DrawTexture (new Rect (1300, 545, batteryMeter.width / 5, batteryMeter.height / 5), batteryMeter);
    }
   }
 
   // Update is called once per frame
   void Update () {
    // In my opinion, it is necessary to find only object battery  
    if (GameObject.Find("flashlight") != null && !battAreDead) { // find the flashlight
     curBattHealth -= speedBatt*Time.deltaTime;   // drain curbattHealth
     if (curBattHealth <= 0) {
      curBattHealth = 0;
      battAreDead = true; //and destroy flashlight
      Destroy(GameObject.Find("flashlight").gameObject);
     }  
    } else { //re-drain battery
     curBattHealth += speedBatt*Time.deltaTime;
     if (curBattHealth >= maxBatteryHealth) {
      curbattHealth = maxBatteryHealth;
      battAreDead =false;
     }
    }
   }
  }

And attach this script to any object (except flashlight object) on scene, for example, MainCamera.

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 Atzig · Jul 19, 2014 at 09:52 PM 0
Share

Thanks a lot! There were a couple of typos but I got it working. I'd vote it up but apparently I can't yet.

avatar image zharik86 · Jul 20, 2014 at 11:19 AM 0
Share

@Atzig Except vote, you can check this answer, if I helped you. To check the response, look slightly below than vote.

avatar image Atzig · Jul 20, 2014 at 04:13 PM 0
Share

Done! I did notice however that the battery drains whether or not the flashlight is on, I'm sure I can figure that out though, thanks again!

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

2 People are following this question.

avatar image avatar image

Related Questions

slowly make gui image more transparent 2 Answers

Disable component via script 1 Answer

Fading an image drawn with GUI.DrawTexture 1 Answer

Gui.Button Image in Button - error 1502 and 1503 0 Answers

making a fadeout with the new GUI in 4.6? 3 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