public void RpcSplit(GameObject obj, GameObject split, float angle, bool hasAuthority, float splitFactor) { //We do not call on NetworkServer methods here. This is used only to sync up with the original game unit for all clients. //This includes adding the newly spawned game unit into the Selection Manager that handles keeping track of all game units. if (obj == null || split == null) { return; } GameUnit original = obj.GetComponent <GameUnit>(); GameUnit copy = split.GetComponent <GameUnit>(); if (original.unitAttributes == null) { GameObject attrObj = GameObject.FindGameObjectWithTag("UnitAttributes"); if (obj != null) { original.unitAttributes = attrObj.GetComponent <UnitAttributes>(); if (original.unitAttributes == null) { Debug.LogError("Unit attributes are missing from original unit."); } } } if (copy.unitAttributes == null) { GameObject attrObj = GameObject.FindGameObjectWithTag("UnitAttributes"); if (obj != null) { copy.unitAttributes = attrObj.GetComponent <UnitAttributes>(); if (copy.unitAttributes == null) { Debug.LogError("Unit attributes are missing from copy unit."); } } } NavMeshAgent originalAgent = obj.GetComponent <NavMeshAgent>(); originalAgent.ResetPath(); NavMeshAgent copyAgent = split.GetComponent <NavMeshAgent>(); copyAgent.ResetPath(); GameObject[] splitManagerGroup = GameObject.FindGameObjectsWithTag("SplitManager"); if (splitManagerGroup.Length > 0) { for (int i = 0; i < splitManagerGroup.Length; i++) { SplitManager manager = splitManagerGroup[i].GetComponent <SplitManager>(); if (manager != null && manager.hasAuthority == hasAuthority) { manager.splitGroupList.Add(new SplitGroup(original, copy, angle, splitFactor)); if (manager.selectionManager == null) { GameObject[] objs = GameObject.FindGameObjectsWithTag("SelectionManager"); foreach (GameObject select in objs) { SelectionManager selectManager = select.GetComponent <SelectionManager>(); if (selectManager.hasAuthority) { manager.selectionManager = selectManager; } } } manager.selectionManager.allObjects.Add(split); } } } }
public void ServerInitialize() { //Server code //This is run for spawning new non-player objects. Since it is a server calling to all clients (local and remote), it needs to pass in a //NetworkConnection that connects from server to THAT PARTICULAR client, who is going to own client authority on the spawned object. //Checking if global manager exists; GlobalManager globalManagerObject = GameObject.FindObjectOfType <GlobalManager>(); if (globalManagerObject != null) { this.doesGlobalManagerExist = true; } else { this.doesGlobalManagerExist = false; } //Setting up Player GameObject playerUmbrellaObject = new GameObject("Player"); GameObject playerUnitUmbrellaObject = new GameObject("Units"); playerUnitUmbrellaObject.transform.SetParent(playerUmbrellaObject.transform); playerUmbrellaObject.tag = "Player"; //Player unit GameObject playerObject = MonoBehaviour.Instantiate(this.spawnPrefab) as GameObject; playerObject.transform.SetParent(playerUnitUmbrellaObject.transform); playerObject.transform.position = this.transform.position; NetworkIdentity objIdentity = playerObject.GetComponent <NetworkIdentity>(); NetworkServer.SpawnWithClientAuthority(playerObject, this.connectionToClient); //Player selection manager GameObject manager = MonoBehaviour.Instantiate(this.selectionManagerPrefab) as GameObject; manager.transform.SetParent(playerUmbrellaObject.transform); SelectionManager selectionManager = manager.GetComponent <SelectionManager>(); if (selectionManager != null) { selectionManager.allObjects.Add(playerObject); selectionManager.authorityOwner = objIdentity.clientAuthorityOwner; } NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient); //Player split manager manager = MonoBehaviour.Instantiate(this.splitManagerPrefab) as GameObject; manager.transform.SetParent(playerUmbrellaObject.transform); SplitManager splitManager = manager.GetComponent <SplitManager>(); if (splitManager != null) { splitManager.selectionManager = selectionManager; splitManager.unitParent = playerUnitUmbrellaObject.transform; if (this.doesGlobalManagerExist && globalManagerObject != null) { splitManager.globalManagerObject = globalManagerObject; splitManager.maxUnitCount = globalManagerObject.playerMaxUnitCount; } } NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient); //Player merge manager manager = MonoBehaviour.Instantiate(this.mergeManagerPrefab) as GameObject; manager.transform.SetParent(playerUmbrellaObject.transform); MergeManager mergeManager = manager.GetComponent <MergeManager>(); if (mergeManager != null) { mergeManager.selectionManager = selectionManager; } NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient); //Unit Attributes Tracker manager = MonoBehaviour.Instantiate(this.unitAttributesPrefab) as GameObject; manager.transform.SetParent(playerUmbrellaObject.transform); UnitAttributes attributes = manager.GetComponent <UnitAttributes>(); if (attributes != null) { splitManager.unitAttributes = attributes; mergeManager.unitAttributes = attributes; } NetworkServer.SpawnWithClientAuthority(manager, this.connectionToClient); RpcCameraSetup(playerObject); int colorValue; switch (Spawner.colorCode) { default: colorValue = -1; break; case 0: colorValue = 0; break; case 1: colorValue = 1; break; case 2: colorValue = 2; break; } Spawner.colorCode++; if (Spawner.colorCode > 2) { Spawner.colorCode = 0; } RpcUnitAttributesSetup(manager, playerObject, colorValue); NetworkServer.SpawnWithClientAuthority(this.remotePrefab, this.connectionToClient); }