效果图
这次用的是上次虚拟摇杆的工程文件,需要的可去下载
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、运行项目就可以看到人物的血条了,血条的位置可以自己调整