- Home /
Solved by PMMmmpies in the comments.
UI Button Instead of OnGUI Button?
How would I make this to use the new UI buttons instead of the OnGUI buttons?
function OnGUI () {
var guiTime = time;
var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
//text.Time is the time that will be displayed.
GetComponent(GUIText).text = textTime;
if (GUI.Button(Rect(500,55,50,30), buttonTexture, buttonText)){
timerOn = !timerOn;
if(timerOn) buttonText = "Stop";
else buttonText = "Start";
audio.PlayOneShot(beep1);
}
if (GUI.Button(Rect(555,55,50,30), buttonTexture, "Reset")){
time = 0;
audio.PlayOneShot(beep2);
}
}
Answer by DiegoSLTS · Feb 14, 2015 at 06:21 PM
I'll try to tell you my approach, but it's hard to explain only with words. Also, some information is missing (you obviously have more than 2 buttons, so I'll assume you also have a Text object somewhere to display the actual time)
I'd make 2 scripts, one for each button.
For the Start/Stop button:
Add a reference to the Text object that should display the formatted time.
Add a public property to store the elapsed time.
The lines where you create a string with the current time expressed as hh:mm:ss would be inside a "if (timerOn) { .. }" block inside the Update method. After getting the string, instead of searchin for a GUIText component, use the Text object's reference.
The lines that change the state of the timer and the text on the button ("Stop" or "Start") would be on a public method called "OnClick" or something like that.
Set the "On Click" callback on the inspector to call the OnClick method.
For the Reset button:
Add a reference to the Stop/Start button.
Add an "OnClick" method that sets the StopStart's elapsed time property to 0 and plays the sound.
In the inspector, set the On Click for this button to that OnClick method.
Thanks DiegoSLTS, Nope there are just the two buttons, and there is a text object. I think I follow what you mean by making two scripts, just not sure how they should be made. It's not over complicated but I have trouble understanding the differences between the new UI method VS the old OnGUI stuff? This is the only think missing from my code snippet:
#pragma strict
private var time : float;
var textTime : String;
var timerOn : boolean;
var buttonText : String;
var beep1 : AudioClip;
var beep2 : AudioClip;
var buttonTexture: Texture2D;
function Start() {
timerOn = false;
buttonText = "Start";
}
function Update(){
if(timerOn)
time += Time.deltaTime;
}
$$anonymous$$y JS is poor but create a public function for each buttons actions. Drag the script onto one of the buttons, in that buttons inspector go to the OnClick area and click + and a slot appears, drag the button with the script on it to the slot and from the drop down to the right select $$anonymous$$yScriptName -> $$anonymous$$yFunctionName.
You don't really need two script just two functions, you don't even need the script on both buttons you just need to be able to put the script on a UI element/GameObject so you can drag it across to that slot and select whichever function applies to that button.
Thanks $$anonymous$$mmpies, But how would I write those function from all of this on GUI stuff?
Well my JS is really very poor but...
public function TimerOnOff()
{
// do your timer on/off stuff
}
public function TimerReset()
{
// do your timer reset stuff
}
I think all JS variables are public unless stated otherwise. So set a GameObject var for the Text (call it $$anonymous$$yText) and drag that Text UI item onto the script slot. Then use GetComponent to find the UI Text component and set its value.
Ahh if it were C# I could be more use.
Thanks $$anonymous$$mmpies, I'll see if I can make heads or tails of this, $$anonymous$$y Code guy would much prefer if this was C# as that's what he knows best, just sucks that the one I'm using is Java, it works but I need it to work with the UI buttons and not OnGUI buttons ;)