用户登录
用户注册

分享至

unity血条

  • 作者: 嗫?暁雲?
  • 来源: 51数据库
  • 2020-09-21
通过修改贴图的宽度来实现进度条的功能,以血条为例,若100%生命值时HealthBar的贴图宽度为Width, 当生命值为x%时,只要修改HealthBar的宽度为Width*x%即可。

HealthBar.pixelInset = new Rect()

{

x = HealthBar.pixelInset.x,

y = HealthBar.pixelInset.y,

width = m_StandardHealth



  修改一下,基本上代码差不多可以直接用了。看一看你很容易就懂了。
public texture2d blood_red;
public texture2d blood_black;
public int hp = 100;
void ongui(){
//得到npc头顶在3d世界中的坐标
//默认npc坐标点在脚底下,所以这里加上npcheight它模型的高度即可
vector3 worldposition = new vector3 (transform.position.x , transform.position.y + playerheight,transform.position.z);
//根据npc头顶的3d坐标换算成它在2d屏幕中的坐标
vector2 position = camera.worldtoscreenpoint (worldposition);
//得到真实npc头顶的2d坐标
position = new vector2 (position.x, screen.height - position.y);
//注解2
//计算出血条的宽高
vector2 bloodsize = gui.skin.label.calcsize (new guicontent(blood_red));

if (hp < 0)="" hp="0;" 通过血值计算红色血条显示区域="" int="" blood_width="blood_red.width" *="" 100/100;="" 先绘制黑色血条="" gui.drawtexture(new="" rect(position.x="" -="" (bloodsize.x/3),position.y="" -="" bloodsize.y="" ,bloodsize.x-bloodsize.x/3,bloodsize.y/2),blood_black);="" 在绘制红色血条="" gui.drawtexture(new="" rect(position.x="" -="" (bloodsize.x/3),position.y="" -="" bloodsize.y="" ,(blood_width-bloodsize.x/3)*="" hp/100,bloodsize.y/2),blood_red);="" vector3="" worldposition2="new" vector3="" (m_men.position.x="" ,="" m_men.position.y="" +="" 5,m_men.position.z);="" 根据npc头顶的3d坐标换算成它在2d屏幕中的坐标="" vector2="" position2="camera.worldtoscreenpoint" (worldposition2);="" 得到真实npc头顶的2d坐标="" position2="new" vector2="" (position2.x,="" screen.height="" -="" position2.y);="" 注解2="" 计算出血条的宽高="" vector2="" bloodsize2="gui.skin.label.calcsize" (new="" guicontent(blood_red));="" int="" hp2="">().hp;
if (hp2 < 0)="" hp2="0;" 通过血值计算红色血条显示区域="" int="" blood_width2="blood_red.width" *="" 100/100;="" 先绘制黑色血条="" gui.drawtexture(new="" rect(position2.x="" -="" (bloodsize.x/3),position2.y="" -="" bloodsize2.y="" ,bloodsize2.x-bloodsize2.x/3,bloodsize2.y/2),blood_black);="" 在绘制红色血条="" gui.drawtexture(new="" rect(position2.x="" -="" (bloodsize.x/3),position2.y="" -="" bloodsize2.y="" ,(blood_width2-bloodsize2.x/3)*="" hp2/100,bloodsize2.y/2),blood_red);="" 注解3="" 计算npc名称的宽高="" vector2="" namesize="gui.skin.label.calcsize" (new="" guicontent(name));="" 设置显示颜色为黄色="" gui.color="color.yellow;" 绘制npc名称="" gui.label(new="" rect(="" screen.width*2/5,="" screen.height/20,="" screen.width*2/5+screen.width/10,="" screen.height/20+screen.height/10),="" "round="" "+m_begintime);="" gui.label(new="" rect(="" screen.width*4/5,="" screen.height/20,="" screen.width*4/5+screen.width/10,="" screen.height/20+screen.height/10),="" "player="" "+m_winplayer);="" gui.label(new="" rect(="" screen.width*0/5,="" screen.height/20,="" screen.width*1/5+screen.width/10,="" screen.height/20+screen.height/10),="" "men="" "+m_winmen);="">
软件
前端设计
程序设计
Java相关