创建血条
血条的创建 1.使用Slider来控制血条的变化,将Fill改变颜色,将Slider中的Handle禁用,会发现Value最大时Fill没有填满,这时可以通过改变Fill的大小和位置将backgrand填满或直接将backgrand禁用。
代码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BleedChange : MonoBehaviour
{
public Text text;
public Slider slider;
private int MaxHp=100;
private int CurructHp;
public Image image;
private void Start()
{
CurructHp = MaxHp;
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
PlayerBleedChange(10);
}
if(Input.GetKeyDown(KeyCode.K))
{
PlayerBleedChange(20);
}
}
public void PlayerBleedChange(int damage)
{
CurructHp -= damage;
ShowBleed();
if(CurructHp<=20)
{
image.color = Color.red;
}
}
public void ShowBleed()
{
slider.value = CurructHp /(float) MaxHp;
text.text = 100 * slider.value + "/100";
}
}
|