前提知识
这里没有使用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;
}
}