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
1
Question by zero84085 · Aug 26, 2012 at 10:10 PM · guitexture2dtextureschange

change variable and Texture2D

hello guys i have create a nightvision script and that i want to make a gui battery(of nightvision machine) after the variable go to 80 or 60 and etc change the texture to another, how i can make that... take a look to my script.

 var texture : Texture2D;
 var nightVisionON : AudioClip;
 var nightVisionOFF : AudioClip;
 var isNightVision = false;
 var fAlpha : float = 0.5;
 var NightVisionLight : GameObject;
 var batteryTexture : Texture2D;
 var battery100 : Texture2D;
 var battery80 : Texture2D;
 var battery65 : Texture2D;
 var battery50 : Texture2D;
 var battery35 : Texture2D;
 var battery10 : Texture2D;
 var NoBattery : AudioClip;
 var timeSpeed : float = 0.5;
 var batteryPower : float = 100;
  
 function Start()
 {
         NightVisionLight.active = false;
 }
  
 function Update()
 {
         
         if(Input.GetKeyDown("g")&&(isNightVision == false))
         {
                 isNightVision = true;
                 batteryPower = 100;
                 audio.PlayOneShot(nightVisionON);
                 NightVisionLight.active = true;
         }
         else if(Input.GetKeyDown("g")&&(isNightVision == true))
         {
                 isNightVision = false;
                 batteryPower = 0;
                 audio.PlayOneShot(nightVisionOFF);
                 NightVisionLight.active = false;
 }
 if(batteryPower > 0){
 batteryPower -= Time.deltaTime;
 }
 if(batteryPower <= 1){ 
 (isNightVision) = false;
 NightVisionLight.active = false;
 }
 if(batteryPower <= 0.5){ 
 (batteryPower) = 99;
 }
 }
 function OnGUI()
 {
         if(isNightVision == true)
         {
                 colPreviousGUIColor = GUI.color;
                 GUI.color = new Color(colPreviousGUIColor.r, colPreviousGUIColor.g, colPreviousGUIColor.b, fAlpha);
                 GUI.DrawTexture(Rect(0.0f, 0.0f, Screen.width, Screen.height), texture);
 
 GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), batteryTexture);
 if(batteryPower == 80);
 renderer.material.mainTexture = battery80;
 GUI.EndGroup();
 }
 }
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

1 Reply

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

Answer by aldonaletto · Aug 27, 2012 at 01:23 AM

If I understood correctly, you want to draw a different batteryTexture for each range (10, 35, 50, 65, 80, 100) - am I right? If so, compare the battery charge to the limits, selecting the higher texture until the battery charge falls below the next lower texture limit - like this:

...
function OnGUI()
{
        if(isNightVision == true)
        {
                colPreviousGUIColor = GUI.color;
                GUI.color = new Color(colPreviousGUIColor.r, colPreviousGUIColor.g, colPreviousGUIColor.b, fAlpha);
                GUI.DrawTexture(Rect(0.0f, 0.0f, Screen.width, Screen.height), texture);
                // compare the batteryPower to the limits to select the texture:
                if (batteryPower > 80) // from 80 to 100...
                    batteryTexture = battery100; // select battery100
                else if (batteryPower > 65) // from 65 to 80...
                    batteryTexture = battery80; // select battery80, and so on
                else if (batteryPower > 50)
                    batteryTexture = battery65;
                else if (batteryPower > 35)
                    batteryTexture = battery50;
                else if (batteryPower > 10)
                    batteryTexture = battery35;
                else
                    batteryTexture = battery10;
                // draw the selected texture:
                GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), batteryTexture);
        }
}
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 zero84085 · Aug 27, 2012 at 01:57 AM 0
Share

thanks, you are great not only you paste the code you make but also you explain it with the // ... 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

8 People are following this question.

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

Related Questions

Change Alpha on individual Texture2Ds 1 Answer

Best way to show an image 2 Answers

GUI Overlay Display 0 Answers

Is there a way to import png images with TextureType = GUI automatically? 1 Answer

Using Grayscale Textures For Transparency 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