private GameObject InternalSpawn(GameObject prefab, Vector3 position, Quaternion rotation) { PoolTable table = GetOrCreateTable(prefab); GameObject obj = table.Spawn(position, rotation); poolInstances[obj] = new PoolInstance(obj, true, table); return(obj); }
/// <summary> /// Includes a previously excluded object in the pool. /// </summary> /// <param name="game_object"></param> public void IncludePooledObject(GameObject game_object) { if (!ExcludedPoolTable.Contains(game_object)) { return; } ExcludedPoolTable.Remove(game_object); PoolTable.Add(game_object); }
/// <summary> /// Makes a new generation and picks a variable of any given table. /// </summary> public static GameObject Fetch(PoolTable poolTable) { // Defines local variables for this calculation. int totalChanceSum = 0; int[] intervals = new int[poolTable.poolVariables.Count + 1]; #region Weight Sums // Gets the weight of each variable and get's the interval of each object. for (int i = 0; i < poolTable.poolVariables.Count; i++) { totalChanceSum += poolTable.poolVariables[i].chance; intervals[i] = totalChanceSum; } #endregion // Generates a random value from all the weight limit. int randomValue = Random.Range(0, totalChanceSum + 1); #region Interval Calculations // Checks if the value is in any interval. for (int i = 0; i < intervals.Length - 1; i++) { // Assigns the upper and lower limit of the random check. int lowerLimit = 0; int upperLimit = 0; // Fail-safe in case this is the first object being checked, so it doesn't give a non-existing // memory error. if (i == 0) { lowerLimit = 0; } else { lowerLimit = intervals[i - 1]; } // Assigns the upper limit. upperLimit = intervals[i]; // Checks if the random value belong to this limit. if (randomValue >= lowerLimit && randomValue <= upperLimit) { return(poolTable.poolVariables[i].variable); } } #endregion /* Returns null in worse case scenario. This only happens if we do something stupid. * / Let's not do something stupid. * / Please. */ Debug.Log("Pool didn't find a variable!"); return(null); }
private void DoReplace(GameObject prefab, GameObject otherPrefab) { Debug.Log("Replacing " + prefab.name + " with " + otherPrefab.name); PoolTable table; if (!poolTables.TryGetValue(prefab, out table)) { Debug.LogError("Prefab does not exist to replace: " + prefab.name + " with: " + otherPrefab.name); return; } if (table.Prefab == otherPrefab) { Debug.Log("Prefab to replace already matches the new prefab, ignoring"); return; } // Despawn current instances foreach (var pair in poolInstances) { if (pair.Value.Table == table) { pair.Value.InUse = false; table.Despawn(pair.Key); } } // Process despawns next update Instance.enabled = true; // Check overriden pool tables so see if other prefab already has a table PoolTable otherTable; if (overriddenPoolTables.TryGetValue(otherPrefab, out otherTable)) { Debug.Log("Using existing overridden pool table"); overriddenPoolTables.Remove(otherPrefab); } else { Debug.Log("Creating new pool table"); otherTable = new PoolTable(otherPrefab); items.Add(otherTable); // Preallocate the same number of instances otherTable.Preallocate(table.ActiveCount + table.FreeCount); } // Move the old table to the overriden tables overriddenPoolTables[table.Prefab] = table; // Replace the pool table reference poolTables[prefab] = otherTable; }
PoolTable GetOrCreateTable(GameObject prefab) { PoolTable table; if (!poolTables.TryGetValue(prefab, out table)) { table = new PoolTable(prefab); poolTables[prefab] = table; items.Add(table); } return(table); }
// -- PRIVATE // .. OPERATIONS GameObject CreateNewObject() { GameObject new_game_object; new_game_object = (GameObject)GameObject.Instantiate(PrefabToClone); new_game_object.SetActive(false); new_game_object.GetComponent <Transform>().SetParent(GetComponent <Transform>(), false); PoolTable.Add(new_game_object); return(new_game_object); }
private void DoDespawn(GameObject obj) { PoolInstance inst; if (poolInstances.TryGetValue(obj, out inst)) { PoolTable table = inst.Table; if (table != null) { inst.InUse = false; table.Despawn(obj); enabled = true; return; } } //Debug.LogError("Could not find obj to despawn in pool: " + obj.name); GameObject.Destroy(obj); }
private void InternalDespawn(GameObject obj) { PoolInstance ins; if (poolInstances.TryGetValue(obj, out ins)) { PoolTable table = ins.Table; if (table != null) { ins.InUse = false; table.Despawn(obj); return; } } #if DEBUG Debug.LogWarning("Cannot find any " + obj.name + " in pool to despawn"); #endif Destroy(obj); }
///<summary> ///Prepare Sound Asset ///</summary> public void PrepareAssets(string _tag, Transform _parentTrans = null) { if (poolTable == null) { poolTable = ResourceManager.LoadAsset(tablePath, "PoolTable", resLinkType) as PoolTable; if (poolTable == null) { Debug.LogError("Not found PoolTable"); return; } } if (poolDic.ContainsKey(_tag)) { return; } PoolTable.PoolInfo poolInfo = poolTable.GetPoolInfo(_tag); if (poolInfo == null) { Debug.LogError("Prepare Faild. Not found pool info : " + _tag); return; } GameObject newPrefab = ResourceManager.LoadAsset(poolInfo.path, poolInfo.tag, resLinkType) as GameObject; prefabDic.Add(poolInfo.tag, newPrefab); for (int i = 0; i < poolInfo.preloadCount; i++) { GameObject newObj = CreatePoolObject(poolInfo.tag, _parentTrans); if (newObj == null) { Debug.LogError("Prepare Faild. Not found PoolObject : " + _tag); return; } } Debug.Log("Prepare Success PoolObject : " + _tag); }
public PoolInstance(GameObject instance, bool inUse, PoolTable table) { this.instance = instance; this.inUse = inUse; this.table = table; }