Hey all i have followet a tut on youtube and thats in C# So here is the scripet to the MenuManager and Multiplayer Manager. And on youtube i found a scripet to this make an account on my database (That scrip will be here too) but i need really to know how to get the username to be the playername Plz Help Me
This Multiplayer manager
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
public static MultiplayerManager instance;
public string PlayerName;
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public List<MapSetting> MapList = new List<MapSetting>();
public MapSetting CurrentMap = null;
public int oldprefix;
public bool IsMatchStarted = false;
void Start()
{
instance = this;
PlayerName = PlayerPrefs.GetString("PlayerName");
CurrentMap = MapList[0];
}
void FixedUpdate()
{
instance = this;
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer(MatchMaxUsers, 2550, false);
MasterServer.RegisterHost("DeathMatch", MatchName, "");
//Network.InitializeSecurity();
}
void OnServerInitialized()
{
Server_PlayerJoinRequest(PlayerName, Network.player);
}
void OnConnectedToServer()
{
networkView.RPC("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
}
void OnPlayerDisconnected(NetworkPlayer id)
{
networkView.RPC("Client_RemovePlayer", RPCMode.All, id);
}
void OnPlayerConnected(NetworkPlayer player)
{
foreach(MPPlayer pl in PlayerList)
{
networkView.RPC("Client_AddPlayerToList", player, pl.PlayerName, pl.PlayerNetwork);
}
}
void OnDisconnectedFromServer()
{
PlayerList.Clear();
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
networkView.RPC("Client_AddPlayerToList", RPCMode.All, playername, view);
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add(tempplayer);
}
[RPC]
void Client_RemovePlayer(NetworkPlayer view)
{
MPPlayer temppl = null;
foreach(MPPlayer pl in PlayerList)
{
if (pl.PlayerNetwork == view)
{
temppl = pl;
}
}
if(temppl != null)
{
PlayerList.Remove(temppl);
}
}
[RPC]
void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
{
CurrentMap = GetMap(map);
}
public MapSetting GetMap(string name)
{
MapSetting get = null;
foreach(MapSetting st in MapList)
{
if(st.MapName == name)
{
get = st;
break;
}
}
return get;
}
[RPC]
void Client_LoadMultiplayerMap(string map, int prefix)
{
Network.SetLevelPrefix(prefix);
Application.LoadLevel(map);
}
}
[System.Serializable]
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
}
[System.Serializable]
public class MapSetting
{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}
Now the Menu Manager
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
public static MenuManager instance;
public string CurrentMenu;
public string MatchName = "";
public string MatchPassword = "";
public int MatchMaxPlayers = 32;
private Vector2 ScrollLobby = Vector2.zero;
private string iptemp = "127.0.0.1";
void Start()
{
instance = this;
CurrentMenu = "Main";
}
void FixedUpdate()
{
instance = this;
}
void OnGUI()
{
if(CurrentMenu == "Main")
Menu_Main();
if(CurrentMenu == "Lobby")
Menu_Lobby();
if(CurrentMenu == "Host")
Menu_HostGame();
if(CurrentMenu == "ChoMap")
Menu_ChooseMap();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void Menu_Main()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Host Game"))
{
NavigateTo("Host");
}
if (GUI.Button(new Rect(10, 70, 200, 50), "Refresh"))
{
MasterServer.RequestHostList("DeathMatch");
}
GUI.Label(new Rect(220, 10, 130, 30), "Player Name");
MultiplayerManager.instance.PlayerName = GUI.TextField(new Rect(350, 10, 150, 30), MultiplayerManager.instance.PlayerName);
if (GUI.Button(new Rect(510, 10, 100, 30), "Save Name"))
{
PlayerPrefs.SetString("PlayerName", MultiplayerManager.instance.PlayerName);
}
GUI.Label(new Rect(220, 50, 130, 30), "Direct Connect");
iptemp = GUI.TextField(new Rect(350, 50, 150, 30), iptemp);
if (GUI.Button(new Rect(510, 50, 100, 30), "Connect"))
{
Network.Connect(iptemp, 2550);
}
GUILayout.BeginArea(new Rect(Screen.width - 400, 0, 400, Screen.height), "Server List", "Box");
GUILayout.Space(20);
foreach (HostData match in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal("Box");
GUILayout.Label(match.gameName);
if(GUILayout.Button("Connect"))
{
Network.Connect(match);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
private void Menu_HostGame()
{
//Buttons Host Game
if (GUI.Button(new Rect(10, 10, 200, 50), "Back"))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(10, 60, 200, 50), "Start Server"))
{
MultiplayerManager.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
}
if (GUI.Button(new Rect(10, 160, 200, 50), "Choose Map"))
{
NavigateTo("ChoMap");
}
GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField(new Rect(400, 10, 200, 30), MatchName);
GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
MatchPassword = GUI.PasswordField(new Rect(400, 50, 200, 30), MatchPassword, '*');
GUI.Label(new Rect(220, 90, 130, 30), "Match Max Players");
GUI.Label(new Rect(400, 90, 200, 30), (MatchMaxPlayers + 1).ToString());
MatchMaxPlayers = Mathf.Clamp(MatchMaxPlayers, 8, 31);
if (GUI.Button(new Rect(425, 90, 25, 30), "+"))
MatchMaxPlayers += 2;
if (GUI.Button(new Rect(450, 90, 25, 30), "-"))
MatchMaxPlayers -= 2;
GUI.Label(new Rect(650, 10, 130, 30), MultiplayerManager.instance.CurrentMap.MapName);
}
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth(200));
foreach(MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
if(pl.PlayerNetwork == Network.player)
GUI.color = Color.blue;
GUILayout.Box(pl.PlayerName);
GUI.color = Color.white;
}
GUILayout.EndScrollView();
GUI.Box (new Rect(250, 10, 200, 40), MultiplayerManager.instance.CurrentMap.MapName);
if(Network.isServer)
{
if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 80, 200, 40), "Start Match"))
{
MultiplayerManager.instance.networkView.RPC("Client_LoadMultiplayerMap", RPCMode.All, MultiplayerManager.instance.CurrentMap.MapLoadName, MultiplayerManager.instance.oldprefix + 1);
MultiplayerManager.instance.oldprefix += 1;
MultiplayerManager.instance.IsMatchStarted = true;
}
}
if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 40, 200, 40), "Disconnect"))
{
Network.Disconnect();
}
}
private void Menu_ChooseMap()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Back"))
{
NavigateTo("Host");
}
GUI.Label(new Rect(220, 10, 130, 30), "Choose Map");
GUILayout.BeginArea(new Rect(350, 10, 150, Screen.height));
foreach(MapSetting map in MultiplayerManager.instance.MapList)
{
if(GUILayout.Button(map.MapName))
{
NavigateTo("Host");
MultiplayerManager.instance.CurrentMap = map;
}
}
GUILayout.EndArea();
}
void OnServerInitialized()
{
NavigateTo("Lobby");
}
void OnConnectedToServer()
{
NavigateTo("Lobby");
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
NavigateTo("Main");
}
}
The DataBase Scripet
using UnityEngine;
using System.Collections;
public class database : MonoBehaviour
{
public static string user = "", name = "";
private string password = "", rePass = "", message = "";
private bool register = false;
private void OnGUI()
{
if (message != "")
GUILayout.Box(message);
if (register)
{
GUILayout.Label("Username");
user = GUILayout.TextField(user);
GUILayout.Label("Name");
name = GUILayout.TextField(name);
GUILayout.Label("password");
password = GUILayout.PasswordField(password, "*"[0]);
GUILayout.Label("Re-password");
rePass = GUILayout.PasswordField(rePass, "*"[0]);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Back"))
register = false;
if (GUILayout.Button("Register"))
{
message = "";
if (user == "" || name == "" || password == "")
message += "Please enter all the fields \n";
else
{
if (password == rePass)
{
WWWForm form = new WWWForm();
form.AddField("user", user);
form.AddField("name", name);
form.AddField("password", password);
WWW w = new WWW("http://f6-preview.awardspace.com/unitytutorial.com/register.php", form);
StartCoroutine(registerFunc(w));
}
else
message += "Your Password does not match \n";
}
}
GUILayout.EndHorizontal();
}
else
{
GUILayout.Label("User:");
user = GUILayout.TextField(user);
GUILayout.Label("Password:");
password = GUILayout.PasswordField(password, "*"[0]);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Login"))
{
message = "";
if (user == "" || password == "")
message += "Please enter all the fields \n";
else
{
WWWForm form = new WWWForm();
form.AddField("user", user);
form.AddField("password", password);
WWW w = new WWW("http://f6-preview.awardspace.com/unitytutorial.com/login.php", form);
StartCoroutine(login(w));
}
}
if (GUILayout.Button("Register"))
register = true;
GUILayout.EndHorizontal();
}
}
IEnumerator login(WWW w)
{
yield return w;
if (w.error == null)
{
if (w.text == "login-SUCCESS")
{
print("WOOOOOOOOOOOOOOO!");
}
else
message += w.text;
}
else
{
message += "ERROR: " + w.error + "\n";
}
}
IEnumerator registerFunc(WWW w)
{
yield return w;
if (w.error == null)
{
message += w.text;
}
else
{
message += "ERROR: " + w.error + "\n";
}
}
}
Plz Help me