/// <summary> /// 获取/创建对象,并保存 /// </summary> /// <param name="prefab"></param> /// <param name="pos"></param> /// <returns></returns> public PoolGameObject RequestPoolGameObject(GameObject prefab, Vector3 pos) { CreatePool(); PoolGameObject temp = GetFromPool(prefab.name); if (temp == null) { GameObject go = GameObject.Instantiate(prefab); temp = go.GetOrAddComponent <PoolGameObject>(); temp.transform.position = pos; temp.SetParent(pool.transform); if (!mPool.ContainsKey(prefab.name)) { mPool.Add(prefab.name, new Queue <PoolGameObject>()); } } if (mPool[prefab.name].Count < Config.PoolCount) { mPool[prefab.name].Enqueue(temp); } else { Debug.LogWarning(prefab.name + "对象池已经超过" + Config.PoolCount + ",所以不再保存"); } return(temp); }
public PoolGameObject RequestPoolGameObject(GameObject prefab, Vector3 pos) { CreatePool(); PoolGameObject temp = GetFromPool(prefab.name); if (temp == null) { GameObject go = GameObject.Instantiate(prefab); if (!go.HadComponent <PoolGameObject>()) { temp = go.AddComponent <PoolGameObject>(); } else { temp = go.GetComponent <PoolGameObject>(); } temp.transform.position = pos; temp.SetParent(pool.transform); if (!mPool.ContainsKey(prefab.name)) { mPool.Add(prefab.name, new Queue <PoolGameObject>()); } } mPool[prefab.name].Enqueue(temp); return(temp); }
/// <summary> /// 获取/创建对象,并保存 /// </summary> /// <param name="prefabPath"></param> /// <param name="prefabName"></param> /// <param name="pos"></param> /// <returns></returns> public PoolGameObject RequestPoolGameObject(string prefabPath, string prefabName, Vector3 pos) { CreatePool(); PoolGameObject temp = GetFromPool(prefabName); if (temp == null) { temp = Resources.Load <PoolGameObject>(prefabPath); temp = GameObject.Instantiate(temp); temp.transform.position = pos; temp.SetParent(pool.transform); if (!mPool.ContainsKey(prefabName)) { mPool.Add(prefabName, new Queue <PoolGameObject>()); } } if (mPool[prefabName].Count < Config.PoolCount) { mPool[prefabName].Enqueue(temp); } else { Debug.LogWarning(prefabName + "对象池已经超过" + Config.PoolCount + ",所以不再保存"); } return(temp); }
/// <summary> /// 回收对象池 /// </summary> public void ReturnObject(PoolGameObject obj) { CreatePool(); if (obj == null) { return; } if (!mPool.ContainsKey(obj.name)) { mPool.Add(obj.name, new Queue <PoolGameObject>()); } if (mPool[prefab.name].Count < Config.PoolCount) { mPool[prefab.name].Enqueue(temp); } else { Debug.LogWarning(prefab.name + "对象池已经超过" + Config.PoolCount + ",所以不再保存"); } obj.Hide(); obj.gameObjectStatus = GameObjectStaus.SLEEP; obj.SetParent(pool.transform); }
public static PoolGameObject RequestPoolGameObject(GameObject prefab, Vector3 pos) { Instance.CreatePool(); PoolGameObject temp = GetFromPool(prefab.name); if (temp == null) { GameObject go = GameObject.Instantiate(prefab); if (!go.HadComponent <PoolGameObject>()) { temp = go.AddComponent <PoolGameObject>(); } else { temp = go.GetComponent <PoolGameObject>(); } temp.transform.position = pos; temp.SetParent(Instance.pool.transform); } Instance.SavePoolGameObject(); return(temp); }
public static PoolGameObject RequestPoolGameObject(GameObject prefab, Vector3 pos) { Instance.CreatePool(); PoolGameObject temp = GetFromPool(prefab.name); if (temp == null) { GameObject go = GameObject.Instantiate(prefab); if (!go.HadComponent <PoolGameObject>()) { temp = go.AddComponent <PoolGameObject>(); } else { temp = go.GetComponent <PoolGameObject>(); } temp.transform.position = pos; temp.SetParent(Instance.pool.transform); } if (Instance.mPool[prefab.name].Count < Config.PoolCount) { Instance.mPool[prefab.name].Enqueue(temp); } else { Debug.LogWarning("The Pool is Less then PoolCount.Don't Save it."); } return(temp); }
/// <summary> /// 回收对象池 /// </summary> public static void ReturnObject(PoolGameObject obj){ Instance.CreatePool(); if (obj==null) { return; } if (!Instance.mPool.ContainsKey(obj.name)){ Instance.mPool.Add(obj.name, new Queue<PoolGameObject>()); } Instance.mPool[obj.name].Enqueue(obj); obj.Hide(); obj.gameObjectStatus = GameObjectStaus.SLEEP; obj.SetParent(Instance.pool.transform); }
/// <summary> /// 获取/创建对象,并保存 /// </summary> /// <param name="prefabPath"></param> /// <param name="prefabName"></param> /// <param name="pos"></param> /// <returns></returns> public PoolGameObject RequestPoolGameObject(string prefabPath, string prefabName, Vector3 pos) { CreatePool(); PoolGameObject temp = GetFromPool(prefabName); if (temp == null) { temp = Resources.Load <PoolGameObject>(prefabPath); temp = GameObject.Instantiate(temp); temp.transform.position = pos; temp.SetParent(pool.transform); if (!mPool.ContainsKey(prefabName)) { mPool.Add(prefabName, new Queue <PoolGameObject>()); } } mPool[prefabName].Enqueue(temp); return(temp); }
public static PoolGameObject RequestPoolGameObject(string prefabPath, Vector3 pos) { Instance.CreatePool(); PoolGameObject temp = GetFromPool(prefabPath); if (temp == null) { GameObject go = new GameObject(); if (!go.HadComponent <PoolGameObject>()) { temp = go.AddComponent <PoolGameObject>(); } else { temp = go.GetComponent <PoolGameObject>(); } temp.transform.position = pos; temp.SetParent(Instance.pool.transform); if (!Instance.mPool.ContainsKey(prefabPath)) { Instance.mPool.Add(prefabPath, new Queue <PoolGameObject>()); } } if (Instance.mPool[prefabPath].Count < Config.PoolCount) { Instance.mPool[prefabPath].Enqueue(temp); } else { Debug.LogWarning("The Pool is Less then PoolCount.Don't Save it."); } return(temp); }