找回密码
 立即注册
查看: 4772|回复: 76

[网络] 实现网络中游戏对象的血量值的同步

[复制链接]
发表于 2012-6-14 23:09 | 显示全部楼层 |阅读模式

1.把NetworkView附加到GameObject(游戏物体),假设GameObject的名为Plyaer

2.把下面代码命名为HealthText附加到Player上.
  1. var currentHealth : int;
  2. var healthText : GameObject;

  3. function Update () {
  4.         healthText.GetComponent(TextMesh).text=currentHealth.ToString();
  5.         if(Input.GetButtonDown("Fire1") && networkView.isMine){
  6.                 if(currentHealth>0)
  7.                 {
  8.                         currentHealth-=10;
  9.                 }
  10.                 else{
  11.                         currentHealth=200;
  12.                 }
  13.                
  14.         }
  15. }


  16. function OnSerializeNetworkView(stream : BitStream,info : NetworkMessageInfo){//用来在一个由网络视图监控的脚本中自定义变量同步
  17.         var health : int =0;
  18.         if(stream.isWriting){//比特流是否正在被写入
  19.                 health=currentHealth;
  20.                 stream.Serialize(health);//序列化不同类型的变量
  21.         } else {
  22.                 stream.Serialize(health);
  23.                 currentHealth=health;
  24.         }
  25. }
复制代码
3.然后拖动HealthText.js到NetworkView上的Observed上即可.
{:soso__1243193949118710676_4:}
发表于 2014-5-15 14:12 | 显示全部楼层
好好 学习了 学习了
发表于 2017-2-28 19:26 | 显示全部楼层
很不错
发表于 2017-2-28 19:29 | 显示全部楼层
楼主是超人
发表于 2017-2-28 19:07 | 显示全部楼层
顶顶多好
发表于 2017-2-28 19:16 | 显示全部楼层
难得一见的好帖
发表于 2017-2-28 19:19 | 显示全部楼层
很好哦
发表于 2017-3-14 21:29 | 显示全部楼层
很不错
发表于 2017-3-14 21:49 | 显示全部楼层
楼主是超人
发表于 2017-3-14 22:04 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-28 02:06 , Processed in 0.093239 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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