/// <summary>Called when a client wants the host to spawn a scene object, it spawns a sceneobject based on given information</summary> private void OnSceneObjectSpawn(object content) { if (PhotonNetwork.IsMasterClient) { CustomSpawnInfo info = (CustomSpawnInfo)content; PhotonNetwork.InstantiateSceneObject(info.PrefabId, info.Position, info.Rotation); } }
/// <summary>Instantiates an object owned by the scene accross the network using given prefabName, position and rotation. This can be a static or a movable networked object</summary> public static GameObject InstantiateSceneObject(string prefabName, Vector3 position, Quaternion rotation) { if (settings.IsStaticObject(prefabName)) { //static objects are spawned using the static objects spawn event type. CustomSpawnInfo info = new CustomSpawnInfo(prefabName, position, rotation); eventHandler.RaiseEvent((byte)InternalEvent.StaticObjectSpawn, info, ReceiverGroup.All, true); return(null); } else if (!PhotonNetwork.IsMasterClient) { //objects spawned as scene object by a non host client will be converted into an event send to the host to spawn it instead CustomSpawnInfo info = new CustomSpawnInfo(prefabName, position, rotation); int[] targets = new int[] { PhotonNetwork.CurrentRoom.MasterClientId }; eventHandler.RaiseEvent((byte)InternalEvent.SceneObjectSpawn, info, targets, true); return(null); } return(PhotonNetwork.InstantiateSceneObject(prefabName, position, rotation)); }
/// <summary>Called when an event has been raised to spawn a static object, it will create one using the given information</summary> private void OnStaticObjectSpawn(object content) { CustomSpawnInfo info = (CustomSpawnInfo)content; //try getting a new resource from the poolad game objects stored in the queue GameObject instance = null; ConcurrentQueue <GameObject> pooledGameObjects = resourcePoolDict[info.PrefabId]; if (pooledGameObjects.TryDequeue(out instance)) { instance.transform.position = info.Position; instance.transform.rotation = info.Rotation; } else { //if there are no avaialbe pooled game objects, the used resource for instantiation will be the original prefab GameObject res = null; foreach (GameObject prefab in settings.StaticNetworkedObjects) { if (prefab.name == info.PrefabId) { res = prefab; break; } } instance = GameObject.Instantiate(res, info.Position, info.Rotation); instance.name = res.name; //set static object view id and add it to stored object dictionary searches StaticNetworkedObject obj = instance.GetComponent <StaticNetworkedObject>(); obj.SetViewId(staticObjectDict.Count); staticObjectDict.Add(obj.ViewId, instance); } instance.SetActive(true); }