Unity3D-制作人物血量条
来源:互联网 发布:时时彩合买源码 编辑:程序博客网 时间:2024/04/29 01:26
方法一、使用NGUI
Step0:导入NGUI包.创建一个UIRoot(2D)和一个Cube;
Step1:
Step2:设置参数
Step3:将下面代码挂到Cube上:
using UnityEngine;using System.Collections;public class Enemy : MonoBehaviour {public GameObject mSlider;//将前面创建的Slider拖到此处;private GameObject uiRoot;void Start () {uiRoot = GameObject.FindGameObjectWithTag ("UIRoot");}void Update () {Vector3 pos0 = transform.position;Vector3 pos1 = Camera.main.WorldToScreenPoint (pos0); //将屏幕坐标转换为NGUI相机的世界坐标。 mSlider.transform.position = UICamera.currentCamera.ScreenToWorldPoint (pos1) + new Vector3 (-0.4f, 0.3f, 0); //改变血量if (Input.GetKeyDown(KeyCode.A)) {mSlider.GetComponent<UISlider>().value += 0.1f;}if (Input.GetKeyDown(KeyCode.D)) {mSlider.GetComponent<UISlider>().value -= 0.1f;}}}
Step4:点击键盘A和D查看运行结果如图:
方法二、使用GUITexture
GUITexture是二维GUI中的纹理图片,在Scene视图中不能显示Texture的样子,它的样子只能在摄像机中看到。
你需要将下面代码挂到Cube上,再拖上对应的PNG图片。
using UnityEngine;using System.Collections;public class Enemy : MonoBehaviour {public GUITexture HPTexture;//下图红色部分public GUITexture backgroundOfHPTexture;//下图白色部分public GUITexture charactorTexture;//下图太空人public float percentOfHP = 0.3f;//血量百分比// Update is called once per framevoid Update (){Vector3 pos = Camera.main.WorldToViewportPoint(transform.position); //GUITexture的位置是相对于摄像机屏幕的位置,左下角是(0,0,0)右上角(1,1,0)忽略Z坐标。backgroundOfHPTexture.transform.position = new Vector3 (pos.x-0.15f, pos.y+0.15f, 0); //设置相对于自身的坐标和大小。 backgroundOfHPTexture.pixelInset = new Rect(0,0,200f,10f);HPTexture.transform.position = new Vector3 (pos.x-0.15f, pos.y+0.15f, 1);HPTexture.pixelInset = new Rect(0,0,200f * percentOfHP,10f);charactorTexture.transform.position = new Vector3 (pos.x-0.15f, pos.y-0.1f, 1);charactorTexture.pixelInset = new Rect (0, 0, 128, 58);transform.Translate (new Vector3 (1f * Time.deltaTime,0,0 * Time.deltaTime ));}}
最终效果图:
1 0
- Unity3D-制作人物血量条
- Unity3D-血量条制作
- 血量条的制作
- 血量条
- UGUI 人物血条制作
- unity3d学习笔记(十九)--ngui制作人物头顶的头像和血条
- Unity3d制作虚拟按钮控制人物移动
- unity3d的血量减少和增加的数字显示制作(NGUI)
- Unity3d 血条/进度条制作
- Unity3d 血条/进度条制作
- Unity3D血条的制作
- cocos2dx 简单血量条实现
- unity3d制作弧形血条,魔条
- Unity3d人物的名称与血条的绘制方法
- unity3d创建一个跟随人物的血条
- Unity3D人物头顶名称与血条更新与绘制
- ngui制作人物头顶的头像和血条
- Unity3D 人形血条制作小知识
- 软件测试职业发展,到底该向何处?
- lua基础01
- MapReduce作业shuffle分析
- 男生从北大退学转读技校
- 【SSH三大框架】Hibernate基础第八篇:多对多关联关系的操作
- Unity3D-制作人物血量条
- python操作redis
- 使用selenium的时候firefox容易出现的问题
- js javascript判断是ios android设备
- java static 基础
- Oracle内存结构(buffer的header存在于SGA的the variable area中)
- J2EE的安全认证机制
- OC中 NSObject 对象常用的一些方法
- linux服务器操作手册