找回密码
 立即注册
查看: 5747|回复: 75

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

[复制链接]
发表于 2013-3-5 18:53 | 显示全部楼层 |阅读模式
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上即可.
发表于 2017-4-5 21:30 | 显示全部楼层
好帖就是要顶
发表于 2017-4-5 21:39 | 显示全部楼层
真心顶
发表于 2017-4-5 20:58 | 显示全部楼层
说的非常好
发表于 2017-4-5 21:22 | 显示全部楼层
很好哦
发表于 2017-4-5 21:47 | 显示全部楼层
不错不错
发表于 2017-4-15 07:03 | 显示全部楼层
好帖就是要顶
发表于 2017-4-15 07:00 | 显示全部楼层
顶顶多好
发表于 2017-4-15 07:11 | 显示全部楼层
说的非常好
发表于 2017-4-15 07:05 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-3 23:02 , Processed in 0.819724 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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