永发信息网

unity怎么实现血条颜色变化

答案:2  悬赏:20  手机版
解决时间 2021-02-15 02:33
  • 提问者网友:姑娘长的好罪过
  • 2021-02-14 03:52
unity怎么实现血条颜色变化
最佳答案
  • 五星知识达人网友:慢性怪人
  • 2021-02-14 05:08
通过修改贴图的宽度来实现进度条的功能,以血条为例,若100%生命值时HealthBar的贴图宽度为Width, 当生命值为x%时,只要修改HealthBar的宽度为Width*x%即可。

HealthBar.pixelInset = new Rect()

{

x = HealthBar.pixelInset.x,

y = HealthBar.pixelInset.y,

width = m_StandardHealth
全部回答
  • 1楼网友:山君与见山
  • 2021-02-14 06:30
修改一下,基本上代码差不多可以直接用了。看一看你很容易就懂了。 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 = m_men.getcomponent().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); }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯