Unity-使用GUI绘制进度条或者血条

横向:

纵向:Unity-使用GUI绘制进度条或者血条_第1张图片

将下面的脚本挂在camera上,并创建一个GUI Skin;

using UnityEngine;
using System.Collections;

public class BloodBarTest : MonoBehaviour
{
    public GUISkin theSkin;
    public float bloodValue = 0.0f;
    private float tmpValue;
    private Rect rctBloodBar;
    private Rect rctUpButton;
    private Rect rctDownButton;
    private bool onoff;

    void Start()
    {
        //血条或者进度条-纵向
        rctBloodBar = new Rect(20, 20, 20, 200);
        rctUpButton = new Rect(50, 20, 40, 20);
        rctDownButton = new Rect(50, 50, 40, 20);
        
        ////血条或者进度条-横向
        //rctBloodBar = new Rect(20, 20, 200, 20);
        ////加血-按钮
        //rctUpButton = new Rect(20, 50, 40, 20);
        ////减血-按钮
        //rctDownButton = new Rect(70, 50, 40, 20);
        //实时获取HP
        tmpValue = bloodValue;
    }

    void OnGUI()
    {
        GUI.skin = theSkin;
        if (GUI.Button(rctUpButton, "加血"))
        {
            tmpValue = -1.0f;
        }
        if (GUI.Button(rctDownButton, "减血"))
        {
            tmpValue += 0.1f;
        }
        if (bloodValue > 0.0f) tmpValue = 0.0f;
        if (bloodValue < -1.0f) tmpValue = -1.0f;
        //插值计算HP值
        bloodValue = Mathf.Lerp(bloodValue, tmpValue, 0.05f);
        GUI.VerticalScrollbar(rctBloodBar, 1.0f, bloodValue, 0.0f, 1.0f, GUI.skin.GetStyle("verticalScrollbar"));
        //GUI.HorizontalScrollbar(rctBloodBar, 1.0f, bloodValue, 1.0f, 0.0f, GUI.skin.GetStyle("horizontalscrollbar"));
    }
}


你可能感兴趣的:(GUI,unity,进度条或血条)