您的位置:首页 > 移动开发 > Unity3D

Unity 项目中绘制血条的代码碎片

2013-07-30 22:26 381 查看
using UnityEngine;
using System.Collections;

public class BloodCS : MonoBehaviour {
	
	
	public Texture blood_red;
	public Texture blood_black;
	private GameObject hero;
	
	
	private int HP = 100;
	
	float enemyHeight;
	
	private const int AI_ATTACK_DISTANCE = 10;
	

	
	// Use this for initialization
	void Start () {
		hero = GameObject.Find("Hero");
		blood_red = (Texture)Resources.Load("blood_red");
		blood_black = (Texture)Resources.Load("blood_black");
	
		float size_y = gameObject.collider.bounds.size.y;
		float scal_y = gameObject.transform.localScale.y;
		
		enemyHeight = size_y * scal_y;
	}
	
	void OnGUI()
	{
		Vector3 worldPosition = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y + enemyHeight,gameObject.transform.position.z);
	
		Vector2 position = Camera.main.WorldToScreenPoint(worldPosition);
		
		position = new Vector2(position.x,Screen.height - position.y);
		
		int blood_width = 40 * HP / 100;
		
		if(Vector3.Distance(gameObject.transform.position,hero.transform.position) <= AI_ATTACK_DISTANCE)
		{
			DataModel.blood_show = true;
		}
		
		if(gameObject.name == "Hero")
		{
			DataModel.blood_show = true;
		}
		
		
		if(DataModel.blood_show)
		{
		
		GUI.DrawTexture(new Rect(position.x - 20,position.y ,40,6),blood_black);
		GUI.DrawTexture(new Rect(position.x - 20,position.y ,blood_width,6),blood_red);
			DataModel.blood_show = false;
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: