Created
December 12, 2020 14:29
-
-
Save leventeren/d33cfb93a8e3d8ad1c97d8e4265a6f86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
//For general object operations | |
public class PoolObjectInfo : MonoBehaviour | |
{ | |
//Resource Type | |
public string Type { get { return _Type; } private set { _Type = value; } } | |
[SerializeField] | |
private string _Type; | |
//Resource ID Each created resource will have a unique id | |
public string AssetID { get { return _AssetID; } private set { _AssetID = value; } } | |
[SerializeField] | |
private string _AssetID; | |
//Recycling time | |
public float RecoverTime { get { return _RecoverTime; } private set { _RecoverTime = value; } } | |
[SerializeField] | |
private float _RecoverTime; | |
private IEnumerator _iEnumerator; | |
//public string Type { get; private set; } | |
//public string AssetID { get; private set; } | |
//public float RecoverTime { get; private set; } | |
void Start() | |
{ | |
} | |
//Resources are regularly recycled | |
public void Recover() | |
{ | |
if (RecoverTime == -1 && RecoverTime == 0) | |
return; | |
if (_iEnumerator!=null) | |
{ | |
StopCoroutine(_iEnumerator); | |
} | |
_iEnumerator = RecoverGameObj(); | |
StartCoroutine(_iEnumerator); | |
} | |
IEnumerator RecoverGameObj() | |
{ | |
yield return new WaitForSeconds(RecoverTime); | |
PoolManager.Instance.RecoverGameObj(gameObject); | |
_iEnumerator = null; | |
} | |
private void OnDisable() | |
{ | |
if (_iEnumerator != null) | |
{ | |
StopCoroutine(_iEnumerator); | |
} | |
} | |
public void InitData(string type,float time=-1) | |
{ | |
if (Type==null) | |
{ | |
Type = type; | |
AssetID = Guid.NewGuid().ToString("N"); | |
RecoverTime = time; | |
} | |
} | |
public void ResetInfo() | |
{ | |
if (Type != null) | |
{ | |
AssetID = Guid.NewGuid().ToString("N"); | |
} | |
} | |
} | |
Create attributes for unity drag | |
[Serializable] | |
public class PoolObject { | |
public string Name;//first name | |
public float RecoverTime=-1;//Recycling time | |
public GameObject GameObj;//Object | |
} | |
Create Management Class | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
public class PoolManager : MonoBehaviour { | |
private Dictionary<string, Dictionary<string, GameObject>> _dtGameObjUsingPool = new Dictionary<string, Dictionary<string, GameObject>>(); | |
private Dictionary<string, Dictionary<string, GameObject>> _dtGameObjFreePool = new Dictionary<string, Dictionary<string, GameObject>>(); | |
private Dictionary<string, PoolObject> _dtGameObj = new Dictionary<string, PoolObject>(); | |
public List<PoolObject> _LstGameObj; | |
public static PoolManager Instance { get; private set; } | |
// Use this for initialization | |
void Start() {//initialization | |
Instance = this; | |
for (int i=0;i<_LstGameObj.Count;i++) { | |
if (_dtGameObj.ContainsKey(_LstGameObj[i].Name)) | |
{ | |
Debug.LogWarning(_LstGameObj[i].Name+ " Repeat addition !"); | |
} | |
else | |
{ | |
_dtGameObj.Add(_LstGameObj[i].Name, _LstGameObj[i]); | |
} | |
} | |
foreach (string item in _dtGameObj.Keys) | |
{ | |
Debug.Log(item); | |
} | |
} | |
// Update is called once per frame | |
void Update() { | |
} | |
private void Pool() | |
{ | |
} | |
//Get an object | |
public GameObject GetGameObj(string name, Transform parent = null) { | |
KeyValuePair<string, GameObject> keyValuePair=new KeyValuePair<string, GameObject>(); | |
if (_dtGameObjFreePool.ContainsKey(name)&& _dtGameObjFreePool[name].Count!=0) | |
{ | |
Dictionary<string, GameObject>.Enumerator freePoolE = _dtGameObjFreePool[name].GetEnumerator(); | |
freePoolE.MoveNext(); | |
keyValuePair = freePoolE.Current; | |
_dtGameObjFreePool[name].Remove(keyValuePair.Key); | |
} | |
else | |
{ | |
if (_dtGameObj.ContainsKey(name)) { | |
GameObject gameObj = Instantiate(_dtGameObj[name].GameObj) as GameObject; | |
gameObj.AddComponent<PoolObjectInfo>(); | |
gameObj.GetComponent<PoolObjectInfo>().InitData(name, _dtGameObj[name].RecoverTime); | |
keyValuePair=new KeyValuePair< string,GameObject> (gameObj.GetComponent<PoolObjectInfo>().AssetID,gameObj); | |
} | |
else | |
{ | |
Debug.LogWarning( name + " not found "); | |
return null; | |
} | |
} | |
if (keyValuePair.Key==null) | |
{ | |
Debug.LogWarning(name + " not found "); | |
return null; | |
} | |
if (!_dtGameObjUsingPool.ContainsKey(name)) | |
{ | |
_dtGameObjUsingPool.Add(name,new Dictionary<string, GameObject>()); | |
} | |
if (parent != null) | |
{ | |
keyValuePair.Value.transform.SetParent(parent); | |
} | |
_dtGameObjUsingPool[name].Add(keyValuePair.Key,keyValuePair.Value); | |
return keyValuePair.Value; | |
} | |
public GameObject GetGameObj(string name,string assetId, Transform parent = null) | |
{ | |
if (_dtGameObjFreePool.ContainsKey(name) && _dtGameObjFreePool[name].ContainsKey(assetId)) | |
{ | |
if (!_dtGameObjUsingPool.ContainsKey(name)) | |
{ | |
_dtGameObjUsingPool.Add(name, new Dictionary<string, GameObject>()); | |
} | |
GameObject res = _dtGameObjFreePool[name][assetId]; | |
_dtGameObjUsingPool[name].Add(assetId, res); | |
_dtGameObjFreePool[name].Remove(assetId); | |
if (parent != null) | |
{ | |
res.transform.SetParent(parent); | |
} | |
return res; | |
} | |
else | |
{ | |
Debug.LogWarning(name + " not found "); | |
} | |
return null; | |
} | |
public T GetGameObj<T>(string name,Transform parent=null) | |
{ | |
KeyValuePair<string, GameObject> keyValuePair = new KeyValuePair<string, GameObject>(); | |
if (_dtGameObjFreePool.ContainsKey(name) && _dtGameObjFreePool[name].Count != 0) | |
{ | |
Dictionary<string, GameObject>.Enumerator freePoolE = _dtGameObjFreePool[name].GetEnumerator(); | |
freePoolE.MoveNext(); | |
keyValuePair = freePoolE.Current; | |
_dtGameObjFreePool[name].Remove(keyValuePair.Key); | |
} | |
else | |
{ | |
if (_dtGameObj.ContainsKey(name)) | |
{ | |
GameObject gameObj = Instantiate(_dtGameObj[name].GameObj) as GameObject; | |
gameObj.AddComponent<PoolObjectInfo>().InitData(name, _dtGameObj[name].RecoverTime); | |
keyValuePair = new KeyValuePair<string, GameObject>(gameObj.GetComponent<PoolObjectInfo>().AssetID, gameObj); | |
} | |
else | |
{ | |
Debug.LogWarning(name + " not found "); | |
return default(T); | |
} | |
} | |
if (keyValuePair.Key == null || keyValuePair.Value.GetComponent<T>()==null) | |
{ | |
Debug.LogWarning(name + " not found "); | |
return default(T); | |
} | |
if (!_dtGameObjUsingPool.ContainsKey(name)) | |
{ | |
_dtGameObjUsingPool.Add(name, new Dictionary<string, GameObject>()); | |
} | |
_dtGameObjUsingPool[name].Add(keyValuePair.Key, keyValuePair.Value); | |
if (parent != null) | |
{ | |
keyValuePair.Value.transform.SetParent(parent); | |
} | |
return keyValuePair.Value.GetComponent<T>(); | |
} | |
public T GetGameObj<T>(string name, string assetId, Transform parent = null) | |
{ | |
if (_dtGameObjFreePool.ContainsKey(name) && _dtGameObjFreePool[name].ContainsKey(assetId)) | |
{ | |
if (!_dtGameObjUsingPool.ContainsKey(name)) | |
{ | |
_dtGameObjUsingPool.Add(name, new Dictionary<string, GameObject>()); | |
} | |
GameObject res = _dtGameObjFreePool[name][assetId]; | |
_dtGameObjUsingPool[name].Add(assetId, res); | |
_dtGameObjFreePool[name].Remove(assetId); | |
if (res.GetComponent<T>()==null) | |
{ | |
Debug.LogWarning(name + " not found "); | |
return default(T); | |
} | |
if (parent != null) | |
{ | |
res.transform.SetParent(parent); | |
} | |
return res.GetComponent<T>(); | |
} | |
else | |
{ | |
Debug.LogWarning(name + " not found "); | |
} | |
return default(T); | |
} | |
public void RecoverGameObj(GameObject gameObj) | |
{ | |
PoolObjectInfo info = gameObj.GetComponent<PoolObjectInfo>(); | |
gameObj.SetActive(false); | |
if (info == null) | |
{ | |
return; | |
} | |
if (!_dtGameObjFreePool.ContainsKey(info.Type)) | |
{ | |
_dtGameObjFreePool.Add(info.Type, new Dictionary<string, GameObject>()); | |
} | |
if (_dtGameObjUsingPool[info.Type].ContainsKey(info.AssetID)) | |
{ | |
_dtGameObjUsingPool[info.Type].Remove(info.AssetID); | |
_dtGameObjFreePool[info.Type].Add(info.AssetID, gameObj); | |
} | |
} | |
public void RecoverGameObj(string name) | |
{ | |
if (!_dtGameObjUsingPool.ContainsKey(name)) | |
{ | |
return; | |
} | |
foreach(GameObject o in _dtGameObjUsingPool[name].Values) | |
{ | |
RecoverGameObj(o); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment