Version: 5.4
HUD를 사용한 통합
NetworkTransport를 사용한 통합

Unity의 고급 레벨 API를 사용한 통합

네트워킹 고급 레벨 API를 사용하여 Unity 멀티플레이어 서비스를 통합하려면 NetworkMatch 클래스를 스크립트에 직접 사용해야 합니다. 이 클래스를 사용하려면 NetworkMatch의 함수를 수동으로 호출하고 콜백을 직접 처리해야 합니다.

다음은 NetworkMatch, NetworkServerNetworkClient 클래스만 사용하여 매치를 만들고, 매치를 나열하고, 매치에 조인하는 방법의 예입니다.

아래 스크립트에서는 공용 Unity 매치메이커 서버에 연결하도록 매치메이커를 설정합니다. 매치를 만들고, 나열하고, 조인하기 위한 NetworkMatch 함수가 호출됩니다.

  • CreateMatch를 사용하여 매치를 만듭니다.
  • JoinMatch를 사용하여 매치를 조인합니다.
  • ListMatches를 사용하여 매치메이커 서버에 등록된 매치를 나열합니다.

내부적으로 NetworkMatch는 웹 서비스를 사용하여 매치를 설정하고, 프로세스가 완료되면 해당 콜백 함수(예: 매치 만들기의 경우 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()
    {
        // You would normally not join a match you created yourself but this is possible here for demonstration purposes.
        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!");
    }
}
HUD를 사용한 통합
NetworkTransport를 사용한 통합