MonoBehaviour.OnSerializeNetworkView 当序列化网络视图

function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) : void

Description描述

Used to customize synchronization of variables in a script watched by a network view.

在一个网络视图脚本中,用于自定义变量同步。

It is automatically determined if the variables being serialized should be sent or received, see example below for a better description. This depends on who owns the object, i.e. the owner sends and all others receive.

它自动决定被序列化的变量是否应该发送或接收,查看下面的例子获取更好的描述。这个依赖于谁拥有这个物体,例如,所有者发送,其他物体接收。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public int currentHealth = 0;
	void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
		if (stream.isWriting) {
			int healthC = currentHealth;
			stream.Serialize(ref healthC);
		} else {
			int healthZ = 0;
			stream.Serialize(ref healthZ);
			currentHealth = healthZ;
		}
	}
}
// This objects health information
// 此物体的健康信息
var currentHealth : int = 0;

function OnSerializeNetworkView(stream : BitStream , info : NetworkMessageInfo ) {
	if (stream.isWriting) {
		var healthC : int = currentHealth;
		stream.Serialize(healthC);
	} else {
		var healthZ : int = 0;
		stream.Serialize(healthZ);
		currentHealth = healthZ;
	}    
}
最后修改:2011年1月2日 Sunday 19:00

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。