// ---------------------------------------------------------------------------------------------------- #endregion #region Spawning Methods // ---------------------------------------------------------------------------------------------------- /// <summary> /// Spawns a new instance. /// </summary> public GameObject Spawn(Vector3 position, Quaternion rotation, Vector3 scale) { // Fetch next available instance FeatherPoolInstance poolInstance = NextAvailableInstance; if (poolInstance == null) { return(null); } // Signal if implementing poolInstance.InvokeOnSpawning(); // Set Transform poolInstance.SetPosition(position); poolInstance.SetRotation(rotation); poolInstance.SetScale(scale); // Signal if implementing poolInstance.InvokeResettable(); // Signal if implementing poolInstance.InvokeOnSpawned(); // Activate instance poolInstance.SetActive(); // Return instance return(poolInstance.PrefabInstance); }
// ---------------------------------------------------------------------------------------------------- #endregion #region Despawn Methods // ---------------------------------------------------------------------------------------------------- /// <summary> /// Despawns and returns an instance. /// </summary> /// <param name="instance">The instance.</param> public void Despawn(FeatherPoolInstance instance) { // Check if object already despwned if (!this.activeInstances.Contains(instance)) { return; } // Remove instance from active instances and makes it available this.activeInstances.Remove(instance); this.availableInstances.Push(instance); // Signal if implementing instance.InvokeOnDespawning(); // Reset Object instance.PrefabInstance.SetActive(false); instance.PrefabInstance.transform.SetParent(this.transform); instance.ResetTransform(); // Signal if implementing instance.InvokeOnDespawning(); #if UNITY_EDITOR UpdateProgressBar(); #endif }
/// <summary> /// Tries the get a FeatherPoolInstance containing the GameObject. /// </summary> /// <param name="pooledObject">The game object.</param> /// <returns>FeatherPoolInstance.</returns> private FeatherPoolInstance TryGetInstance(GameObject pooledObject) { FeatherPoolInstance instance = null; this.instanceDictionary.TryGetValue(pooledObject.GetInstanceID(), out instance); return(instance); }
/// <summary> /// Tries the get a FeatherPoolInstance containing the GameObject. /// </summary> /// <param name="pooledObjectID">The game object.</param> /// <returns>FeatherPoolInstance.</returns> private FeatherPoolInstance TryGetInstance(int pooledObjectID) { FeatherPoolInstance instance = null; this.instanceDictionary.TryGetValue(pooledObjectID, out instance); return(instance); }
/// <summary> /// Determines whether the specified game object is available. /// </summary> /// <param name="pooledObject">The game object.</param> /// <returns><c>true</c> if the specified game object is available; otherwise, <c>false</c>.</returns> public bool IsAvailable(GameObject pooledObject) { FeatherPoolInstance instance = TryGetInstance(pooledObject); if (instance != null) { return(this.availableInstances.Contains(instance)); } return(false); }
/// <summary> /// Determines whether the specified game object is active. /// </summary> /// <param name="pooledObjectID">The game object.</param> /// <returns><c>true</c> if the specified game object is active; otherwise, <c>false</c>.</returns> public bool IsActive(int pooledObjectID) { FeatherPoolInstance instance = TryGetInstance(pooledObjectID); if (instance != null) { return(this.activeInstances.Contains(instance)); } return(false); }
/// <summary> /// Despawns and returns an instance. /// </summary> /// <param name="pooledObject">The gameObject.</param> public void Despawn(GameObject pooledObject) { FeatherPoolInstance instance = TryGetInstance(pooledObject); if (instance != null) { this.Despawn(instance); } else { Debug.LogError("[FeatherPool] Tried despawning object <b>" + gameObject.name + "</b>, but object not part of this pool."); } }
/// <summary> /// Adds a new instance. /// </summary> /// <param name="newInstance">The new instance.</param> private void AddNewInstance(GameObject newInstance) { Transform instanceTransform = newInstance.transform; instanceTransform.name = PrefabObject.name; instanceTransform.SetParent(this.transform); newInstance.SetActive(false); FeatherPoolInstance newItemInstance = new FeatherPoolInstance(newInstance); availableInstances.Push(newItemInstance); instanceDictionary.Add(newInstance.GetInstanceID(), newItemInstance); }