找回密码
 立即注册
查看: 5704|回复: 104

[GUI] Unity3D令人蛋疼的血条制作 还是共享了..

[复制链接]
发表于 2012-6-15 00:29 | 显示全部楼层 |阅读模式
首先在场景创建GUI Texture,拖动到你想让它出现的位置,取名“BloodLength”

看到这张图大家领悟了吧 没错 就是用图片拼接的方法实现血条血量

设置初始血条长度为100。
创建控制血条的脚本:
在脚本中我将人物碰撞及血量减少的方法放到了一起,下面来做详细解释:
  1. var player_ :GameObject;//获取自动寻路的角色
  2. var target_: Transform ;//获取你控制的角色
  3. var attackRange = 200.0;//碰撞发生的范围
复制代码
//player_hp包里面血条的各种形态(需导入)

  1. var h00:Texture2D;
  2. var h05:Texture2D;
  3. var h10:Texture2D;
  4. .......................................
  5. var h100:Texture2D;
复制代码
var animation_time:float;//依靠player出脚的时间来判断target应该减少的血量

var g_Health :GameObject;//获取GUI Texture
static var health = 100;//定义血量初始值
//判断血量,更改素材
  1. function DownBloodLength ()
  2. {
  3. g_Health = GameObject.Find("BloodLength");
  4. if(health > 95 )
  5. {
  6.   g_Health.guiTexture.texture = h100;
  7.   return;
  8. }
  9. else if(health > 90)
  10. {
  11.   g_Health.guiTexture.texture = h95;
  12.   return;
  13. }
  14. else if(health > 85)
  15. {
  16.   g_Health.guiTexture.texture = h90;
  17.   return;
  18. }
  19. else if(health > 80)
  20. {
  21.   g_Health.guiTexture.texture = h85;
  22.   return;
  23. }
  24. else if(health > 75)
  25. {
  26.   g_Health.guiTexture.texture = h80;
  27.   return;
  28. }
  29. else if(health > 70)
  30. {
  31.   g_Health.guiTexture.texture = h75;
  32.   return;
  33. }
  34. else if(health > 65)
  35. {
  36.   g_Health.guiTexture.texture = h70;
  37.   return;
  38. }
  39. else if(health > 60)
  40. {
  41.   g_Health.guiTexture.texture = h65;
  42.   return;
  43. }
  44. else if(health > 55)
  45. {
  46.   g_Health.guiTexture.texture = h60;
  47.   return;
  48. }
  49. else if(health > 50)
  50. {
  51.   g_Health.guiTexture.texture = h55;
  52.   return;
  53. }
  54. else if(health > 45)
  55. {
  56.   g_Health.guiTexture.texture = h50;
  57.   return;
  58. }
  59. else if(health > 40)
  60. {
  61.   g_Health.guiTexture.texture = h45;
  62.   return;
  63. }
  64. else if(health > 35)
  65. {
  66.   g_Health.guiTexture.texture = h40;
  67.   return;
  68. }
  69. else if(health > 30)
  70. {
  71.   g_Health.guiTexture.texture = h35;
  72.   return;
  73. }
  74. else if(health > 25)
  75. {
  76.   g_Health.guiTexture.texture = h30;
  77.   return;
  78. }
  79. else if(health > 20)
  80. {
  81.   g_Health.guiTexture.texture = h25;
  82.   return;
  83. }
  84. else if(health > 15)
  85. {
  86.   g_Health.guiTexture.texture = h20;
  87.   return;
  88. }
  89. else if(health > 10)
  90. {
  91.   g_Health.guiTexture.texture = h15;
  92.   return;
  93. }
  94. else if(health > 5)
  95. {
  96.   g_Health.guiTexture.texture = h10;
  97.   return;
  98. }
  99. else if(health > 0)
  100. {
  101.   g_Health.guiTexture.texture = h05;
  102.   return;
  103. }
  104. else if(health <= 0)
  105. {
  106.   g_Health.guiTexture.texture = h00;
  107.   return;
  108. }
  109. }
复制代码
  1. function Start()
  2. {
  3. animation.wrapMode = WrapMode.Loop;

  4. var guale = target_.animation["guale"];
  5. guale.wrapMode = WrapMode.Once;
  6. }
复制代码
  1. function Update () {
  2.       DownBloodLength ();
  3.       
  4.       if(target_ == null)
  5.                    return;

  6. //距离满足碰撞条件
  7. var Distance_CS :float = Vector3.Distance(player_.transform.position,target_.position);
  8. if(Distance_CS < attackRange)
  9. {
  10.   if(Distance_CS >12)
  11.   {
  12.    var targetPoint1 = target_.position;//当前Cube位置
  13.    player_.transform.rotation = Quaternion.LookRotation(targetPoint1 - transform.position,Vector3.up);
  14.    player_.animation.CrossFade("pao");
  15.   }
  16.   else if(Distance_CS <= 12)
  17.   {//如果2人物接近,player进行踢腿。
  18.    animation_time+=Time.deltaTime;
  19.    player_.animation.CrossFade("chujiao");
  20.   }
  21.   if(player_.animation.IsPlaying("chujiao"))
  22.   {  //在脚本中踢腿的动作完成需60帧,也就是两秒。
  23.       if(animation_time > 2)
  24.       {
  25.           health = health -Mathf.CeilToInt(animation_time/10); //进行减血。。。
  26.           if(health <= 0)
  27.           {
  28.            target_.animation.CrossFade("guale");
  29.           }
  30.       }      
  31.   }
  32. }
复制代码
完!{:soso__6235880048239246314_3:}






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

评分

参与人数 1鲜花 +1 收起 理由
yuaupei + 1 很给力!

查看全部评分

 楼主| 发表于 2012-6-15 00:30 | 显示全部楼层
这是一个老外写的 我们拓的{:soso__11564777293513085719_3:}
发表于 2012-11-13 11:09 | 显示全部楼层
多米诺 发表于 2012-6-15 00:30
这是一个老外写的 我们拓的

顶支持下{:5_439:}
发表于 2012-12-4 18:29 | 显示全部楼层
very good sample
发表于 2012-12-6 17:15 | 显示全部楼层
xcghfxhsfht
发表于 2012-12-17 14:55 | 显示全部楼层
看一下下 还要大于10个字
发表于 2012-12-17 17:04 | 显示全部楼层
这代码也太庸长了吧,还搞一大堆贴图,为啥不用一个贴图进行缩放来显示进度呢?
发表于 2012-12-24 11:17 | 显示全部楼层
新手顶一下!!
发表于 2012-12-24 14:55 | 显示全部楼层
下载看看!
发表于 2012-12-24 20:41 | 显示全部楼层
没有简便一点儿的方法吗?
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 22:22 , Processed in 0.102989 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表