- Home /
Question by
dudedude123 · Sep 21, 2015 at 09:52 AM ·
c#networkinglobby
setting up the lobby
I have made a script for the lobby.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.Match;
using System.Collections.Generic;
public class HostGame : MonoBehaviour
{
List<MatchDesc> matchList = new List<MatchDesc>();
bool matchCreated;
NetworkMatch networkMatch;
void Awake()
{
networkMatch = gameObject.AddComponent<NetworkMatch>();
}
public void CreateRooms()
{
CreateMatchRequest create = new CreateMatchRequest();
create.name = "NewRoom";
create.size = 2;
create.advertise = true;
create.password = "";
networkMatch.CreateMatch(create, OnMatchCreate);
}
public void ListRooms()
{
networkMatch.ListMatches(0, 20, "", OnMatchList);
}
void OnGUI()
{
if (matchList.Count > 0)
{
GUILayout.Label("Current rooms");
}
foreach (var match in matchList)
{
if (GUILayout.Button(match.name))
{
networkMatch.JoinMatch(match.networkId, "", OnMatchJoined);
}
}
}
public void OnMatchCreate(CreateMatchResponse matchResponse)
{
if (matchResponse.success)
{
Debug.Log("Create match succeeded");
matchCreated = true;
Utility.SetAccessTokenForNetwork(matchResponse.networkId, new NetworkAccessToken(matchResponse.accessTokenString));
NetworkServer.Listen(new MatchInfo(matchResponse), 9000);
}
else
{
Debug.LogError("Create match failed");
}
}
public void OnMatchList(ListMatchResponse matchListResponse)
{
if (matchListResponse.success && matchListResponse.matches != null)
{
networkMatch.JoinMatch(matchListResponse.matches[0].networkId, "", OnMatchJoined);
}
}
public void OnMatchJoined(JoinMatchResponse matchJoin)
{
if (matchJoin.success)
{
Debug.Log("Join match succeeded");
if (matchCreated)
{
Debug.LogWarning("Match already set up, aborting...");
return;
}
Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
NetworkClient myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.Connect(new MatchInfo(matchJoin));
}
else
{
Debug.LogError("Join match failed");
}
}
public void OnConnected(NetworkMessage msg)
{
Debug.Log("Connected!");
Application.LoadLevel("Game");
}
}
I want to use this code to load the level
public void OnConnected(NetworkMessage msg)
{
Debug.Log("Connected!");
Application.LoadLevel("Game");
}
Is this the right code to load the multiplayer level?
Comment