使用Unity的高级API进行集成

要使用Networking High-Level API集成Unity Multiplayer Services ,您必须直接在脚本中使用NetworkMatch类。要使用它,你必须NetworkMatch手动调用函数并自己处理回调。

以下是如何创建匹配项,列出匹配项以及仅使用NetworkMatchNetworkServerNetworkClient类加入匹配项的示例。

该脚本将媒人设置为指向公共Unity媒人服务器。它调用NetworkMatch创建,列出和连接匹配的函数:

  1. CreateMatch创建一个匹配
  2. JoinMatch加入比赛
  3. ListMatch用于列出匹配器服务器上注册的匹配项

在内部,NetworkMatch使用Web服务来建立匹配,并且在过程完成时调用给定的回调函数,例如OnMatchCreate用于创建匹配。

  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using UnityEngine.Networking.Match;
  4. using System.Collections.Generic;
  5. public class HostGame : MonoBehaviour
  6. {
  7. List<MatchInfoSnapshot> matchList = new List<MatchInfoSnapshot>();
  8. bool matchCreated;
  9. NetworkMatch networkMatch;
  10. void Awake()
  11. {
  12. networkMatch = gameObject.AddComponent<NetworkMatch>();
  13. }
  14. void OnGUI()
  15. {
  16. // You would normally not join a match you created yourself but this is possible here for demonstration purposes.
  17. if (GUILayout.Button("Create Room"))
  18. {
  19. string matchName = "room";
  20. uint matchSize = 4;
  21. bool matchAdvertise = true;
  22. string matchPassword = "";
  23. networkMatch.CreateMatch(matchName, matchSize, matchAdvertise, matchPassword, "", "", 0, 0, OnMatchCreate);
  24. }
  25. if (GUILayout.Button("List rooms"))
  26. {
  27. networkMatch.ListMatches(0, 20, "", true, 0, 0, OnMatchList);
  28. }
  29. if (matchList.Count > 0)
  30. {
  31. GUILayout.Label("Current rooms");
  32. }
  33. foreach (var match in matchList)
  34. {
  35. if (GUILayout.Button(match.name))
  36. {
  37. networkMatch.JoinMatch(match.networkId, "", "", "", 0, 0, OnMatchJoined);
  38. }
  39. }
  40. }
  41. public void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
  42. {
  43. if (success)
  44. {
  45. Debug.Log("Create match succeeded");
  46. matchCreated = true;
  47. NetworkServer.Listen(matchInfo, 9000);
  48. Utility.SetAccessTokenForNetwork(matchInfo.networkId, matchInfo.accessToken);
  49. }
  50. else
  51. {
  52. Debug.LogError("Create match failed: " + extendedInfo);
  53. }
  54. }
  55. public void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches)
  56. {
  57. if (success && matches != null && matches.Count > 0)
  58. {
  59. networkMatch.JoinMatch(matches[0].networkId, "", "", "", 0, 0, OnMatchJoined);
  60. }
  61. else if (!success)
  62. {
  63. Debug.LogError("List match failed: " + extendedInfo);
  64. }
  65. }
  66. public void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
  67. {
  68. if (success)
  69. {
  70. Debug.Log("Join match succeeded");
  71. if (matchCreated)
  72. {
  73. Debug.LogWarning("Match already set up, aborting...");
  74. return;
  75. }
  76. Utility.SetAccessTokenForNetwork(matchInfo.networkId, matchInfo.accessToken);
  77. NetworkClient myClient = new NetworkClient();
  78. myClient.RegisterHandler(MsgType.Connect, OnConnected);
  79. myClient.Connect(matchInfo);
  80. }
  81. else
  82. {
  83. Debug.LogError("Join match failed " + extendedInfo);
  84. }
  85. }
  86. public void OnConnected(NetworkMessage msg)
  87. {
  88. Debug.Log("Connected!");
  89. }
  90. }

?