Unity 绘制人物头顶的血条

来源:互联网 发布:微软笔记软件 编辑:程序博客网 时间:2024/04/19 19:42


using UnityEngine;using System.Collections;public class EnemyBlood : MonoBehaviour {public Texture blood_red;public Texture blood_black;private GameObject enemy;private Camera camera1;private int HP = 100;float enemyHeight; // Use this for initializationvoid Start () {enemy = GameObject.Find("Enemy");camera1 = Camera.main;float size_y = enemy.collider.bounds.size.y;float scal_y = enemy.transform.localScale.y;enemyHeight = size_y * scal_y;}void OnGUI(){Vector3 worldPosition = new Vector3(transform.position.x,transform.position.y + enemyHeight,transform.position.z);Vector2 position = camera1.WorldToScreenPoint(worldPosition);position = new Vector2(position.x,Screen.height - position.y);int blood_width = blood_red.width * HP / 100;GUI.DrawTexture(new Rect(position.x - 20,position.y - 18 ,40,6),blood_black);GUI.DrawTexture(new Rect(position.x - 20,position.y - 18,40,6),blood_red);}}






using UnityEngine;using System.Collections;public class EnemyBlood : MonoBehaviour {//红色血条的纹理public Texture blood_red;//黑色血条的纹理public Texture blood_black;//敌人对象private GameObject enemy;//获取摄像机private Camera camera1;//血量private int HP = 100;//敌人的身高float enemyHeight; // Use this for initializationvoid Start () {//获取敌人对象enemy = GameObject.Find("Enemy");//摄像机对象camera1 = Camera.main;//敌人的y值float size_y = enemy.collider.bounds.size.y;//y轴的缩放比例float scal_y = enemy.transform.localScale.y;//具体的身高enemyHeight = size_y * scal_y;}void OnGUI(){//敌人在3D世界中的坐标,默认的坐标在脚底下所以要加上身高Vector3 worldPosition = new Vector3(transform.position.x,transform.position.y + enemyHeight,transform.position.z);//将3D坐标转换为2D坐标,Enemy头顶的坐标。Vector2 position = camera1.WorldToScreenPoint(worldPosition);//得到真实的坐标3D坐标的原点在坐下角而,屏幕坐标的左上角。position = new Vector2(position.x,Screen.height - position.y);int blood_width = blood_red.width * HP / 100;//绘制GUI.DrawTexture(new Rect(position.x - 25,position.y - 6 ,50,6),blood_black);GUI.DrawTexture(new Rect(position.x - 25,position.y - 6,50,6),blood_red);}}




原创粉丝点击