欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity实现技能冷却

程序员文章站 2022-04-03 08:35:42
...

效果如下:

Unity实现技能冷却

Unity实现技能冷却

using BoxWorldServer;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerSkillEffect : MonoBehaviour
{

    //技能按钮  
    private Button redBtn;
    private Button blueBtn;
    private Button bleedBtn;
    private Button runBtn;
    //技能剩余时间显示
    private Text redBtnCDTimeText;
    private Text blueBtnCDTimeText;
    private Text bleedBtnCDTimeText;
    private Text runBtnCDTimeText;
    //技能时间
    private float redBtnCDTime = 3f;
    private float blueBtnCDTime = 3f;
    private float bleedBtnCDTime = 10f;
    private float runBtnCDTime = 5f;

    //累加器
    private float redBtnTimes = 0f;
    private float blueBtnTimes = 0f;
    private float bleedBtnTimes = 0f;
    private float runBtnTimes = 0f;
    //是否遮罩
    [HideInInspector]
    public bool redBtnIsOn = false;
    [HideInInspector]
    public bool blueBtnIsOn = false;
    [HideInInspector]
    public bool bleedBtnIsOn = false;
    [HideInInspector]
    public bool runBtnIsOn = false;

    private void Awake()
    {
        //技能相关UI
        redBtn = GameObject.Find("RedBtn").GetComponent<Button>();
        blueBtn = GameObject.Find("BlueBtn").GetComponent<Button>();
        bleedBtn = GameObject.Find("BleedBtn").GetComponent<Button>();
        runBtn = GameObject.Find("RunBtn").GetComponent<Button>();
        redBtnCDTimeText = GameObject.Find("redBtnCDTimeText").GetComponent<Text>();
        blueBtnCDTimeText = GameObject.Find("blueBtnCDTimeText").GetComponent<Text>();
        bleedBtnCDTimeText = GameObject.Find("bleedBtnCDTimeText").GetComponent<Text>();
        runBtnCDTimeText = GameObject.Find("runBtnCDTimeText").GetComponent<Text>();

        //绑定按钮事件
        redBtn.onClick.AddListener(OnRedBtnClick);
        blueBtn.onClick.AddListener(OnBlueBtnClick);
        bleedBtn.onClick.AddListener(OnBleedBtnClick);
        runBtn.onClick.AddListener(OnRunBtnClick);

    }

    private void Update()
    {
        BtnIsOn();
        TriggerBtnInput();
    }
    //判断按钮是否冷却
    public void BtnIsOn()
    {
        if (redBtnIsOn)
        {
            redBtnTimes += Time.deltaTime;
            redBtn.image.fillAmount = redBtnTimes / redBtnCDTime;
            redBtnCDTimeText.text = Math.Round(redBtnCDTime - redBtnTimes, 1).ToString();
            if (redBtnTimes >= redBtnCDTime)
            {
                redBtnCDTimeText.text = null;
                redBtnIsOn = false;
                redBtn.image.fillAmount = 1;
                redBtnTimes = 0f;
                redBtn.interactable = true;
            }
        }
        if (blueBtnIsOn)
        {
            blueBtnTimes += Time.deltaTime;
            blueBtn.image.fillAmount = blueBtnTimes / blueBtnCDTime;
            blueBtnCDTimeText.text = Math.Round(blueBtnCDTime - blueBtnTimes, 1).ToString();
            if (blueBtnTimes >= blueBtnCDTime)
            {
                blueBtnCDTimeText.text = null;
                blueBtnIsOn = false;
                blueBtn.image.fillAmount = 1;
                blueBtnTimes = 0f;
                blueBtn.interactable = true;
            }
        }
        if (bleedBtnIsOn)
        {
            bleedBtnTimes += Time.deltaTime;
            bleedBtn.image.fillAmount = bleedBtnTimes / bleedBtnCDTime;
            bleedBtnCDTimeText.text = Math.Round(bleedBtnCDTime - bleedBtnTimes, 1).ToString();
            if (bleedBtnTimes >= bleedBtnCDTime)
            {
                bleedBtnCDTimeText.text = null;
                bleedBtnIsOn = false;
                bleedBtn.image.fillAmount = 1;
                bleedBtnTimes = 0f;
                bleedBtn.interactable = true;
            }
        }
        if (runBtnIsOn)
        {
            runBtnTimes += Time.deltaTime;
            runBtn.image.fillAmount = runBtnTimes / runBtnCDTime;
            runBtnCDTimeText.text = Math.Round(runBtnCDTime - runBtnTimes, 1).ToString();
            if (runBtnTimes >= runBtnCDTime)
            {
                runBtnCDTimeText.text = null;
                runBtnIsOn = false;
                runBtn.image.fillAmount = 1;
                runBtnTimes = 0f;
                runBtn.interactable = true;
            }
        }
    }
    //通过键盘按键释放技能
    public void TriggerBtnInput()
    {
        //技能播放
        if (Input.GetKey(KeyCode.Q))
        {
            OnRedBtnClick();
        }
        else if (Input.GetKey(KeyCode.E))
        {
            OnBlueBtnClick();
        }
        else if (Input.GetKey(KeyCode.R))
        {
            OnBleedBtnClick();
        }
        else if (Input.GetKey(KeyCode.T))
        {
            OnRunBtnClick();
        }
        //按下C键打开关闭聊天面板
        if(Input.GetKey(KeyCode.C))
        {
            fightPanel.OnShowChatPanelClick();
        }
    }
    //技能按钮事件
    public void OnRedBtnClick()
    {
        redBtnIsOn = true;
        redBtn.interactable = false;
    }
    public void OnBlueBtnClick()
    {
        blueBtnIsOn = true;
        bleedBtn.interactable = false;
    }
    public void OnBleedBtnClick()
    {
        bleedBtnIsOn = true;
        bleedBtn.interactable = false;
    }
    public void OnRunBtnClick()
    {
        runBtnIsOn = true;
        runBtn.interactable = false;
    }


}

 

相关标签: unity