/// <summary> /// Spawn to a specific position, rotation /// </summary> /// <param name="Position">Position to spawn to.</param> /// <param name="Rotation">Direction of rotation to spawn to.</param> /// <param name="WhichTarget">Target layers to pass to the spawned game object.</param> /// <returns>The instance of the game object that was instantiated.</returns> public GameObject Spawn(Vector3 Position, Quaternion Rotation, SpawnTarget WhichTarget) { Target = WhichTarget; GameObject goInstance; if (GlobalFuncs.MAGICAL_POOL && !DoNotPool) { if (PoolSlotId > 0) // pool slot already found { goInstance = GlobalFuncs.TheMagicalPool().GatherFromPool(PoolSlotId - 1, Position, Rotation); // load from the pool } else { goInstance = GlobalFuncs.TheMagicalPool().GatherFromPool(ref PoolSlotId, Prefab, Position, Rotation, WhichTarget); // load from the pool } } else { goInstance = UnityEngine.Object.Instantiate(Prefab, Position, Rotation); // load from the prefab goInstance.SetActive(false); // disable GlobalFuncs.SetComponentTagsSlotsAndLayers(ref goInstance, WhichTarget, -1); // update the components } SetSpawnOptions(ref goInstance, PhysicsForceOptions); // set the options return(goInstance); // pass the spawned object back for potential modification }
/// <summary> /// Build the pool slot instances and settings. /// </summary> public void BuildSlot() { // create the slot string FriendlyTargetName = (Target == SpawnTarget.Any ? "" : (Target == SpawnTarget.Enemy ? "_Player" : "_AI")); goSlot = new GameObject(); // create new slot goSlot.name = "Slot(" + Prefab.name + ")" + FriendlyTargetName; // name it goSlot.transform.parent = GlobalFuncs.mpi.transform; // append the hierarchy beneath the pool slot // build initial for cloning goMasterInstance = UnityEngine.Object.Instantiate(Prefab) as GameObject; // create the master from the prefab goMasterInstance.SetActive(false); // deactivate it goMasterInstance.transform.parent = goSlot.transform; // keep it tidy in the hierarchy goMasterInstance.name = goMasterInstance.name.Replace("(Clone)", "") + FriendlyTargetName; // enforce pool return on root game object var mr = goMasterInstance.GetComponent <MagicPool_Return>(); if (!mr) { // not found create mr = goMasterInstance.AddComponent <MagicPool_Return>(); } mr.PoolSlotID = iSlotID + 1; // set slot id for return // set targeting, pool slot id on components GlobalFuncs.SetComponentTagsSlotsAndLayers(ref goMasterInstance, Target, iSlotID); // fill up the pool lgoInstances = new List <GameObject>(); // initialise the pool list PoolIsEmpty(); // create new disabled instances in the slot if (GlobalFuncs.DEBUGGING_MESSAGES) { Debug.Log("Added slot to the pool for " + Prefab.name); } }