技能icon的原理和实现方法

UGUI简单版

直接创建一个Image,选择图片,修改aplah值为150左右。在此Image下再创建一个子Image(Image可以命名为FillImage来区分),选择原来的图片,Type选择Fill,添加脚本。完成

添加鼠标点击事件调用DoSkill函数也十分简单,直接在子Image下添加一个EventTrigger组件,选择Add new Event, 选择PointerDown,然后选择子Image物体中脚本函数即可。

 

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

 

public class Simple_CD : MonoBehaviour {

 

private bool finished = true;

private Image fillImage;

void Start () {

fillImage = this.GetComponent<Image>();

}

void Update () {

if(Input.GetKeyDown(KeyCode.F))

{

DoSkill();

}

}

public void DoSkill()

{

if(finished)

{

finished = false;

StartCoroutine(SetSkillValue());

}

}

IEnumerator SetSkillValue()

{

for(int i = 0; i <= 100; i++)

{

fillImage.fillAmount = i*0.01f;

yield return new WaitForSeconds(0.01f);

}

finished = true;

}

}

 

 

技能icon进化版:

前面过程和上面差不多,再在子Image(就是FillImage)下创建一个Text来显示倒计时。在FillImage的脚本中控制Text倒计时的时间显示。控制脚本如下:

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

 

public class test : MonoBehaviour {

 

public Text timerText;

public float coldTime;

private float timer;

private Image fillImage;

private bool isStartTimer = false;

void Start ()

{

fillImage = GameObject.Find("FillImage").GetComponent<Image>();

coldTime = 5f;

timer = coldTime;

timerText.enabled = false;

}

void Update () {

if(Input.GetKeyDown(KeyCode.A))

{

isStartTimer = true;

}

if(isStartTimer)

DoSkill();

}

public void DoSkill()

{

if(isStartTimer)

{

timer -= Time.deltaTime;

fillImage.fillAmount = timer/coldTime;

timerText.enabled = true;

if(timer <= 0)

{

fillImage.fillAmount = 1;

timer = coldTime;

isStartTimer = false;

timerText.enabled = false;

}

timerText.text = Mathf.CeilToInt(timer).ToString();

}

}

public void OnClick()

{

isStartTimer = true;

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值