高レベル API を使って Unity Multiplayer Services を統合するには、NetworkMatch クラスを直接スクリプトで使用する必要があります。それを使うには、手動で NetworkMatch
で関数を呼び出し、コールバックを自分で処理しなければなりません。
以下は、NetworkMatch、 NetworkServer、 NetworkClient クラスを使ってどのようにマッチを作成 (create) し、一覧にし (list)、加わるか (join) の例です。
このスクリプトは、パブリックのマッチメーカーサーバーにアクセスするためのマッチメーカーを設定します。マッチを作成し (creating)、リストし (listing)、参加する (joining) ための NetworkMatch
関数を呼び出します。
内部的には、NetworkMatch
は web サービスを使いマッチを構築し、処理が終了したときにコールバック関数が呼び出されます。例えば、マッチが作成されたときに OnMatchCreate
が呼び出されます。
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using System.Collections.Generic;
public class HostGame : MonoBehaviour
{
List<MatchInfoSnapshot> matchList = new List<MatchInfoSnapshot>();
bool matchCreated;
NetworkMatch networkMatch;
void Awake()
{
networkMatch = gameObject.AddComponent<NetworkMatch>();
}
void OnGUI()
{
// 自身で作成したマッチには通常は参加しませんが、デモ目的のため、ここでは可能です。
if (GUILayout.Button("Create Room"))
{
string matchName = "room";
uint matchSize = 4;
bool matchAdvertise = true;
string matchPassword = "";
networkMatch.CreateMatch(matchName, matchSize, matchAdvertise, matchPassword, "", "", 0, 0, OnMatchCreate);
}
if (GUILayout.Button("List rooms"))
{
networkMatch.ListMatches(0, 20, "", true, 0, 0, OnMatchList);
}
if (matchList.Count > 0)
{
GUILayout.Label("Current rooms");
}
foreach (var match in matchList)
{
if (GUILayout.Button(match.name))
{
networkMatch.JoinMatch(match.networkId, "", "", "", 0, 0, OnMatchJoined);
}
}
}
public void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Create match succeeded");
matchCreated = true;
NetworkServer.Listen(matchInfo, 9000);
Utility.SetAccessTokenForNetwork(matchInfo.networkId, matchInfo.accessToken);
}
else
{
Debug.LogError("Create match failed: " + extendedInfo);
}
}
public void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches)
{
if (success && matches != null && matches.Count > 0)
{
networkMatch.JoinMatch(matches[0].networkId, "", "", "", 0, 0, OnMatchJoined);
}
else if (!success)
{
Debug.LogError("List match failed: " + extendedInfo);
}
}
public void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Join match succeeded");
if (matchCreated)
{
Debug.LogWarning("Match already set up, aborting...");
return;
}
Utility.SetAccessTokenForNetwork(matchInfo.networkId, matchInfo.accessToken);
NetworkClient myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.Connect(matchInfo);
}
else
{
Debug.LogError("Join match failed " + extendedInfo);
}
}
public void OnConnected(NetworkMessage msg)
{
Debug.Log("Connected!");
}
}
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.