コード例 #1
0
ファイル: Spawn.cs プロジェクト: Lankercool/unitystation
    /// <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 destination = SpawnDestination.At(target);
            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
            }
            var       prefab    = DeterminePrefab(target);
            SpawnInfo spawnInfo = SpawnInfo.Spawnable(
                SpawnablePrefab.For(prefab),
                destination);


            _ServerFireClientServerSpawnHooks(SpawnResult.Single(spawnInfo, target));
            return(target);
        }
    }