unity3d简单血条的制作方法

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

 

[csharp]  view plain copy
  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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值