脚本挂在 image中
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ImageScript : MonoBehaviour {
//定义变量,存储image主件
Image ima;
//定义一个计时器(一开始就执行一次)
float timer = 1;
//定义一个CD值
public float CD = 5;
//定义显示的数字
float number = 0;
//声明一个游戏对象,存储Text (挂在image中)
public GameObject textObject;
//声明一个Test组件
Text textComponont;
void Start (){
//获取组件
ima = GetComponent<Image>();
textComponont = textObject.GetComponent<Text> ();
//程序执行的时候。图片正常显示,数字消失
ima.fillAmount = 1;
textObject.SetActive (false);
}
//判断技能是否处于冷却
bool isOpen = false;
void Update(){
//按R键执行, !isopen 保证技能实施时,R键不在作用
if (Input.GetKeyDown(KeyCode.R) && !isOpen) {
//开关
isOpen = true;
ima.fillAmount = 0;//图片置零
textObject.SetActive(true);//数字开始显示
number = CD; //数字显示时间与图片恢复时间一致
}
//技能效果状态
if (isOpen) {
ima.fillAmount += Time.deltaTime / CD; //技能恢复时间
ima.color = new Color (0.5f, 0.5f, 0.5f);//设置透明效果
//技能恢复
if (ima.fillAmount == 1) {
isOpen = false;//不在执行if语句
textObject.SetActive (false);//数字消失
textComponont.text = CD.ToString ();//只是为了让文本下次显示的数字是CD,但是不显示
}
timer += Time.deltaTime;
if (timer >= 1) {
timer = 0;//每一秒数字变化,置零
if (number >= 0) {
textComponont.text = number--.ToString ();//显示数字
}
}
} else {
ima.color = new Color (1f, 1f, 1f);//当技能实行完,技能没有了透明效果
}
}
}
// 以上是设置图片的透明效果达到目的,实际技能释放有3个对象,技能图片,技能释放时灰色图片,数字倒计时。