This commit is contained in:
2024-10-16 00:03:41 +08:00
commit 897058435c
5033 changed files with 1009728 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
using System.Collections.Generic;
using UnityEngine;
namespace Framework.GamePool
{
/// <summary>
/// 用于临时对指定GameObject做对象池管理
/// </summary>
public class MiniGameObjectPool<T> where T : MonoBehaviour
{
public Transform FreePrent;
private T _prefabObject;
private Queue<T> _freePool;
private List<T> _activePool;
private Transform _defaultActivePrent;
public Queue<T> FreePool => this._freePool;
public List<T> ActivePool => this._activePool;
public MiniGameObjectPool ()
{
}
public MiniGameObjectPool (T prefabObject)
{
this.InitPool(prefabObject);
}
public MiniGameObjectPool<T> InitPool(T prefab)
{
return InitPool (prefab , prefab.transform.parent);
}
public MiniGameObjectPool<T> InitPool(T prefab, Transform hidePrent, Transform controllerTransform)
{
this.InitPool(prefab, hidePrent);
this.SetActivePrent(controllerTransform);
return this;
}
public MiniGameObjectPool<T> InitPool(T prefab, Transform hidePrent)
{
this._prefabObject = prefab;
this._freePool = new Queue<T>();
this._activePool = new List<T>();
this.SetHideParent(hidePrent);
this.SetActivePrent(prefab.transform.parent);
prefab.gameObject.SetActive(false);
prefab.transform.SetParent(hidePrent);
return this;
}
/// <summary>
/// 销毁该对象池
/// </summary>
public void ReleasePool()
{
for (int i = 0; i < this._freePool.Count; i++)
{
Object.Destroy(this._freePool.Dequeue().gameObject);
}
for (int i = 0; i < this._activePool.Count; i++)
{
Object.Destroy(this._activePool[i].gameObject);
}
this._activePool.Clear();
}
public void SetActivePrent(Transform prent)
{
this._defaultActivePrent = prent;
}
public void SetHideParent(Transform prent)
{
this.FreePrent = prent;
}
public T Create()
{
return Create(_defaultActivePrent);
}
public T Create(Transform parent)
{
T instantiate;
if (this._freePool.Count != 0)
{
instantiate = this.Dequeue();
instantiate.transform.SetParent(parent);
}
else
{
instantiate = Object.Instantiate(this._prefabObject, parent);
}
instantiate.gameObject.SetActive(true);
this._activePool.Add(instantiate);
return instantiate;
}
private T Dequeue()
{
if (this._freePool.Count > 0)
{
var instantiate = this._freePool.Dequeue();
if (instantiate == null)
{
return this.Dequeue();
}
return instantiate;
}
return null;
}
public void Destroy(T instantiate)
{
if (this._activePool.Remove(instantiate))
{
instantiate.gameObject.SetActive(false);
if (this.FreePrent != null)
instantiate.transform.SetParent(this.FreePrent);
this._freePool.Enqueue(instantiate);
}
}
public void DestroyList(List<T> instantiateList)
{
for (int i = instantiateList.Count - 1; i >= 0; i--)
{
var obj = instantiateList[i];
obj.gameObject.SetActive(false);
if (this.FreePrent != null)
obj.transform.SetParent(this.FreePrent);
this._freePool.Enqueue(obj);
this._activePool.Remove(obj);
}
}
public void DestroyAll()
{
for (int i = this._activePool.Count - 1; i >= 0; i--)
{
var obj = this._activePool[i];
obj.gameObject.SetActive(false);
if (this.FreePrent != null)
obj.transform.SetParent(this.FreePrent);
this._freePool.Enqueue(obj);
}
this._activePool.Clear();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d249c40d5e140b78a271e0b806009c0
timeCreated: 1624895127

View File

@@ -0,0 +1,12 @@
using Framework.GamePool.@abstract;
namespace Framework.GamePool
{
public class NonePoolObject : GamePoolAbstract
{
public override void OnPool_Init()
{
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3165a7af3e934936baf85d2ff7d9d3c7
timeCreated: 1589017843

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 0078a67fbe9b29243b2d4d96b6e7a42c
folderAsset: yes
timeCreated: 1523865678
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using Framework.GamePool.@interface;
using Framework.GamePool.manager;
using UnityEngine;
namespace Framework.GamePool.@abstract
{
public abstract class GamePoolAbstract : MonoBehaviour, GamePoolInterface
{
public string Table { get; internal set; }
public string SubTable { get; internal set; }
public string Group { get; internal set; }
protected bool isDisable;
public bool IsDisable() { return this.isDisable; }
public abstract void OnPool_Init();
public virtual void Pool_Disable()
{
this.isDisable = true;
this.gameObject.transform.SetParent(GamePoolManager.Instance.transform);
this.gameObject.SetActive(false);
}
public virtual void Pool_Enable()
{
this.isDisable = false;
this.gameObject.SetActive(true);
}
public void DestroyPoolObject ()
{
GamePoolManager.Instance.Destroy (this);
}
protected void SetSubTable (object subTable) => SubTable = subTable.ToString ();
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 941670e1ab08d6142833bc62f4ec202e
timeCreated: 1523872117
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: a26659248fa0ffa4092196df3a3e10bd
folderAsset: yes
timeCreated: 1523872603
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Framework.GamePool.manager;
using UnityEngine;
namespace Framework.GamePool.config
{
public class GamePoolConfig : ScriptableObject
{
public string ConfigName;
public List<PoolObject> PoolList;
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 4435810e68b65df4fa644b0f371a9331
timeCreated: 1523869566
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: e3d234793a86b6e45b1f3074795a9ae5
folderAsset: yes
timeCreated: 1523865690
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
namespace Framework.GamePool.@interface {
public interface GamePoolInterface
{
/// <summary>
/// 对象禁用
/// </summary>
void Pool_Disable();
/// <summary>
/// 对象激活
/// </summary>
void Pool_Enable();
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 66b0bf738a4eaab4699d8e8378912622
timeCreated: 1523888806
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 88903fec8243d2e49a5a8ab7bd8401de
folderAsset: yes
timeCreated: 1523862521
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,452 @@
using System;
using System.Collections.Generic;
using Framework.Asset;
using Framework.GamePool.@abstract;
using Framework.GamePool.config;
using Framework.Utils.SingletonTemplate;
using UnityEngine;
using YooAsset;
using Object = UnityEngine.Object;
namespace Framework.GamePool.manager
{
[System.Serializable]
public struct PoolObject
{
public string Table;
public GamePoolAbstract Poolobject;
public PoolObject (string table, GamePoolAbstract poolobject)
{
this.Table = table;
this.Poolobject = poolobject;
}
}
public class GamePoolManager : MgrMonoBase<GamePoolManager>
{
public const string ResRootPath = "GamePool/";
public Dictionary<string, PoolAsset> GamePoolDic;
public List<GamePoolAbstract> GamePoolList;
public List<GamePoolAbstract> GamePoolDisableList;
protected override void InitMgr ()
{
this.GamePoolDic = new Dictionary<string, PoolAsset> ();
this.GamePoolList = new List<GamePoolAbstract> ();
this.GamePoolDisableList = new List<GamePoolAbstract> ();
// var objects = Resources.LoadAll<GamePoolAbstract>("GamePool");
// for (int i = 0; i < objects.Length; i++)
// {
// this.LoadPoolObject(objects[i].name, objects[i]);
// }
}
/// <summary>
/// 加载指定路径下的所有对象池预制体
/// 根目录为: Assets/GameRes/GamePool/ 路径填写无需在加上根目录
/// </summary>
public void LoadPoolObjects (string path, string group = null)
{
var pools = AssetManager.Instance.LoadAssetRequests<GameObject> (AssetManager.ResRootPath + ResRootPath + path);
if (pools == null || pools.Count == 0)
{
return;
}
foreach (var assetRequest in pools)
{
var poolAsset = new PoolAsset (assetRequest, group);
if (!this.GamePoolDic.ContainsKey (poolAsset.Table))
{
this.GamePoolDic.Add (poolAsset.Table, poolAsset);
}
else
{
poolAsset.KillAsset ();
}
}
}
/// <summary>
/// 加载指定路径下的对象池文件
/// </summary>
/// <param name="path"></param>
/// <param name="Group"></param>
public void LoadPoolObject (string path, string Group = null)
{
if (path.LastIndexOf (".prefab", StringComparison.Ordinal) == -1)
{
path += ".prefab";
}
var poolObj = AssetManager.Instance.LoadAssetRequest<GameObject> (ResRootPath + path);
var poolAsset = new PoolAsset (poolObj, Group);
if (!this.GamePoolDic.ContainsKey (poolAsset.Table))
{
GamePoolDic.Add (poolAsset.Table , poolAsset);
}
else
{
poolAsset.KillAsset ();
}
}
/// <summary>
/// 查询是否加载了指定标签的对象
/// </summary>
/// <param name="ObjectName"></param>
/// <returns></returns>
public bool SelectObject (string ObjectName)
{
return this.GamePoolDic.ContainsKey (ObjectName);
}
public void CheckNullObject ()
{
for (int i = this.GamePoolList.Count - 1; i >= 0; i--)
{
if (this.GamePoolList[i] == null)
{
// Debug.Log("删除对象池对象");
this.GamePoolList.RemoveAt (i);
}
}
}
/// <summary>
/// 获取场景中所有激活状态下的指定对象
/// </summary>
/// <param name="objectName"></param>
/// <returns></returns>
public List<GameObject> GetActiveObjectAll (string objectName)
{
List<GameObject> gameL = new List<GameObject> ();
this.GamePoolList.ForEach (go =>
{
if (go.Table.Equals (objectName))
{
gameL.Add (go.gameObject);
}
});
return gameL;
}
public List<T> GetActiveObjectAll<T> (string objectName) where T : UnityEngine.Component
{
List<T> gameL = new List<T> ();
this.CheckNullObject ();
this.GamePoolList.ForEach (go =>
{
if (go.Table.Equals (objectName))
{
T component = go.GetComponent<T> ();
if (component != null)
{
gameL.Add (component);
}
}
});
return gameL;
}
/// <summary>
/// 禁用指定table的所有对象
/// </summary>
/// <param name="table"></param>
public void DestroyObjectWitchTable (string table)
{
for (int i = this.GamePoolList.Count - 1; i >= 0; i--)
{
if (GamePoolList[i].Table.Equals (table))
{
this.Destroy (this.GamePoolList[i]);
}
}
}
public void DestroyObjectWitchGroup (string group)
{
List<GamePoolAbstract> abstracts = new List<GamePoolAbstract> ();
foreach (var poolAbstract in this.GamePoolList)
{
if (group.Equals (poolAbstract.Group))
{
abstracts.Add (poolAbstract);
}
}
foreach (var poolAbstract in abstracts)
{
this.Destroy (poolAbstract);
}
}
/// <summary>
/// 返回一个激活的对象到指定位置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <param name="subTable"></param>
/// <param name="pos"></param>
/// <returns></returns>
public T InstantiatePoolObject<T> (string table, string subTable, Vector3 pos, Transform parent = null, string group = null)
where T : UnityEngine.Component
{
if (this.GamePoolDic.ContainsKey (table))
{
if (ReferenceEquals (parent , null)) parent = this.transform;
var isSubTable = !string.IsNullOrEmpty (subTable);
for (int i = 0; i < this.GamePoolDisableList.Count; i++)
{
var poolObject = this.GamePoolDisableList[i];
if (poolObject.Table.Equals (table))
{
if (isSubTable && !subTable.Equals (poolObject.SubTable , StringComparison.Ordinal))
{
continue;
}
if (poolObject.transform.parent != parent)
{
poolObject.transform.SetParent (parent);
}
var transform2 = poolObject.transform;
transform2.localPosition = pos;
transform2.localScale = Vector3.one;
this.GamePoolDisableList.RemoveAt (i);
poolObject.Group = group ?? this.GamePoolDic[table].Group;
poolObject.Pool_Enable ();
this.GamePoolList.Add (poolObject);
return poolObject.GetComponent<T> ();
}
}
GamePoolAbstract go = GameObject.Instantiate (this.GamePoolDic[table].PoolAbstract, parent);
var transform1 = go.transform;
transform1.localPosition = pos;
transform1.localScale = Vector3.one;
go.Table = table;
go.SubTable = subTable;
go.Group = group ?? this.GamePoolDic[table].Group;
go.OnPool_Init ();
go.Pool_Enable ();
this.GamePoolList.Add (go);
return go.GetComponent<T> ();
}
Debug.Log ("<color=red> 错误的从对象池实例化 </color> " + table);
return default;
}
public void ResetGroup (GamePoolAbstract poolObject , string group)
{
poolObject.Group = group;
}
public T InstantiatePoolObject<T> (string table, Vector3 postion, Quaternion quaternion, Transform parent = null , string group = null)
where T : UnityEngine.Component
{
var t = this.InstantiatePoolObject<T> (table, null, postion, parent , group);
t.transform.rotation = quaternion;
return t;
}
public T InstantiatePoolObject<T> (string table, Vector3 postion, Quaternion quaternion, Vector3 scale,
Transform parent = null , string group = null) where T : UnityEngine.Component
{
var t = this.InstantiatePoolObject<T> (table, null, postion, parent , group);
var transform1 = t.transform;
transform1.localRotation = quaternion;
transform1.localScale = scale;
return t;
}
public T InstantiatePoolObject<T> (string table, Transform parent = null) where T : UnityEngine.Component
{
return this.InstantiatePoolObject<T> (table, null, Vector3.zero, parent);
}
public T InstantiatePoolObject<T> (string table, string subTable, Transform parent = null) where T : UnityEngine.Component
{
return this.InstantiatePoolObject<T> (table, subTable, Vector3.zero, parent);
}
public T InstantiatePoolObject<T> (string table, string subTable, string group, Transform parent = null) where T : UnityEngine.Component
{
return this.InstantiatePoolObject<T> (table, subTable, Vector3.zero, parent , group);
}
public T InstantiatePoolObject<T> (string table, string subTable, Vector3 pos, Quaternion quaternion, Vector3 scale,
Transform parent = null) where T : UnityEngine.Component
{
var t = this.InstantiatePoolObject<T> (table, subTable, pos, parent);
var transform1 = t.transform;
transform1.rotation = quaternion;
transform1.localScale = scale;
return t;
}
/// <summary>
/// 不建议使用该方法,因为该方法会调用一次 poolObject.GetComponent()
/// 建议自行对GamePoolAbstract做记录管理使用Destroy(GamePoolAbstract poolObject) 方法
/// </summary>
/// <param name="poolObject"></param>
public void Destroy (Component poolObject)
{
var gamePoolAbstract = poolObject.GetComponent<GamePoolAbstract> ();
if (poolObject != null && gamePoolAbstract != null)
{
Destroy (gamePoolAbstract);
}
}
public void Destroy (GamePoolAbstract poolObject)
{
if (poolObject == null)
return;
if (!poolObject.IsDisable ())
{
poolObject.Pool_Disable ();
this.GamePoolList.Remove (poolObject);
this.GamePoolDisableList.Add (poolObject);
}
}
public void KillAll (bool isClearDic = false)
{
this.CheckNullObject ();
foreach (var poolObject in this.GamePoolList)
{
Object.Destroy (poolObject.gameObject);
}
foreach (var poolObject in this.GamePoolDisableList)
{
Object.Destroy (poolObject.gameObject);
}
this.GamePoolList.Clear ();
this.GamePoolDisableList.Clear ();
if (isClearDic)
{
foreach (var poolAsset in this.GamePoolDic.Values)
poolAsset.KillAsset ();
this.GamePoolDic.Clear ();
}
}
public void KillForGroup (string group)
{
this.CheckNullObject ();
for (int i = this.GamePoolList.Count - 1; i >= 0; i--)
{
if (group.Equals (this.GamePoolList[i].Group))
{
this.GamePoolList[i].Pool_Disable ();
Object.Destroy (this.GamePoolList[i].gameObject);
this.GamePoolList.RemoveAt (i);
}
}
for (int i = this.GamePoolDisableList.Count - 1; i >= 0; i--)
{
if (group.Equals (this.GamePoolDisableList[i].Group))
{
Object.Destroy (this.GamePoolDisableList[i].gameObject);
this.GamePoolDisableList.RemoveAt (i);
}
}
var list = new List<string> (this.GamePoolDic.Keys);
for (int i = list.Count - 1; i >= 0; i--)
{
var key = list[i];
if (group.Equals (this.GamePoolDic[key].Group))
{
this.GamePoolDic[key].KillAsset ();
this.GamePoolDic.Remove (key);
}
}
}
public void LoadPoolObjects (string path, string group, int preloadCount)
{
var pools = AssetManager.Instance.LoadAssetRequests<GameObject> (AssetManager.ResRootPath + ResRootPath + path);
if (pools == null || pools.Count == 0)
{
return;
}
foreach (var assetRequest in pools)
{
var poolAsset = new PoolAsset (assetRequest, group);
if (!this.GamePoolDic.ContainsKey (poolAsset.Table))
{
this.GamePoolDic.Add (poolAsset.Table, poolAsset);
}
else
{
poolAsset.KillAsset ();
}
//预加载
if (preloadCount > 0)
{
for (int i = 0; i < preloadCount; i++)
{
var go = this.InstantiatePoolObject<GamePoolAbstract> (poolAsset.Table, Vector3.zero, Quaternion.identity, Vector3.one);
this.Destroy (go);
}
}
}
}
public void DestroyList<T> (List<T> cardEntities) where T : Component
{
foreach (var cardEntity in cardEntities)
{
this.Destroy (cardEntity);
}
}
// public void DestroyList (List<GamePoolAbstract> cardEntities)
// {
// foreach (var cardEntity in cardEntities)
// {
// this.Destroy(cardEntity);
// }
// }
}
public class PoolAsset
{
private AssetHandle _assetRequest;
private string _group;
private GamePoolAbstract _poolAbstract;
internal PoolAsset (AssetHandle assetRequest, string Group)
{
this._assetRequest = assetRequest;
this._group = Group;
this._poolAbstract = ((GameObject) assetRequest.AssetObject).GetComponent<GamePoolAbstract> ();
}
public string Group => this._group;
public GamePoolAbstract PoolAbstract => this._poolAbstract;
public string Table => this._poolAbstract.name;
internal void KillAsset ()
{
this._assetRequest.Release ();
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: fb40ebc0e3452274990470a49ed1c9e9
timeCreated: 1523865722
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: