unity技能刷新效果实现

前提知识
这里没有使用slider,而是使用的Image Type中的Filled

1.按名字得到子控件中的gameobject

	private Image _runImage;
	_runImage = transform.Find("RunImage").GetComponent<Image>();

2.某个按键按下

	public KeyCode Key;
    if (Input.GetKeyDown(Key))
    {
        _isStart = true;
    }

3.改变Filled的Fill Amount属性

	_runImage.fillAmount = (CodeTime - _passTime) / CodeTime;

技能刷新的全部代码:

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

public class Skill : MonoBehaviour
{

    public float CodeTime = 2;

    public KeyCode Key;

    private float _passTime = 0;

    private bool _isStart = false;

    private Image _runImage;
    // Start is called before the first frame update
    void Start()
    {
        _runImage = transform.Find("RunImage").GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(Key))
        {
            _isStart = true;
        }
        if (_isStart)
        {
            _passTime += Time.deltaTime;
            _runImage.fillAmount = (CodeTime - _passTime) / CodeTime;
            if (_passTime >= CodeTime)
            {
                _runImage.fillAmount = 0;
                _isStart = false;
                _passTime = 0;
            }
        }
    }

    public void OnClick()
    {
        _isStart = true;
    }
}

在这里插入图片描述

发布了167 篇原创文章 · 获赞 179 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/104596325