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 () { } }
相关文章推荐
- Unity 绘制人物头顶的血条
- Unity中如何用代码实现横版游戏路径的绘制
- Unity学习之绘制血条及仇恨距离
- Unity-使用GUI绘制进度条或者血条
- Unity 代码碎片敌人的AI
- Unity 安卓项目 接入调用java代码
- Unity新项目如何快速理清顶层代码结构
- Unity公布4.6版本UI代码开源项目参与指南
- unity项目优化--绘制调用批处理unity3d Draw Call Batching
- Unity5.1 创建IOS的Xcode项目. 如何手动增量更新应用的资源和代码.
- unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开
- Unity动态绘制曲线Mesh的代码
- [UE4][Canvas]用C++代码绘制血条(HealthBar)
- Unity项目中使用Git管理代码的设置步骤
- 统计项目的java代码总行数
- unity项目去除unity开始动画和发布设置
- 贴吧项目(struts+hibernate)--界面代码
- GitHub上更新原有的项目代码(二)
- git_上传项目代码到github
- 为Unity项目生成文档(一)