(Unity 5.1 からは UNET の使用を推奨しています。下記の情報は旧ネットワークシステムのものです。)
以下でマルチプレイヤーゲームでレベルロードする簡単なサンプルを示します。レベルロードの途中でネットワークメッセージの処理が行われないことをチェックしてます。さらにすべて準備が整った後でないかぎりメッセージ送信が行われないこともあわせてチェックしてます。最後にレベルロード完了時にすべてのスクリプトにメッセージ送信を行いレベルロードが終わってなんらかの処理を受け付けできることを示します。SetLevelPrefix 関数により新しくロードしたレベルに不要なネットワーク更新が行われないようにします。不要な更新とは例えば前のレベルでの更新です。このサンプルではグループを使用してゲームデータとグループへのレベルロード通信を分離します。グループ 0 はゲームデータのトラフィックで使用されグループ 1 はレベルロードに使用します。レベルロードの途中ではグループ 0 はブロックされますがグループ 1 は開放されていて、レベルロードの際にチャット通信も別に行うことができるよう開放されてます。
using UnityEngine;
using UnityEngine.Network;
using System.Collections;
[RequireComponent(NetworkView)]
public class ExampleScript : MonoBehaviour {
string[] supportedNetworkLevels = new[]{ "mylevel" };
string disconnectedLevel = "loader";
int lastLevelPrefix = 0;
NetworkView networkView;
void Awake ()
{
// Network level loading is done in a separate channel.
DontDestroyOnLoad(this);
networkView = new NetworkView ();
networkView.group = 1;
Application.LoadLevel(disconnectedLevel);
}
void OnGUI ()
{
if (Network.peerType != NetworkPeerType.Disconnected)
{
GUILayout.BeginArea(Rect(0, Screen.height - 30, Screen.width, 30));
GUILayout.BeginHorizontal();
foreach (var level in supportedNetworkLevels)
{
if (GUILayout.Button(level))
{
Network.RemoveRPCsInGroup(0);
Network.RemoveRPCsInGroup(1);
networkView.RPC( "LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1);
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
[RPC]
IEnumerator LoadLevel (string level, int levelPrefix)
{
lastLevelPrefix = levelPrefix;
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, thus all those objects will get deleted anyway
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded first.
// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
yield return;
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
Network.SetSendingEnabled(0, true);
var gameObjects = FindObjectsOfType(GameObject) as GameObject[];
foreach (var go in gameObjects)
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
void OnDisconnectedFromServer ()
{
Application.LoadLevel(disconnectedLevel);
}
}
C# スクリプトの例
var supportedNetworkLevels : String[] = [ "mylevel" ];
var disconnectedLevel : String = "loader";
private var lastLevelPrefix = 0;
function Awake ()
{
// Network level loading is done in a separate channel.
DontDestroyOnLoad(this);
networkView.group = 1;
Application.LoadLevel(disconnectedLevel);
}
function OnGUI ()
{
if (Network.peerType != NetworkPeerType.Disconnected)
{
GUILayout.BeginArea(Rect(0, Screen.height - 30, Screen.width, 30));
GUILayout.BeginHorizontal();
for (var level in supportedNetworkLevels)
{
if (GUILayout.Button(level))
{
Network.RemoveRPCsInGroup(0);
Network.RemoveRPCsInGroup(1);
networkView.RPC( "LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1);
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
@RPC
function LoadLevel (level : String, levelPrefix : int)
{
lastLevelPrefix = levelPrefix;
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, thus all those objects will get deleted anyway
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded first.
// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
yield;
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
Network.SetSendingEnabled(0, true);
for (var go in FindObjectsOfType(GameObject))
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
function OnDisconnectedFromServer ()
{
Application.LoadLevel(disconnectedLevel);
}
@script RequireComponent(NetworkView)
JS スクリプトの例