Unity makes circular cooling

The first is the effect display: 

In addition, the effect of the ring-shaped cooling strip is similar, if necessary, you can pay attention to the author for the next article. I won't go into details here. .

 

Note: I wrote this article mainly to record myself, and also to learn from friends in need. If there are any incomprehensions or deficiencies, readers can send messages to correct them. Thanks, thanks. . . . .

The following is an introduction to the cooling effect of the main gray circle

The key to realize this function is to understand the filling method of the image, add an image on the button, the name is coolingimg, the color of the image is changed from a circle to gray, and then control the Fill Amount parameter under the image component by timing.

 Button structure under Hierarchy. reload is a button, buttonimg is the button image, and coolingimg is the cooling fill image.

 

       Inspector property interface of coolingimg under the reload button:

reload button, Inspector property interface. The script is mounted here.

 

 The script details are as follows:

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

public class Coolingtime : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject player;
    private Animator Anim;//动画
    private float currentTime; //
    private Image coolingImage;
    private float coolingTimer;
    public GameObject fire;
    public GameObject button;

    public int magazinenum;//子弹数
    public int currnum;//当前子弹数
    public Text magazine;
    private string magazinevalue;
    public int weapon;

    // Use this for initialization
    void Start()
    {
        coolingImage = transform.GetChild(1).GetComponent<Image>();//获取需要冷却的灰色图片
        coolingImage.raycastTarget = false;//内存优化
        coolingImage.fillAmount = 0.0f;//初始化零填充冷却图片,点击按钮后才会出现 
    }
    void Awake()
    {
        Anim = player.GetComponent<Animator>();//玩家动作动画
    }
    // Update is called once per frame
    void Update()
    {
        weapon = button.GetComponent<WeaponControl>().weapon;//获取玩家当前所持武器
        Debug.Log(weapon);
        UpdateImage();//填充图片
        magazinevalue = currnum + "/" + magazinenum;//子弹数显示
        magazine.text = magazinevalue;//子弹数显示
    }

    public void OnBtnClickSkill(float timer)//按钮点击事件,参数冷却时间
    {
        if(currnum > 0)//判断是否还有弹夹
        {
            //初始化按钮事件
            currnum -= 1;
            coolingTimer = timer;//冷却时间
            currentTime = 0.0f;
            coolingImage.fillAmount = 1.0f;//填充度
        }
        else
        {
            currnum = 0;
        }
        
    }

    private void UpdateImage()//冷却图片填充
    {
        if (currentTime < coolingTimer)
        {
            currentTime += Time.deltaTime;//记录当前时间
            coolingImage.fillAmount = 1 - currentTime / coolingTimer;//倒计时,填充度为1 - currentTime / coolingTimer;当前时间占比

            if (coolingImage.fillAmount != 0)//
            {
                coolingImage.raycastTarget = true;
                Anim.SetBool("Reload", true);//播放换弹动作
            }
            else//冷却后执行的事件
            {
                
                if (weapon == 0)//判断所持武器
                {
                    fire.transform.gameObject.GetComponent<PlayerFire>().currnum = fire.transform.gameObject.GetComponent<PlayerFire>().cartridgenum;//换弹
                    Anim.SetBool("Reload", false);

                }
                else if (weapon == 1)
                {
                    fire.transform.gameObject.GetComponent<PlayerFire>().pistolcurrnum = fire.transform.gameObject.GetComponent<PlayerFire>().pistolcartridgenum;
                    Anim.SetBool("Reload", false);
                }
                coolingImage.raycastTarget = false;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_57282862/article/details/126068249