Skip to content

Commit

Permalink
1、完善PoolManager
Browse files Browse the repository at this point in the history
  • Loading branch information
KingSun5 committed Apr 9, 2019
1 parent 82c8ef2 commit 269ccfd
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 26 deletions.
15 changes: 1 addition & 14 deletions Assets/Scenes/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public class ObjectPool<T>
/// </summary>
private Func<T> _returnT;
/// <summary>
/// 刷新清理时间 负数不清理
/// </summary>
public float RefreshTime = -1;
/// <summary>
/// 默认保持池子内数量 负数不保持
/// </summary>
public int HoldObject = -1;
Expand All @@ -41,7 +37,7 @@ public class ObjectPool<T>
/// <param name="refreshTime"></param> 刷新时间
public ObjectPool(Func<T> returnT)
{
// _returnT = returnT;
_returnT = returnT;
// initSize = initSize < 0 ? 0 : initSize;
_unUseList = new List<ObjectPoolContainer<T>>();
_userDict = new Dictionary<T, ObjectPoolContainer<T>>();
Expand Down Expand Up @@ -114,14 +110,5 @@ public void Release(T item)
Debug.LogWarning("这池子中不存在包含 "+item +"的容器!");
}
}

// IEnumerator RefreshObjectPool(float refreshTime)
// {
// while (true)
// {
//
// yield return new WaitForSeconds(refreshTime);
// }
// }
}
}
85 changes: 74 additions & 11 deletions Assets/Scenes/PoolManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -11,12 +12,44 @@ namespace Scenes
/// author:Sun
/// des:对象池管理
/// </summary>
public class PoolManager
public class PoolManager:MonoBehaviour
{

private static PoolManager _instance = null;

public static PoolManager Instance
{
get
{
if (GameObject.Find("PoolManager")==null)
{
var pool = new GameObject("PoolManager").AddComponent<PoolManager>();
DontDestroyOnLoad(pool);
}
_instance = GameObject.Find("PoolManager").GetComponent<PoolManager>();
return _instance;
}
}

private void Awake()
{
StartCoroutine(ClearObjectPool());
_objectIdDict = new Dictionary<int, ObjectPool<GameObject>>();
_objectPools = new Dictionary<GameObject, ObjectPool<GameObject>>();
}

/// <summary>
/// 刷新清理时间 负数不清理
/// </summary>
public float RefreshTime = 5;
/// <summary>
/// 生成物Id和对应池子
/// </summary>
private Dictionary<int, ObjectPool<GameObject>> _objectIdDict;
/// <summary>
/// 对象池
/// 对象池子
/// </summary>
private Dictionary<GameObject, ObjectPool<GameObject>> _objectDict;
private Dictionary<GameObject, ObjectPool<GameObject>> _objectPools;

/// <summary>
/// 拿到目标对象
Expand All @@ -25,13 +58,12 @@ public class PoolManager
/// <returns></returns>
public GameObject Get(GameObject prefab)
{
var obj = new GameObject();
if (!_objectDict.ContainsKey(prefab))
GameObject obj = GetPool(prefab).Get();
if (!_objectIdDict.ContainsKey(obj.GetInstanceID()))
{
var pool = new ObjectPool<GameObject>(() => { return InstantiatePrefab(prefab); });
_objectDict.Add(prefab,pool);
_objectIdDict.Add(obj.GetInstanceID(),GetPool(prefab));
}
obj = _objectDict[prefab].Get();
obj.gameObject.SetActive(true);
return obj;
}

Expand All @@ -41,27 +73,45 @@ public GameObject Get(GameObject prefab)
/// <param name="prefab"></param>
/// <param name="position"></param>
/// <param name="rotation"></param>
/// <param name="root"></param>
/// <returns></returns>
public GameObject Get(GameObject prefab, Vector3 position, Quaternion rotation)
public GameObject Get(GameObject prefab, Vector3 position, Quaternion rotation,Transform root)
{
var obj = Get(prefab);
obj.transform.position = position;
obj.transform.rotation = rotation;
obj.transform.parent = root;
return obj;
}

/// <summary>
/// 返回物体对应的池子
/// </summary>
/// <param name="prefab"></param>
/// <returns></returns>
private ObjectPool<GameObject> GetPool(GameObject prefab)
{
if (!_objectPools.ContainsKey(prefab))
{
var pool = new ObjectPool<GameObject>(() => { return InstantiatePrefab(prefab); });
_objectPools.Add(prefab,pool);
}
return _objectPools[prefab];
}

/// <summary>
/// 释放目标对象
/// </summary>
/// <param name="prefab"></param>
/// <exception cref="Exception"></exception>
public void Release(GameObject prefab)
{
if (!_objectDict.ContainsKey(prefab))
if (!_objectIdDict.ContainsKey(prefab.GetInstanceID()))
{
throw new Exception("不存在"+ prefab +"相关对象池");
}
_objectDict[prefab].Release(prefab);
prefab.gameObject.SetActive(false);
_objectIdDict[prefab.GetInstanceID()].Release(prefab);
}


Expand All @@ -76,5 +126,18 @@ private GameObject InstantiatePrefab(GameObject prefab)
return go;
}

/// <summary>
/// 定时清理池子
/// </summary>
/// <returns></returns>
IEnumerator ClearObjectPool()
{
while (true)
{
// print(111);
yield return new WaitForSeconds(RefreshTime);
}
}

}
}
Loading

0 comments on commit 269ccfd

Please sign in to comment.