unity3d简单血条的制作方法

来源:互联网 发布:用cmd运行java 编辑:程序博客网 时间:2024/06/10 08:51

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

 

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. /// <summary>  
  5. /// 血条  
  6. /// 小伍 QQ:16349023  
  7. /// </summary>  
  8. public class Wy2HealthBar : MonoBehaviour {  
  9.   
  10.     public Texture2D HealthBg;  
  11.     public Texture2D Heathforce;  
  12.   
  13.     public Vector2 offset = new Vector2(13,15);  
  14.   
  15.     private Wy2AIHealth health;  
  16.   
  17.   
  18.   
  19.     void Setup()  
  20.     {  
  21.         health = GetComponent<Wy2AIHealth>();  
  22.     }  
  23.   
  24.     void Reset()  
  25.     {  
  26.         Setup();  
  27.     }  
  28.   
  29.     // Use this for initialization  
  30.     void Start () {  
  31.         Setup();  
  32.     }  
  33.       
  34.     // Update is called once per frame  
  35.     void Update () {  
  36.       
  37.     }  
  38.   
  39.     void OnGUI()  
  40.     {  
  41.         if (Event.current.type != EventType.Repaint)  
  42.         {  
  43.             return;  
  44.         }  
  45.         Rect rectbg=new Rect(0,0,256,64);  
  46.   
  47.         GUI.DrawTexture(rectbg, HealthBg);  
  48.   
  49.         float width = (health.CurrentHealth*145) / health.MaxHealth;  
  50.   
  51.         if (width < 1) return;  
  52.   
  53.         Rect rectfc = new Rect(offset.x, offset.y, width, 10);  
  54.         GUI.DrawTexture(rectfc, Heathforce);  
  55.     }  
  56. }  



 

其中health是角色的生命脚本对象,maxhealth-最大血量,currenthealth是当前血量
0 0
原创粉丝点击