多米诺 发表于 2012-6-14 23:09

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


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

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

function Update () {
      healthText.GetComponent(TextMesh).text=currentHealth.ToString();
      if(Input.GetButtonDown("Fire1") && networkView.isMine){
                if(currentHealth>0)
                {
                        currentHealth-=10;
                }
                else{
                        currentHealth=200;
                }
               
      }
}


function OnSerializeNetworkView(stream : BitStream,info : NetworkMessageInfo){//用来在一个由网络视图监控的脚本中自定义变量同步
      var health : int =0;
      if(stream.isWriting){//比特流是否正在被写入
                health=currentHealth;
                stream.Serialize(health);//序列化不同类型的变量
      } else {
                stream.Serialize(health);
                currentHealth=health;
      }
}3.然后拖动HealthText.js到NetworkView上的Observed上即可.
{:soso__1243193949118710676_4:}

aaabbbsss12345 发表于 2014-5-15 14:12

好好 学习了 学习了

tz259417 发表于 2017-2-28 19:26

很不错

xw12369 发表于 2017-2-28 19:29

楼主是超人

枫叶飘零 发表于 2017-2-28 19:07

顶顶多好

Tony 发表于 2017-2-28 19:16

难得一见的好帖

小宝宝/ht 发表于 2017-2-28 19:19

很好哦

copy_h 发表于 2017-3-14 21:29

很不错

luciferlchaha 发表于 2017-3-14 21:49

楼主是超人

seawingzhang 发表于 2017-3-14 22:04

好帖就是要顶
页: [1]
查看完整版本: 实现网络中游戏对象的血量值的同步