void SV_CreateCar(int slot, PlayerType typ) { // find spawn point SpawnArea area = spawnZone.GetSpawn(slot); // create the player car NetworkPrefab prefab = NetworkManager.FindNetworkPrefab("player_car"); GameObject racerObj = NetworkManager.singleton.NetworkInstantiate(prefab, area.position, area.rotation, false); BaseCar car = racerObj.GetComponent <BaseCar>(); // get a player for this car GamePlayer player = PlayerInputManager.MakePlayerForCar(car); car.pDriver = player; // create an entry for this racer RaceEntry entry = new RaceEntry(car, slot, "Racer " + slot, typ); car.entry = entry; car.visuals.carNum = slot; // update the visuals car.ApplyVisuals(); cars[slot] = car; }
void OnGUI() { if (GUI.Button(new Rect(300, 0, 100, 30), "Spawn Ball")) { NetworkPrefab ball = NetworkManager.FindNetworkPrefab("ball"); NetworkManager.singleton.NetworkInstantiate(ball, Vector3.zero, Quaternion.identity, false); } }
public override void ServerStart() { NetworkPrefab prefab = NetworkManager.FindNetworkPrefab("client_inputs"); NetworkManager.singleton.NetworkInstantiate(prefab, Vector3.zero, Quaternion.identity, false); Debug.Log("Creating cars..."); gameController.SV_CreateCars(numRacers); }
private void RegisterAllPrefabs() { networkPrefabs = new List <NetworkPrefab>(); for (int i = 0; i < editorPrefabs.Length; i++) { NetworkPrefab prefab = new NetworkPrefab(i, editorPrefabs[i]); networkPrefabs.Add(prefab); } }
/// <summary> /// Instantiates a prefab and assigns a network ID to it. /// This is replicated across all peers, and is buffered for any new players. /// To destroy these objects, you should use the "NetworkDestroy" function. /// </summary> /// <param name="pos">Position.</param> /// <param name="rot">Rot.</param> public GameObject NetworkInstantiate(NetworkPrefab prefab, Vector3 pos, Quaternion rot, bool sendToLocalClient) { if (NetworkCore.isServer) { if (prefab == null) { Debug.LogError("NetworkInstantiate called with no prefab. Object not created"); return(null); } GameObject newObj = Instantiate(prefab.serverPrefab, pos, rot); NetObjectID idComp = newObj.GetComponent <NetObjectID>(); NetworkID newID = GetNextID(idComp); idComp.SetNetID(newID); NetWriter writer = StartNetworkMessage("cl_NetworkInstantiate", thisNetworkID); writer.WriteInt(prefab.id); writer.WriteVector3(pos); writer.WriteVector3(rot.eulerAngles); newID.WriteBytes(writer); BufferedMessage msg = null; if (sendToLocalClient) { msg = SendMessageToAllClients(writer, NetworkCore.AllCostMsg, true); } else { msg = SendMessageToOtherClients(writer, NetworkCore.AllCostMsg, true); } if (msg == null) { Debug.Log("Failed to successfully send Instantiation message. Unregistering Network ID: " + newID.ToString()); FreeNetworkID(idComp); return(newObj); } idComp.AddBufferedMessage(msg); newObj.BroadcastMessage("OnNewNetworkSceneLoaded"); return(newObj); } return(null); }