unity3d简单血条的制作方法
通过gui的GUI.DrawTexture方法来实现血条,如下图:
using UnityEngine;using System.Collections;/// <summary>/// 血条/// 小伍 QQ:16349023/// </summary>public class Wy2HealthBar : MonoBehaviour { public Texture2D HealthBg; public Texture2D Heathforce; public Vector2 offset = new Vector2(13,15); private Wy2AIHealth health; void Setup() { health = GetComponent<Wy2AIHealth>(); } void Reset() { Setup(); }// Use this for initializationvoid Start () { Setup();}// Update is called once per framevoid Update () {} void OnGUI() { if (Event.current.type != EventType.Repaint) { return; } Rect rectbg=new Rect(0,0,256,64); GUI.DrawTexture(rectbg, HealthBg); float width = (health.CurrentHealth*145) / health.MaxHealth; if (width < 1) return; Rect rectfc = new Rect(offset.x, offset.y, width, 10); GUI.DrawTexture(rectfc, Heathforce); }}