unity人物血条

效果图


这次用的是上次虚拟摇杆的工程文件,需要的可去下载

1、新建一个PlayerHup的c#脚本,并把它拖给人物模型,代码如下

using UnityEngine;
using System.Collections;

public class PlayerHup : MonoBehaviour {
    public Texture2D Bar_bg, Bar_hp, Bar_sp;
    public float HPmax = 100;
    public float HP = 50;
    public float SPmax = 100;
    public float SP = 50;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnGUI()
    {
        Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position); //把当前的坐标转成世界位标
        GUI.BeginGroup(new Rect(screenPos.x-42, Screen.height-screenPos.y+10, 84, 28));  
        GUI.DrawTexture(new Rect(0, 0, 84, 9), Bar_bg); //血条背景
        GUI.DrawTexture(new Rect(2, 2, (80.0f / HPmax) * HP, 5), Bar_hp); //血条
        GUI.DrawTexture(new Rect(0, 9, 84, 7), Bar_bg); //魔法背景
        GUI.DrawTexture(new Rect(2, 9, (80.0f / SPmax) * SP, 5), Bar_sp); //魔法
        GUI.EndGroup();
    }
}

2、给 Bar_bg, Bar_hp, Bar_sp赋值,把资源文拖给它们就可以了,如图


3、运行项目就可以看到人物的血条了,血条的位置可以自己调整

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值