- Home /
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();
}
}
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); } }
thanks, you are great not only you paste the code you make but also you explain it with the // ... thanks again.
Your answer
Follow this Question
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