- Home /
Question by
Thesaurus1233 · May 06, 2014 at 05:24 PM ·
networkconnection
Connecting twice problem
Hi, i have a problem with unity networking, but because a don't know how to explain it i filmed a video
Here is the script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour {
public string PlayerName;
public string MatchName;
public static NetworkManager Instance;
public List<Player> PlayerList = new List<Player>();
public Player MyPlayer;
// Use this for initialization
void Awake(){
Instance = this;
}
void Start () {
DontDestroyOnLoad(gameObject);
}
// Update is called once per frame
void Update () {
PlayerName = PlayerPrefs.GetString("PlayerName");
}
public void StartServer(string ServerName, int MaxPlayers){
Network.InitializeSecurity();
Network.InitializeServer(MaxPlayers, 25565, true);
MasterServer.RegisterHost("FPsMulty",ServerName, "");
Debug.Log("Se empezo el server");
}
void OnPlayerConnected(NetworkPlayer id){
networkView.RPC("Server_PlayerJoined",RPCMode.Server,PlayerName,id);
foreach(Player pl in PlayerList){
networkView.RPC("Client_PlayerJoined",id,pl.PlayerName,pl.OnlinePlayer);
}
}
void OnServerInitialized(){
Server_PlayerJoined(PlayerName,Network.player);
}
void OnConnectedToServer(){
networkView.RPC("Server_PlayerJoined",RPCMode.Server, PlayerName, Network.player);
}
void OnPlayerDisconnected(NetworkPlayer id){
networkView.RPC("RemovePlayer", RPCMode.All, id);
Network.RemoveRPCs(id);
}
void OnDisconnectedFromServer(NetworkDisconnection info){
PlayerList.Clear();
}
[RPC]
public void Server_PlayerJoined(string Username, NetworkPlayer id){
networkView.RPC("Client_PlayerJoined",RPCMode.All, Username, id);
}
[RPC]
public void Client_PlayerJoined(string Username, NetworkPlayer id){
Player temp = new Player();
temp.PlayerName = Username;
temp.OnlinePlayer = id;
PlayerList.Add(temp);
if(Network.player == id){
MyPlayer = temp;
}
}
[RPC]
public void RemovePlayer(NetworkPlayer id){
Player temp = new Player();
foreach(Player pl in PlayerList){
if(pl.OnlinePlayer == id){
temp = pl;
}
}
if(temp != null){
PlayerList.Remove(temp);
}
}
}
[System.Serializable]
public class Player{
public string PlayerName;
public NetworkPlayer OnlinePlayer;
}
and
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
public Menu instance;
private string CurMenu;
public string Nombre;
public string MatchName = "Name";
public int Players;
// Use this for initialization
void Start () {
CurMenu = "Main";
instance = this;
Nombre = PlayerPrefs.GetString("PlayerName");
}
// Update is called once per frame
void Update () {
}
void ToMenu(string menu){
CurMenu = menu;
}
void OnGUI(){
if(CurMenu == "Main")
Main();
if(CurMenu == "Host")
Host();
if(CurMenu == "Lobby")
Lobby();
if(CurMenu == "List")
MatchList();
}
private void Main(){
if (GUI.Button(new Rect(0,0,128,32),"Crea una partida")){
ToMenu("Host");
}
Nombre = GUI.TextField(new Rect(130,0,128,32),Nombre);
if (GUI.Button(new Rect(260,0,128,32),"Guardar")){
PlayerPrefs.SetString("PlayerName", Nombre);
}
if (GUI.Button(new Rect(0,33,128,32),"Lista de Servidores")){
ToMenu("List");
}
}
private void Host(){
if (GUI.Button(new Rect(0,0,128,32),"Empezar")){
NetworkManager.Instance.StartServer(MatchName, Players);
ToMenu("Lobby");
}
if (GUI.Button(new Rect(0,33,128,32),"Atras")){
ToMenu("Main");
}
MatchName = GUI.TextField(new Rect(130,0,128,32),MatchName);
GUI.Label(new Rect(260,0,128,32),"Nombre de la partida");
Players =Mathf.Clamp(Players,0,32);
GUI.Label(new Rect(136,33,128,32),"Jugadores Máximos");
if (GUI.Button(new Rect(265,33,32,32),"+"))
Players ++;
GUI.Label(new Rect(320,33,64,32),Players.ToString());
if (GUI.Button(new Rect(345,33,32,32),"-"))
Players --;
}
private void Lobby(){
if(Network.isServer){
if (GUI.Button(new Rect(Screen.width - 128,Screen.height - 64,128,32),"Empezar")){
}
}
if (GUI.Button(new Rect(Screen.width - 128,Screen.height - 32,128,32),"Atras")){
ToMenu("Host");
}
GUILayout.BeginArea(new Rect(0,0,Screen.width/2,Screen.height));
foreach(Player pl in NetworkManager.Instance.PlayerList){
GUILayout.Label(pl.PlayerName);
}
GUILayout.EndArea();
}
private void MatchList(){
if (GUI.Button(new Rect(0,0,128,32),"Refrescar")){
MasterServer.RequestHostList("FPsMulty");
}
if (GUI.Button(new Rect(0,33,128,32),"Atras")){
ToMenu("Main");
}
GUILayout.BeginArea(new Rect(Screen.width / 2,0,Screen.width / 2,Screen.height),"Lista de Servidores","box");
foreach(HostData hd in MasterServer.PollHostList()){
GUILayout.BeginHorizontal();
GUILayout.Label(hd.gameName);
if (GUILayout.Button("Conectar")){
Network.Connect(hd);
ToMenu("Lobby");
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
}
If someone could help I would be very grateful.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613144406im_/https://answers.unity.com/themes/thub/images/avi.jpg)