读书人

unity3d容易血条的制作方法

发布时间: 2013-10-08 17:08:58 作者: rapoo

unity3d简单血条的制作方法

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

unity3d容易血条的制作方法

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);    }}



其中health是角色的生命脚本对象,maxhealth-最大血量,currenthealth是当前血量

读书人网 >软件开发

热点推荐