I’m working with NGUI right now but I only really know Java. I found this script online which deals with a cooldown timer on a button.
using UnityEngine;
using System.Collections;
public class SkillButton : MonoBehaviour {
public float coolTimeMax = 0.5f;
float coolTime;
UISlider silder = null;
// Use this for initialization
void Start () {
silder = GetComponent<UISlider>();
coolTime = coolTimeMax;
}
public bool IsUseSkill() {
if (coolTime < 0f) {
coolTime = coolTimeMax;
return true;
}
return false;
}
public void Fire(){
if (IsUseSkill()) {
// player.SendMessage(fire);
}
}
void OnPress () {
Fire();
}
// Update is called once per frame
void Update () {
if (coolTime > 0f) {
coolTime -= Time.deltaTime;
float percent = 1f - coolTime/coolTimeMax;
silder.sliderValue = percent;
}
}
}
Now I kind of created my own, which does the same thing, but I feel mine is a lot less robust;
#pragma strict
var coolTimeMax: float = 0.5f;
private var timer: float = 0.0f;
private var coolTime: float = 0.0f;
private var silder: UISlider = null;
private var getCowCam: GameObject;
private var isUseSkill: boolean = false;
function Start ()
{
silder = GetComponent(UISlider);
StartTimer ();
getCowCam = GameObject.FindGameObjectWithTag("MainCamera");
}
function Update ()
{
silder.sliderValue = timer;
if (UICamera.hoveredObject != null && UICamera.hoveredObject.name == ("Progress Bar") && Input.GetMouseButton(0) && !isUseSkill && timer == 1)
{
isUseSkill = true;
getCowCam.GetComponent(InGameGUI).GetToLauncher ();
}
if (isUseSkill)
{
StartTimer ();
isUseSkill = false;
}
}
function StartTimer ()
{
var t: float = 0.0f;
while (t < 1)
{
t += Time.deltaTime / coolTimeMax;
timer = Mathf.Lerp (0, 1, t);
yield;
}
}
The main difference is that he can just drag and drop his code onto any button and it works.
My click check ended up having so many conditional variables that it makes my script button specific. So I’d like to see what he did that I didn’t