(새 프로젝트에는 5.1에서 도입된 새로운 네트워킹 시스템을 사용해야 합니다. 다음 내용은 이전 네트워킹 시스템을 사용하는 레거시 프로젝트를 대상으로 작성되었습니다.)
아래는 멀티플레이어 게임에서 레벨을 로드하는 간단한 예제입니다. 레벨이 로드될 동안에는 네트워크 메시지가 처리되지 않도록 합니다. 또한 모든 준비가 완료되기 전까지는 메시지가 전송되지 않게 합니다. 끝으로, 레벨이 로드될 때 모든 스크립트에 메시지를 보내 레벨이 로드되었고 이에 반응할 수 있음을 알립니다. 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 스크립트 예제
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.