public T Spawn <T>(T prefab, Transform root, out bool fromPool) where T : Component { var key = prefab.GetHashCode(); if (this.registeredPrefabs.Contains(key) == false) { fromPool = false; return(Object.Instantiate(prefab, root)); } if (this.prefabToPooledInstances.TryGetValue(key, out var stack) == true && stack.Count > 0) { var instance = stack.Pop(); UIWSUtils.SetParent(instance.transform, root); if (instance.gameObject.activeSelf == false) { instance.gameObject.SetActive(true); } this.instanceOnSceneToPrefab.Add(instance, key); fromPool = true; return((T)instance); } else { var instance = Object.Instantiate(prefab, root); if (instance.gameObject.activeSelf == false) { instance.gameObject.SetActive(true); } this.instanceOnSceneToPrefab.Add(instance, key); fromPool = false; return(instance); } }
public void Despawn <T>(T instance, System.Action <T> onDestroy = null) where T : Component { if (this.instanceOnSceneToPrefab.TryGetValue(instance, out var prefabKey) == true) { this.instanceOnSceneToPrefab.Remove(instance); if (this.prefabToPooledInstances.TryGetValue(prefabKey, out var stack) == true) { stack.Push(instance); } else { stack = new Stack <Component>(); stack.Push(instance); this.prefabToPooledInstances.Add(prefabKey, stack); } instance.gameObject.SetActive(false); UIWSUtils.SetParent(instance.transform, this.transform); } else { if (onDestroy != null) { onDestroy.Invoke(instance); } Object.Destroy(instance.gameObject); } }
public static void SetParent(Transform transform, Transform parent) { var data = UIWSUtils.GetTransformData(transform); transform.SetParent(parent); UIWSUtils.SetTransformData(transform, data); }