the worse part is … the GUI from unity 4.6 make it fail…
cause i tried with the GUI.Button fashion and it work perfect… my player can connect to the server
Working Code
using UnityEngine;
namespace Assets.Scripts.Network
{
public class NetworkManager : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject PlayerPrefab;
private string _currentMenu;
private const float BtnW = 150;
private const float BtnH = 40;
private HostData[] _hostData = null;
private const string GameName = "Keep the Circles";
private const string RoomName = "Rocky Valley";
private const string TypeGame = "A capture the flag game";
private const int MaxNbPlayer = 2;
private const int ListenPort = 25001;
void Start ()
{
_currentMenu = "Multiplayer";
}
private void StartServer()
{
UnityEngine.Network.InitializeServer(MaxNbPlayer, ListenPort, !UnityEngine.Network.HavePublicAddress());
// A decommenter si le server unity est down pour rouller le serveur en local.
//MasterServer.ipAddress = "127.0.0.1";
MasterServer.RegisterHost(GameName, RoomName, TypeGame);
}
private void RefreshHostList()
{
MasterServer.RequestHostList(GameName);
}
private void SpawnPlayer()
{
var player = (GameObject) UnityEngine.Network.Instantiate(PlayerPrefab, SpawnPoints[UnityEngine.Network.connections.Length].position, Quaternion.identity, 0);
player.networkView.RPC("SetColor", RPCMode.AllBuffered, new Vector3(UnityEngine.Network.connections.Length, 0, 0));
}
void OnServerInitialized()
{
Debug.Log("Server Initialized!");
SpawnPlayer();
}
void OnMasterServerEvent(MasterServerEvent mse)
{
if (mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registered Server");
}
else if (mse == MasterServerEvent.HostListReceived)
{
_hostData = MasterServer.PollHostList();
}
}
void OnConnectedToServer()
{
SpawnPlayer();
}
void OnGUI()
{
if (_currentMenu == "Main")
Menu_Main();
if (_currentMenu == "Multiplayer")
Menu_Multiplayer();
}
void Menu_Main()
{
}
void Menu_Multiplayer()
{
if (!UnityEngine.Network.isClient && !UnityEngine.Network.isServer)
{
if (GUI.Button(new Rect(50, 50, BtnW, BtnH), "Start Server"))
{
StartServer();
}
if (GUI.Button(new Rect(50, 100, BtnW, BtnH), "Refresh"))
{
Debug.Log("Refreshing...");
RefreshHostList();
}
GUILayout.BeginArea(new Rect(Screen.width - 500, 0, 500, Screen.height), "Server List");
GUILayout.Space(20);
foreach (HostData hostedGame in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal("Box");
GUILayout.Label(hostedGame.gameName);
if (GUILayout.Button("Connect"))
{
UnityEngine.Network.Connect(hostedGame);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
}
}
}
Code That fail ??? and i dont understand what’s wrong with it…
my start server button = StartServer
my connect button = RefreshHostList
my Room button = ConnectToRoomEvent(roomName)
everything work excep the RoomButton that give me the Nat error…
using Assets.Scripts.GUI;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts
{
public class NetworkManager : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject PlayerPrefab;
private string _currentMenu;
private const float BtnW = 150;
private const float BtnH = 40;
private HostData[] _hostData = null;
public Button RoomButton;
public Canvas Canvas;
private const string GameName = "Keep the Circles";
private const string RoomName = "Rocky Valley";
private const string TypeGame = "A capture the flag game";
private const int MaxNbPlayer = 2;
private const int ListenPort = 25001;
void Start()
{
_currentMenu = "Multiplayer";
}
public void StartServer()
{
Network.InitializeServer(MaxNbPlayer, ListenPort, !Network.HavePublicAddress());
// A decommenter si le server unity est down pour rouller le serveur en local.
//MasterServer.ipAddress = "127.0.0.1";
MasterServer.RegisterHost(GameName, RoomName, TypeGame);
}
public void RefreshHostList()
{
MasterServer.RequestHostList(GameName);
}
private void SpawnPlayer()
{
var player = (GameObject) Network.Instantiate(PlayerPrefab, SpawnPoints[Network.connections.Length].position, Quaternion.identity, 0);
}
void OnServerInitialized()
{
Debug.Log("Server Initialized!");
SpawnPlayer();
}
void OnMasterServerEvent(MasterServerEvent mse)
{
if (mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registered Server");
}
else if (mse == MasterServerEvent.HostListReceived)
{
_hostData = MasterServer.PollHostList();
for (int i = 0; i < _hostData.Length; i++)
{
var button = GuiHelper.CreateButton(RoomButton, Canvas, new Vector2(0.03f, 0.87f - (i * 0.20f)), new Vector2(0.23f, 0.94f - (i * 0.18f)));
var text = button.GetComponentInChildren<Text>();
text.text = _hostData[i].gameName;
var roomName = _hostData[i].gameName;
button.onClick.AddListener(() => ConnectToRoomEvent(roomName));
}
}
}
private void ConnectToRoomEvent(string roomName)
{
Network.Connect(roomName);
}
void OnConnectedToServer()
{
SpawnPlayer();
}
}
}