- Home /
GUI Testure Button Swap on MouseDown
I have this script I'm trying to get to work for an oxygen timer, it works fine but I'm trying to get a series of two Texture GUI buttons to swap out a different button texture on mouse down and swap back on mouseUp, but I'm not having a lot of luck figuring it out. I have it where the 2nd button state stays which is not what I want. here is my somewhat messy code... lol slowly getting messier the more I muck about with it.
var beep1 : AudioClip;
var beep2 : AudioClip;
var clockIsPaused : boolean = false;
var startTime : float; // (in sec)
var timeRemaining : float; // (in sec)
var percent : float;
var clockBG : Texture2D;
var clockFG : Texture2D;
var buttonTexture: Texture2D;
var miniButtonTexture : Texture2D;
var miniButtonTexture2 : Texture2D;
var clockBGMaxWidth : float; // the starting width of the foreground bar
var TextureButton1: Texture;
var TextureButton2: Texture;
function Start() {
timeRemaining = startTime;
}
function Awake()
{
TextureButton1 = miniButtonTexture;
TextureButton2 = miniButtonTexture;
//startTime = 20.0;
clockBGMaxWidth = clockFG.width * 1;
}
function Update ()
{
if(!clockIsPaused)
{
// make sure the timer is not paused
DOCountdown();
}
}
function OnGUI()
{
var newBarWidth : float = (percent/105) * clockBGMaxWidth; // this is the width that the foreground bar should be
GUI.BeginGroup (new Rect(0, 120, newBarWidth, clockBG.height));
GUI.DrawTexture (Rect (118, 0, clockBG.width * 1, clockBG.height), clockBG);
GUI.EndGroup ();
GUI.BeginGroup (new Rect(0, 0, clockFG.width * 1, clockFG.height));
GUI.DrawTexture (Rect (0, 0, clockFG.width * 1, clockFG.height), clockFG);
GUI.EndGroup();
if (GUI.Button(Rect(735,20,50,50), buttonTexture, "")){
Application.LoadLevel("ThreeD_Overview");
}
if (GUI.Button(Rect(345,427,64,32), TextureButton1, "")){
//Pause Timer
clockIsPaused = !clockIsPaused;
audio.PlayOneShot(beep1);
TextureButton1 = miniButtonTexture2; //Swap to Texture2
}
if (GUI.Button(Rect(420,427,64,32), TextureButton2, "")){
//Reset Timer
audio.PlayOneShot(beep2);
TextureButton2 = miniButtonTexture2; //Swap to Texture2
timeRemaining = startTime;
}
}
function DOCountdown()
{
//timeRemaining = startTime -Time.time;
timeRemaining -= Time.deltaTime;
percent = timeRemaining/startTime * 100;
if (timeRemaining < 0)
{
timeRemaining = 0;
clockIsPaused = true;
//TimeIsUp();
Debug.Log("Time is up!");
Application.LoadLevel ("KnightSpinningLogo_NoKITTintro");
}
//ShowTime();
}
$$anonymous$$aybe to make your life easier, you should start using the new 4.6 GUI system. This will allow you with great ease to have the effect you want, and can all be done in editor with $$anonymous$$imal code.
I would but I have difficulty trying to figure out how to make the new UI work with existing code like this.
It is really worth learning, the OnGUI system is very outdated. Check the unity documentation and lessons on the UI to learn. http://unity3d.com/learn/tutorials/modules/beginner/ui Start on the Canvas lesson.
Your answer