Created
February 22, 2014 17:01
-
-
Save cubed2d/9158095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NetworkingManager : MonoBehaviour | |
| { | |
| public static NetworkingManager Instance; | |
| public bool IsServer; | |
| public string IpAddress = "127.0.0.1"; | |
| public int Port = 8888; | |
| public Transform PlayerRootPrefab; | |
| public List<NetworkPlayerRecord> Players = new List<NetworkPlayerRecord>(); | |
| private Dictionary<UnityEngine.NetworkPlayer, NetworkPlayerRecord> RecordLookup = new Dictionary<UnityEngine.NetworkPlayer, NetworkPlayerRecord>(); | |
| private NetworkView view; | |
| // Use this for initialization | |
| IEnumerator Start () | |
| { | |
| // Set up a singleton. There can only be one NetworkingManager this lets us get a referece to it though NetworkingManager.Instance rather than searching for it | |
| Instance = this; | |
| if (!Application.isEditor) | |
| { | |
| // were not running from the Unity Editor, Check for command line args.... | |
| var split = System.Environment.GetCommandLineArgs(); | |
| // args examples | |
| // "-Server -Port 8888" | |
| // "-Client -IP 127.0.0.1 -Port 8888" | |
| for (int i = 0; i < split.Length; i++) | |
| { | |
| var arg = split[i]; | |
| if (arg.ToLower() == "-server") | |
| { | |
| IsServer = true; | |
| } | |
| else if (arg.ToLower() == "-client") | |
| { | |
| IsServer = false; | |
| } | |
| else if (arg.ToLower() == "-ip") | |
| { | |
| IpAddress = split[++i]; | |
| } | |
| else if (arg.ToLower() == "-port") | |
| { | |
| Port = int.Parse(split[++i]); | |
| } | |
| } | |
| } | |
| if (IsServer) | |
| { | |
| Network.InitializeServer(5, Port, false); | |
| Debug.Log("Creating Server"); | |
| // OnConnectedToServer(); // uncomment this to allow the server to spawn a player | |
| } | |
| else | |
| { | |
| Network.Connect(IpAddress, Port); | |
| Debug.Log("Connteting to server"); | |
| } | |
| Debug.Log("********************************"); | |
| Debug.Log("** Connected to server!!!!!!!"); | |
| Debug.Log("********************************"); | |
| foreach (var p in Network.connections) | |
| { | |
| // Print a list of all other people in the server.... | |
| Debug.Log("** " + p.ipAddress + " [" + p.externalIP + "]"); | |
| } | |
| Debug.Log("********************************"); | |
| yield return null; | |
| Network.isMessageQueueRunning = false; | |
| // LOAD OR GENERATE THE GAME WORLD HERE | |
| while (!World.IsLoaded) | |
| { | |
| // Wait untill the world is fully loaded | |
| yield return null; | |
| } | |
| Network.isMessageQueueRunning = true; | |
| } | |
| // Update is called once per frame | |
| void Update () | |
| { | |
| // Perfect place to set up a state machine so the server can manage game state | |
| // eg. Starting, Loading, GameRunning, GameWon, Restarting ect. | |
| } | |
| // called whenever a client joins the sever. Use unitys Network instancing to set up a player for them.... | |
| void OnConnectedToServer() | |
| { | |
| Debug.Log("********************************"); | |
| Debug.Log("** Connected to server!!!!!!!"); | |
| Debug.Log("********************************"); | |
| Network.Instantiate(PlayerRootPrefab, new Vector3(500, 25, 500), Quaternion.identity, 0); | |
| } | |
| void OnFailedToConnect(NetworkConnectionError error) | |
| { | |
| Debug.Log("Could not connect to server: " + error); | |
| // todo handle server not being there! | |
| } | |
| void OnServerInitialized() | |
| { | |
| Debug.Log("Server initialized and ready"); | |
| // Todo move state on from starting... | |
| } | |
| public void RegisterPlayerRecord(UnityEngine.NetworkPlayer networkPlayer, PlayerBody playerBody, NetworkViewID id) | |
| { | |
| // this bit is a bit dificault to explain, but in OnConnectedToServer(), we send instance a body for the player to control | |
| // that prefab has PlayerBody compoent on it that calls back here to create a record. | |
| // A record is a small structure that ties together the PlayerBody, NetworkPlayer (unity network object represneting the player) and | |
| // the network id. This makes it easy to locate all the players actual game obects and call RPCs on them! | |
| // We got a new player, lets hook all the existing players in to their server comms | |
| var record = new NetworkPlayerRecord() | |
| { | |
| NetworkPlayer = networkPlayer, | |
| PlayerBody = playerBody, | |
| ID = id | |
| }; | |
| Players.Add(record); | |
| RecordLookup.Add(networkPlayer, record); | |
| } | |
| [RPC] | |
| public void OnPlayerCreated(NetworkMessageInfo info) | |
| { | |
| // This is an RPC that should be called by a player character once they have fully loaded the game world and are ready to play. | |
| // You should keep lists of all the important info a player needs to know (mosnter spawns, item locations ect) | |
| // and call RPCs to send that data to the player now. You can figure out which player has just loaded in to the server | |
| // by using info.Sender. | |
| } | |
| /// <summary> | |
| /// Find a player record for a players body gameobject. | |
| /// </summary> | |
| /// <returns></returns> | |
| public NetworkPlayerRecord FindRecord(GameObject player) | |
| { | |
| foreach (var p in Players) | |
| { | |
| if (p.ServerBody.PlayerBodyInstance.gameObject == player) | |
| return p; | |
| } | |
| return null; | |
| } | |
| // Called by untiy when a player disconects... | |
| void OnPlayerDisconnected(UnityEngine.NetworkPlayer player) | |
| { | |
| // Simply remove all that players stuff | |
| Debug.Log("Clean up after player " + player); | |
| var record = RecordLookup[player]; | |
| if (record != null) | |
| { | |
| RecordLookup.Remove(player); | |
| Players.Remove(record); | |
| } | |
| Network.RemoveRPCs(player); // incase there are nay buffered RPC (please dont use them though, there terrible!) | |
| Network.DestroyPlayerObjects(player); | |
| } | |
| } | |
| [Serializable] | |
| public class NetworkPlayerRecord | |
| { | |
| public UnityEngine.NetworkPlayer NetworkPlayer; | |
| public PlayerBody PlayerBody; | |
| public NetworkViewID ID; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment