ball 项目提交
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
public class FXPool<T> : IDisposable where T : Enum
|
||||
{
|
||||
private Dictionary<T, List<Object>> PoolDic;
|
||||
private Dictionary<T, Transform> PollPar;
|
||||
private List<T> temp;
|
||||
private Transform objectPoolPar;
|
||||
public Func<T, Object> NewObjFunc;
|
||||
public Action<T, Object> RecObjFunc;
|
||||
public Action<T, Object> GetObjFunc;
|
||||
|
||||
public FXPool(Transform _objectPoolPar = null)
|
||||
{
|
||||
objectPoolPar = _objectPoolPar;
|
||||
if (objectPoolPar == null)
|
||||
{
|
||||
objectPoolPar = new GameObject("ObjectPool").transform;
|
||||
objectPoolPar.localPosition = Vector3.zero;
|
||||
objectPoolPar.localEulerAngles = Vector3.zero;
|
||||
}
|
||||
|
||||
PoolDic = new Dictionary<T, List<Object>>();
|
||||
PollPar = new Dictionary<T, Transform>();
|
||||
temp = new List<T>();
|
||||
}
|
||||
|
||||
public Obj GetObject<Obj>(T key) where Obj : Object
|
||||
{
|
||||
Obj obj = null;
|
||||
if (!PoolDic.ContainsKey(key))
|
||||
{
|
||||
AddKey(key);
|
||||
}
|
||||
|
||||
if (PoolDic[key].Count > 0)
|
||||
{
|
||||
obj = PoolDic[key][0] as Obj;
|
||||
PoolDic[key].RemoveAt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = LoadObject<Obj>(key);
|
||||
}
|
||||
|
||||
GetObjFunc?.Invoke(key, obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
private void AddKey(T key)
|
||||
{
|
||||
PoolDic.Add(key, new List<Object>());
|
||||
Transform par = new GameObject(key.ToString()).transform;
|
||||
par.SetParent(objectPoolPar);
|
||||
par.localScale = Vector3.one;
|
||||
par.localPosition = Vector3.zero;
|
||||
par.localEulerAngles = Vector3.zero;
|
||||
PollPar.Add(key, par.transform);
|
||||
}
|
||||
|
||||
private Obj LoadObject<Obj>(T key) where Obj : UnityEngine.Object
|
||||
{
|
||||
Obj obj = NewObjFunc?.Invoke(key) as Obj;
|
||||
if (obj != null)
|
||||
{
|
||||
GameObject go = obj as GameObject ?? (obj as Component).gameObject;
|
||||
|
||||
if (PollPar.TryGetValue(key, out Transform par) && go != null)
|
||||
{
|
||||
go.transform.SetParent(par);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void RecObject(T key, Object obj)
|
||||
{
|
||||
if (obj == null) return;
|
||||
if (!PoolDic.ContainsKey(key))
|
||||
{
|
||||
PoolDic.Add(key, new List<Object>());
|
||||
}
|
||||
|
||||
GameObject go = obj as GameObject ?? (obj as Component).gameObject;
|
||||
if (PollPar.TryGetValue(key, out Transform par))
|
||||
{
|
||||
go.transform.parent = (par);
|
||||
}
|
||||
|
||||
RecObjFunc?.Invoke(key, obj);
|
||||
PoolDic[key].Add(obj);
|
||||
}
|
||||
|
||||
public void RemoveKey(T key)
|
||||
{
|
||||
if (PoolDic.ContainsKey(key))
|
||||
{
|
||||
foreach (Object item in PoolDic[key])
|
||||
{
|
||||
if (item != null)
|
||||
{
|
||||
GameObject go = item as GameObject;
|
||||
if (go == null)
|
||||
{
|
||||
Component cot = item as Component;
|
||||
go = cot.gameObject;
|
||||
}
|
||||
|
||||
GameObject.Destroy(go);
|
||||
}
|
||||
}
|
||||
|
||||
PoolDic[key].Clear();
|
||||
PoolDic.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAll()
|
||||
{
|
||||
temp.Clear();
|
||||
foreach (var item in PoolDic)
|
||||
{
|
||||
temp.Add(item.Key);
|
||||
}
|
||||
|
||||
foreach (var item in temp)
|
||||
{
|
||||
RemoveKey(item);
|
||||
}
|
||||
|
||||
temp.Clear();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
RemoveAll();
|
||||
PoolDic = null;
|
||||
PollPar = null;
|
||||
temp = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user