/// <summary> /// Spawn the specified prefab locally, for this client only. /// </summary> /// <param name="prefabName">name of prefab to spawn an instance of. This is intended to be made to work for pretty much any prefab, but don't /// be surprised if it doesn't as there are LOTS of prefabs in the game which all have unique behavior for how they should spawn. If you are trying /// to instantiate something and it isn't properly setting itself up, check to make sure each component that needs to set something up has /// properly implemented necessary lifecycle methods.</param> /// <param name="worldPosition">world position to appear at. Defaults to HiddenPos (hidden / invisible)</param> /// <param name="rotation">rotation to spawn with, defaults to Quaternion.identity</param> /// <param name="parent">Parent to spawn under, defaults to no parent. Most things /// should always be spawned under the Objects transform in their matrix. Many objects (due to RegisterTile) /// usually take care of properly parenting themselves when spawned so in many cases you can leave it null.</param> /// <param name="count">number of instances to spawn, defaults to 1</param> /// <param name="scatterRadius">radius to scatter the spawned instances by from their spawn position. Defaults to /// null (no scatter).</param> /// <returns>the newly created GameObject</returns> public static SpawnResult ClientPrefab(string prefabName, Vector3?worldPosition = null, Transform parent = null, Quaternion?rotation = null, int count = 1, float?scatterRadius = null) { return(Client(SpawnInfo.Prefab(prefabName, worldPosition, parent, rotation, count, scatterRadius))); }
/// <summary> /// FOR DEV / TESTING ONLY! Simulates destroying and recreating an item by putting it in the pool and taking it back /// out again. If item is not pooled, simply destroys and recreates it as if calling Despawn and then Spawn /// Can use this to validate that the object correctly re-initializes itself after spawning - /// no state should be left over from its previous incarnation. /// </summary> /// <returns>the re-created object</returns> public static GameObject ServerPoolTestRespawn(GameObject target) { var poolPrefabTracker = target.GetComponent <PoolPrefabTracker>(); if (poolPrefabTracker == null) { //destroy / create using normal approach with no pooling Logger.LogWarningFormat("Object {0} has no pool prefab tracker, thus cannot be pooled. It will be destroyed / created" + " without going through the pool.", Category.ItemSpawn, target.name); //determine prefab var position = target.TileWorldPosition(); var prefab = DeterminePrefab(target); if (prefab == null) { Logger.LogErrorFormat("Object {0} at {1} cannot be respawned because it has no PoolPrefabTracker and its name" + " does not match a prefab name, so we cannot" + " determine the prefab to instantiate. Please fix this object so that it" + " has an attached PoolPrefabTracker or so its name matches the prefab it was created from.", Category.ItemSpawn, target.name, position); return(null); } Despawn.ServerSingle(target); return(ServerPrefab(prefab, position.To3Int()).GameObject); } else { //destroy / create with pooling //save previous position var worldPos = target.TileWorldPosition(); var transform = target.GetComponent <IPushable>(); var prevParent = target.transform.parent; //this simulates going into the pool Despawn._ServerFireDespawnHooks(DespawnResult.Single(DespawnInfo.Single(target))); if (transform != null) { transform.VisibleState = false; } //this simulates coming back out of the pool target.SetActive(true); target.transform.parent = prevParent; target.transform.position = worldPos.To3Int(); var cnt = target.GetComponent <CustomNetTransform>(); if (cnt) { cnt.ReInitServerState(); cnt.NotifyPlayers(); //Sending out clientState for already spawned items } SpawnInfo spawnInfo = null; //cloth or prefab? var clothing = target.GetComponent <Clothing>(); var prefab = DeterminePrefab(target); if (clothing != null) { spawnInfo = SpawnInfo.Cloth(clothing.clothingData, worldPos.To3Int(), clothing.Type, clothing.Variant, prefab); } else { spawnInfo = SpawnInfo.Prefab(prefab, worldPos.To3Int()); } _ServerFireClientServerSpawnHooks(SpawnResult.Single(spawnInfo, target)); return(target); } }
/// <summary> /// Spawn the specified prefab, syncing it to all clients /// </summary> /// <param name="prefabName">name of prefab to spawn an instance of. This is intended to be made to work for pretty much any prefab, but don't /// be surprised if it doesn't as there are LOTS of prefabs in the game which all have unique behavior for how they should spawn. If you are trying /// to instantiate something and it isn't properly setting itself up, check to make sure each component that needs to set something up has /// properly implemented necessary lifecycle methods.</param> /// <param name="worldPosition">world position to appear at. Defaults to HiddenPos (hidden / invisible)</param> /// <param name="rotation">rotation to spawn with, defaults to Quaternion.identity</param> /// <param name="parent">Parent to spawn under, defaults to no parent. Most things /// should always be spawned under the Objects transform in their matrix. Many objects (due to RegisterTile) /// usually take care of properly parenting themselves when spawned so in many cases you can leave it null.</param> /// <param name="count">number of instances to spawn, defaults to 1</param> /// <param name="scatterRadius">radius to scatter the spawned instances by from their spawn position. Defaults to /// null (no scatter).</param> /// <param name="cancelIfImpassable">If true, the spawn will be cancelled if the location being spawned into is totally impassable.</param> /// <returns>the newly created GameObject</returns> public static SpawnResult ServerPrefab(string prefabName, Vector3?worldPosition = null, Transform parent = null, Quaternion?rotation = null, int count = 1, float?scatterRadius = null, bool cancelIfImpassable = false) { return(Server(SpawnInfo.Prefab(prefabName, worldPosition, parent, rotation, count, scatterRadius, cancelIfImpassable))); }