Im making a game that has networked multiplayer, but now im stuck. The reason is, I want that you can only spawn if you are in the lobby when the host starts the match but if you connect and the match has started that it kicks you(or just remove the server from the browser when the match starts). Though I do not now how to make this function.
Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkMenu : MonoBehaviour {
public string CurrentMenu = "";
public string servername = "Server Name";
public int Port = 24444;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public GameObject Spawnpoint;
public GameObject Spawnedpl;
public bool matchstarted;
public bool Connected;
public string _Playername;
public static NetworkMenu ins;
public GameObject networkScoreManager;
public ScoreManager score;
public bool _start;
public GUIStyle gui_Style;
void Awake(){
ins = this;
NextMenu("Main");
networkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
score = networkScoreManager.GetComponent<ScoreManager>();
_Playername = PlayerPrefs.GetString("PlayerName");
}
void Start(){
Set_GUI_Style();
}
private void Set_GUI_Style(){
gui_Style.alignment = TextAnchor.MiddleCenter;
}
void OnGUI(){
if(CurrentMenu == "Main"){
GUIMain();
}
if(CurrentMenu == "Start"){
GUIStart();
}
if(CurrentMenu == "Browser"){
GUIBrowser();
}
Lobby();
}
//GUI
private void GUIMain(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Browser",gui_Style)){
NextMenu("Browser");
MasterServer.RequestHostList("354545Battle");
}
if(GUI.Button(new Rect(10,40,200,25),"Host",gui_Style)){
NextMenu("Start");
}
_Playername = GUI.TextArea(new Rect(10,70,200,25),_Playername,15,gui_Style);
if(GUI.Button(new Rect(10,100,200,25),"Save Name",gui_Style)){
PlayerPrefs.SetString("PlayerName", _Playername);
}
if(GUI.Button(new Rect(10,130,200,25),"Quit",gui_Style)){
Application.Quit();
}
}
}
private void GUIStart(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Start Server",gui_Style)){
StartServer();
}
if(GUI.Button(new Rect(10,40,200,25),"Back",gui_Style)){
NextMenu("Main");
}
}
}
void Update(){
if(Network.isServer){
if(Network.connections.Length >= 0 && !matchstarted){
//Server_SendSettings();
//Spawn();
_start = true;
}
}
}
public void Server_SendSettings(){
networkView.RPC("Player_LoadSettings",RPCMode.All,true);
networkScoreManager.networkView.RPC("FoundAllPlayers",RPCMode.All,true,Network.connections.Length + 1);
Debug.Log("Sending Match Settings");
}
public void Lobby(){
if(Network.isServer && !matchstarted){
if(_start){
if(GUI.Button(new Rect(10,10,200,25),"Start Match",gui_Style)){
Server_SendSettings();
}
if(GUI.Button(new Rect(10,40,200,25),"End Match",gui_Style)){
Network.Disconnect();
Application.LoadLevel(0);
}
}
}
}
private void GUIBrowser(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Back",gui_Style)){
NextMenu("Main");
}
if(GUI.Button(new Rect(10,40,200,25),"Refresh",gui_Style)){
MasterServer.RequestHostList("354545Battle");
}
GUILayout.BeginArea(new Rect(Screen.width / 2, 0,Screen.width / 2,Screen.height),"Server List","box");
GUILayout.Space(20);
foreach(HostData hd in MasterServer.PollHostList()){
GUILayout.BeginHorizontal();
GUILayout.Label(hd.gameName);
GUILayout.Label(hd.connectedPlayers + 1 + "/" + hd.playerLimit);
if(GUILayout.Button("Connect")){
Network.Connect(hd);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
}
void OnPlayerConnected(NetworkPlayer player){
}
void OnConnectedToServer(){
Debug.Log("Connecting");
networkView.RPC("PlayerConnectedToServer",RPCMode.Others,_Playername);
if(matchstarted){
Network.Disconnect();
Application.LoadLevel(0);
}
Debug.Log("Done");
}
public void PlayerLeave(GameObject player){
Network.Destroy(player);
networkScoreManager.networkView.RPC("PlayerLeft",RPCMode.All,_Playername);
}
public void HostQuit(GameObject host){
networkView.RPC("KickAllPlayers",RPCMode.All);
}
void OnServerInitialized(){
}
public void StartServer(){
Network.InitializeServer(15,Port,false);
// Network.InitializeSecurity();
MasterServer.RegisterHost("354545Battle",servername,"Hosted Game");
}
public void Spawn(){
GameObject PlayerSpawned = Network.Instantiate(Spawnedpl,Spawnpoint.transform.position,Quaternion.identity,1) as GameObject;
PlayerSpawned.GetComponent<NetworkManager>()._name = _Playername;
}
//Next Menu Void
public void NextMenu(string nxt){
CurrentMenu = nxt;
}
void OnDisconnectedFromServer(){
Application.LoadLevel(0);
}
void OnPlayerDisconnected(NetworkPlayer id){
Network.DestroyPlayerObjects(id);
Network.RemoveRPCs(id);
}
//RPCs
[RPC]
void Player_Joined(){
}
[RPC]
void Player_LoadSettings(bool _matchstarted){
matchstarted = _matchstarted;
Debug.Log("Got Settings!");
Spawn();
}
}
public class MPPlayer{
public string PlayerName;
public NetworkPlayer PlayerNetwork;
}