// ------------------------------------------- /* * InitializeLocalData */ public void InitializeLocalData(string _initialData) { if (MultiplayerConfiguration.LoadNumberOfPlayers() == 1) { InitializeWithData(_initialData); } }
// ------------------------------------------- /* * Check it the actor belongs to the current player */ public bool IsMine() { if (MultiplayerConfiguration.LoadNumberOfPlayers() != 1) { return(YourNetworkTools.Instance.GetUniversalNetworkID() == NetworkID.NetID); } else { return(true); } }
// ------------------------------------------- /* * Informs if the object belongs to the local player */ public bool IsLocalPlayer() { if (MultiplayerConfiguration.LoadNumberOfPlayers() != 1) { return(CommunicationsController.Instance.NetworkID == NetID); } else { return(true); } }
// ------------------------------------------- /* * Will dispatch an event that will destroy the object in all the network */ void OnDestroy() { #if DEBUG_MODE_DISPLAY_LOG Debug.LogError("[ActorNetwork] ++SEND++ SIGNAL FOR AUTODESTRUCTION"); #endif if (NetworkEventController.Instance != null) { NetworkEventController.Instance.NetworkEvent -= OnNetworkEvent; } if (MultiplayerConfiguration.LoadNumberOfPlayers() != 1) { NetworkEventController.Instance?.DispatchNetworkEvent(NetworkEventController.EVENT_WORLDOBJECTCONTROLLER_DESTROY_REQUEST, NetworkID.NetID.ToString(), NetworkID.UID.ToString()); } }
// ------------------------------------------- /* * Start looking for a server to work as a client */ private void Start() { Debug.Log("NetworkDiscoveryUNET::START!!!!"); if (MultiplayerConfiguration.LoadNumberOfPlayers() == 1) { broadcastData = Utilities.RandomCodeGeneration(UnityEngine.Random.Range(0, 10).ToString()); broadcastKey = UnityEngine.Random.Range(2000, 6000); broadcastPort = UnityEngine.Random.Range(20000, 90000); broadcastSubVersion = UnityEngine.Random.Range(1, 9); NetworkEventController.Instance.DelayLocalEvent(NetworkEventController.EVENT_COMMUNICATIONSCONTROLLER_REGISTER_ALL_NETWORK_PREFABS, 0.2f); BasicSystemEventController.Instance.DelayBasicSystemEvent(CommunicationsController.EVENT_COMMSCONTROLLER_SET_UP_IS_SERVER, 0.3f); return; } // Initializes NetworkDiscovery. Initialize(); if (!CheckComponents()) { Debug.Log("Invalid configuration detected. Network Discovery disabled."); Destroy(this); return; } broadcastInterval = BroadcastInterval; // Start listening for broadcasts. StartAsClient(); float InvokeWaitTime = 3 * BroadcastInterval + Random.value * 3 * BroadcastInterval; Invoke("MaybeInitAsServer", InvokeWaitTime * 0.001f); NetworkEventController.Instance.NetworkEvent += new NetworkEventHandler(OnBasicEvent); }
// ------------------------------------------- /* * Returns the network identificator independently if it's a local or a remote game */ public int GetUniversalNetworkID() { if (IsLocalGame) { if (MultiplayerConfiguration.LoadNumberOfPlayers() != 1) { return(CommunicationsController.Instance.NetworkID); } else { return(1); } } else { #if ENABLE_PHOTON return(PhotonController.Instance.UniqueNetworkID); #elif ENABLE_NAKAMA return(NakamaController.Instance.UniqueNetworkID); #else return(ClientTCPEventsController.Instance.UniqueNetworkID); #endif } }
// ------------------------------------------- /* * Manager of global events */ private void OnNetworkEvent(string _nameEvent, bool _isLocalEvent, int _networkOriginID, int _networkTargetID, params object[] _list) { if (_nameEvent == EVENT_YOURNETWORKTOOLS_INITIALITZATION_DATA) { string targetNetworkID = (string)_list[0]; string initialDataNetwork = (string)_list[1]; ActorNetwork[] networkActors = GameObject.FindObjectsOfType <ActorNetwork>(); // Debug.LogError("+++++++++++++EVENT_YOURNETWORKTOOLS_INITIALITZATION_DATA::TARGET[" + targetNetworkID + "]::data[" + initialDataNetwork + "]::TOTAL NETWORK ACTORS["+ networkActors.Length + "]"); for (int i = 0; i < networkActors.Length; i++) { IGameNetworkActor networkActor = networkActors[i].GetComponentInParent <IGameNetworkActor>(); if (networkActor != null) { if (networkActor.NetworkID.CheckID(targetNetworkID)) { networkActor.Initialize(initialDataNetwork); } } } } if (_nameEvent == ClientTCPEventsController.EVENT_CLIENT_TCP_ESTABLISH_NETWORK_ID) { #if ENABLE_BALANCE_LOADER int totalPlayersConfigurated = MultiplayerConfiguration.LoadNumberOfPlayers(); if (totalPlayersConfigurated != MultiplayerConfiguration.VALUE_FOR_JOINING) { string friends = MultiplayerConfiguration.LoadFriendsGame(); if (friends.Length > 0) { string[] friendIDs = friends.Split(','); int idRoomLobby = MultiplayerConfiguration.LoadRoomNumberInServer(-1); ClientTCPEventsController.Instance.CreateRoomForFriends(idRoomLobby, friendIDs, ""); } else { string nameRoomLobby = MultiplayerConfiguration.LoadNameRoomLobby(); if (nameRoomLobby.Length > 0) { int idRoomLobby = MultiplayerConfiguration.LoadRoomNumberInServer(-1); ClientTCPEventsController.Instance.CreateRoomForLobby(idRoomLobby, nameRoomLobby, totalPlayersConfigurated, ""); } else { throw new Exception("THERE IS NO NAME OF LOBBY TO CREATE A TCP CONNECTION"); } } } else { int idRoomLobby = MultiplayerConfiguration.LoadRoomNumberInServer(-1); if (idRoomLobby != -1) { if (MultiplayerConfiguration.LoadIsRoomLobby()) { ClientTCPEventsController.Instance.JoinRoomOfLobby(idRoomLobby, "null", ""); } else { ClientTCPEventsController.Instance.JoinRoomForFriends(idRoomLobby, "null", ""); } } else { throw new Exception("NO GOOD"); } } #endif } if (_nameEvent == ClientTCPEventsController.EVENT_CLIENT_TCP_CONNECTED_ROOM) { // Debug.LogError("EVENT_CLIENT_TCP_CONNECTED_ROOM::UniversalUniqueID[" + GetUniversalNetworkID() + "]"); } if (_nameEvent == NetworkEventController.EVENT_SYSTEM_INITIALITZATION_REMOTE_COMPLETED) { if (IsServer) { // Debug.LogError("++++++++++++++++++++SENDING INFORMATION ABOUT ALL EXISTING NETWORK OBJECTS+++++++++++++++++++++++++++++"); CheckInitializationObjects(); } } if (_nameEvent == NetworkEventController.EVENT_WORLDOBJECTCONTROLLER_LOCAL_CREATION_CONFIRMATION) { if (IsServer) { string keyNetworkGO = (string)_list[0]; CheckInitializationObjects(keyNetworkGO); } } if (_nameEvent == NetworkEventController.EVENT_WORLDOBJECTCONTROLLER_INITIAL_DATA) { if (IsServer) { if (!m_initialData.ContainsKey((string)_list[0])) { string keyNetworkGO = (string)_list[0]; string dataNetworkGO = (string)_list[1]; m_initialData.Add(keyNetworkGO, dataNetworkGO); // Debug.LogError("*************************************DATA ADDED TO LIST(" + keyNetworkGO + ")("+ dataNetworkGO + ")::TOTAL INITIAL DATA["+ m_initialData.Count + "]::TOTAL TCP PLAYERS["+m_tcpNetworkObjects.Count+"]"); CheckInitializationObjects(keyNetworkGO); } } } if (_nameEvent == EVENT_YOURNETWORKTOOLS_NETID_NEW) { if (IsServer) { // Debug.LogError("*************************************NEW TCP NETWORK OBJECT REGISTERED(" + m_tcpNetworkObjects.Count + "]"); CheckInitializationObjects(); } } if (_nameEvent == NetworkEventController.EVENT_WORLDOBJECTCONTROLLER_DESTROY_REQUEST) { DestroyNetworkObject(int.Parse((string)_list[0]), int.Parse((string)_list[1])); } if (_nameEvent == NetworkEventController.EVENT_COMMUNICATIONSCONTROLLER_CREATION_CONFIRMATION_NETWORK_OBJECT) { #if !DISABLE_UNET_COMMS m_unetNetworkObjects.Add(new NetworkWorldObject((GameObject)_list[0])); #endif } if (_nameEvent == NetworkEventController.EVENT_PLAYERCONNECTIONDATA_USER_DISCONNECTED) { Debug.Log("----------------------DISCONNECTED PLAYER[" + (int)_list[0] + "]"); } #if ENABLE_PHOTON if (_nameEvent == EVENT_YOURNETWORKTOOLS_CREATED_GAMEOBJECT) { GameObject newGO = (GameObject)_list[0]; if (!m_tcpNetworkObjects.Contains(newGO)) { m_tcpNetworkObjects.Add(newGO); NetworkEventController.Instance.DispatchLocalEvent(EVENT_YOURNETWORKTOOLS_NETID_NEW); } } #endif if (_nameEvent == ClientTCPEventsController.EVENT_CLIENT_TCP_TRANSFORM_DATA) { int NetID = (int)_list[0]; int UID = (int)_list[1]; int prefabIndex = (int)_list[2]; Vector3 position = (Vector3)_list[3]; Vector3 forward = (Vector3)_list[4]; Vector3 scale = (Vector3)_list[5]; object networkObject = GetNetworkObjectByID(NetID, UID); GameObject networkGameObject = null; if (networkObject == null) { m_networkIDReceived = NetID; networkGameObject = Utilities.AddChild(this.gameObject.transform, GetPrefabByName(GameObjects[prefabIndex].name)); networkGameObject.GetComponent <NetworkID>().IndexPrefab = GetPrefabIndexOfName(GameObjects[prefabIndex].name); networkGameObject.GetComponent <NetworkID>().NetID = NetID; networkGameObject.GetComponent <NetworkID>().UID = UID; m_tcpNetworkObjects.Add(networkGameObject); networkGameObject.transform.position = position; networkGameObject.transform.forward = forward; networkGameObject.transform.localScale = scale; NetworkEventController.Instance.DispatchLocalEvent(EVENT_YOURNETWORKTOOLS_NETID_NEW); } else { networkGameObject = (GameObject)networkObject; InterpolatorController.Instance.Interpolate(networkGameObject, position, TimeToUpdateTransforms * 1.01f); InterpolatorController.Instance.InterpolateForward(networkGameObject, forward, TimeToUpdateTransforms * 1.01f); networkGameObject.transform.localScale = scale; } } if (_nameEvent == EVENT_YOURNETWORKTOOLS_DESTROYED_GAMEOBJECT) { int NetID = (int)_list[1]; int UID = (int)_list[2]; if (IsLocalGame) { #if !DISABLE_UNET_COMMS for (int i = 0; i < m_unetNetworkObjects.Count; i++) { bool removeObject = false; if (m_unetNetworkObjects[i] == null) { removeObject = true; } else { if (m_unetNetworkObjects[i].GetNetworkObjectData() == null) { removeObject = true; } } if (removeObject) { m_unetNetworkObjects.RemoveAt(i); } else { if ((m_unetNetworkObjects[i].GetNetworkObjectData().NetID == NetID) && (m_unetNetworkObjects[i].GetNetworkObjectData().UID == UID)) { m_unetNetworkObjects[i].Destroy(); m_unetNetworkObjects.RemoveAt(i); return; } } } #endif } else { for (int i = 0; i < m_tcpNetworkObjects.Count; i++) { bool removeObject = false; if (m_tcpNetworkObjects[i] == null) { removeObject = true; } if (removeObject) { m_tcpNetworkObjects.RemoveAt(i); } else { if ((m_tcpNetworkObjects[i].GetComponent <NetworkID>().NetID == NetID) && (m_tcpNetworkObjects[i].GetComponent <NetworkID>().UID == UID)) { m_tcpNetworkObjects.RemoveAt(i); return; } } } } } }
// ------------------------------------------- /* * Converts the normal GameObjects in Network GameObjects */ void Start() { System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = "."; System.Threading.Thread.CurrentThread.CurrentCulture = customCulture; NetworkEventController.Instance.NetworkEvent += new NetworkEventHandler(OnNetworkEvent); int isLocalGame = PlayerPrefs.GetInt(COOCKIE_IS_LOCAL_GAME, -1); if (isLocalGame == -1) { IsLocalGame = true; } else { IsLocalGame = false; } if (IsLocalGame) { if (MultiplayerConfiguration.LoadNumberOfPlayers() != 1) { // INSTANTIATE LOCAL NETWORK PREFAB MANAGERS for (int j = 0; j < LocalNetworkPrefabManagers.Length; j++) { Utilities.AddChild(transform, LocalNetworkPrefabManagers[j]); } // NETWORK VARIABLES MANAGER Utilities.AddChild(transform, NetworkVariablesManager); // ASSIGN THE GAME OBJECTS TO THE CONTROLLER WorldObjectController worldObjectController = GameObject.FindObjectOfType <WorldObjectController>(); if (worldObjectController != null) { int totalNumberLocalObjects = 0; for (int i = 0; i < GameObjects.Length; i++) { if (GameObjects[i].GetComponent <NetworkWorldObjectData>() != null) { totalNumberLocalObjects++; } } int counterAppWorldObjects = 0; worldObjectController.AppWorldObjects = new GameObject[totalNumberLocalObjects]; for (int i = 0; i < GameObjects.Length; i++) { GameObject prefabToNetwork = GameObjects[i]; if (prefabToNetwork.GetComponent <NetworkWorldObjectData>() != null) { prefabToNetwork.GetComponent <NetworkWorldObjectData>().enabled = true; #if ENABLE_MIRROR GameObject.FindObjectOfType <MirrorDiscoveryController>().spawnPrefabs.Add(prefabToNetwork); #endif if (prefabToNetwork.GetComponent <NetworkID>() != null) { prefabToNetwork.GetComponent <NetworkID>().enabled = false; } if (prefabToNetwork.GetComponent <ActorNetwork>() == null) { prefabToNetwork.AddComponent <ActorNetwork>(); } worldObjectController.AppWorldObjects[counterAppWorldObjects] = prefabToNetwork; counterAppWorldObjects++; } } } } else { NetworkEventController.Instance.DelayLocalEvent(NetworkEventController.EVENT_SYSTEM_INITIALITZATION_LOCAL_COMPLETED, 0.2f, 1); } } else { #if !ENABLE_PHOTON && !ENABLE_NAKAMA // CONNECT TO THE SERVER ClientTCPEventsController.Instance.Initialitzation(MultiplayerConfiguration.LoadIPAddressServer(), MultiplayerConfiguration.LoadPortServer(), MultiplayerConfiguration.LoadRoomNumberInServer(0), MultiplayerConfiguration.LoadMachineIDServer(0), MultiplayerConfiguration.LoadBufferSizeReceive(), MultiplayerConfiguration.LoadTimeoutReceive(), MultiplayerConfiguration.LoadBufferSizeSend(), MultiplayerConfiguration.LoadTimeoutSend()); // NETWORK VARIABLES MANAGER Utilities.AddChild(transform, NetworkVariablesManager); #elif !ENABLE_PHOTON && ENABLE_NAKAMA NakamaController.Instance.Initialitzation(); #endif // ADD NETWORK IDENTIFICATION TO THE GAME OBJECTS for (int i = 0; i < GameObjects.Length; i++) { GameObject prefabToNetwork = GameObjects[i]; if (prefabToNetwork.GetComponent <NetworkID>() == null) { prefabToNetwork.AddComponent <NetworkID>(); } else { prefabToNetwork.GetComponent <NetworkID>().enabled = true; } #if !DISABLE_UNET_COMMS if (prefabToNetwork.GetComponent <NetworkWorldObjectData>() != null) { prefabToNetwork.GetComponent <NetworkWorldObjectData>().enabled = false; } #endif if (prefabToNetwork.GetComponent <ActorNetwork>() == null) { prefabToNetwork.AddComponent <ActorNetwork>(); } } } m_hasBeenInitialized = true; NetworkEventController.Instance.DispatchLocalEvent(EVENT_YOURNETWORKTOOLS_CONTROLLER_STARTED); }
// ------------------------------------------- /* * Will load the number of players */ public int MenuController_LoadNumberOfPlayers() { return(MultiplayerConfiguration.LoadNumberOfPlayers()); }