下面来分享一下unity3d 制作物体血条的具体流程怎么弄?废话不多说,下面直接就上图和代码:
通过gui的GUI.DrawTexture方法来实现血条,如下图:
- using UnityEngine;
- using System.Collections;
-
- /// <summary>
- /// 血条
- /// </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 initialization
- void Start () {
- Setup();
- }
-
- // Update is called once per frame
- void 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);
- }
- }