下面来分享一下unity3d 制作物体血条的具体流程怎么弄?废话不多说,下面直接就上图和代码:

通过gui的GUI.DrawTexture方法来实现血条,如下图:

  1. using UnityEngine;
  2. using System.Collections;

  3. /// <summary>
  4. /// 血条
  5. /// </summary>
  6. public class Wy2HealthBar : MonoBehaviour {

  7.     public Texture2D HealthBg;
  8.     public Texture2D Heathforce;

  9.     public Vector2 offset = new Vector2(13,15);

  10.     private Wy2AIHealth health;



  11.     void Setup()
  12.     {
  13.         health = GetComponent<Wy2AIHealth>();
  14.     }

  15.     void Reset()
  16.     {
  17.         Setup();
  18.     }

  19. // Use this for initialization
  20. void Start () {
  21.         Setup();
  22. }

  23. // Update is called once per frame
  24. void Update () {

  25. }

  26.     void OnGUI()
  27.     {
  28.         if (Event.current.type != EventType.Repaint)
  29.         {
  30.             return;
  31.         }
  32.         Rect rectbg=new Rect(0,0,256,64);

  33.         GUI.DrawTexture(rectbg, HealthBg);

  34.         float width = (health.CurrentHealth*145) / health.MaxHealth;

  35.         if (width < 1) return;

  36.         Rect rectfc = new Rect(offset.x, offset.y, width, 10);
  37.         GUI.DrawTexture(rectfc, Heathforce);
  38.     }
  39. }