初始化

This commit is contained in:
come
2025-07-26 16:56:42 +08:00
parent 8291dbb91c
commit fa81439a8c
2574 changed files with 328492 additions and 2170 deletions

View File

@@ -0,0 +1,114 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YIUIFramework
{
/// <summary>
/// 可以自动回收的池
/// 适合用于临时的函数返对象
/// </summary>
public static class AutoListPool<T>
{
private static ListPool g_pool = new ListPool();
static AutoListPool()
{
AutoListPool.g_recyclers.Add(g_pool);
}
public static List<T> Get()
{
return g_pool.Get();
}
private class ListPool : Recycler
{
private List<List<T>> m_pool = new List<List<T>>(5);
private int m_index;
public List<T> Get()
{
List<T> item;
if (m_index == m_pool.Count)
{
item = new List<T>();
m_pool.Add(item);
}
else
{
item = m_pool[m_index];
}
m_index++;
Dirty = true;
AutoListPool.g_dirty = true;
//虽然回收时已经清了,但这样双保险,不香么?
item.Clear();
return item;
}
public override void Recycle()
{
if (m_index == 0)
{
return;
}
for (int i = 0; i < m_pool.Count; i++)
{
m_pool[i].Clear();
}
m_index = 0;
Dirty = false;
}
}
}
internal abstract class Recycler
{
public bool Dirty;
public abstract void Recycle();
}
[DefaultExecutionOrder(int.MaxValue)]
internal class AutoListPool : MonoBehaviour
{
internal static readonly List<Recycler> g_recyclers = new List<Recycler>();
internal static bool g_dirty;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void OnLoad()
{
SceneManager.sceneLoaded -= OnSceneLoadedHandler;
SceneManager.sceneLoaded += OnSceneLoadedHandler;
}
private static void OnSceneLoadedHandler(UnityEngine.SceneManagement.Scene arg0, LoadSceneMode arg1)
{
var autoListPoolGo = new GameObject("AutoListPool");
autoListPoolGo.hideFlags = HideFlags.HideInHierarchy;
autoListPoolGo.AddComponent<AutoListPool>();
}
private void LateUpdate()
{
if (!g_dirty)
{
return;
}
g_dirty = false;
for (int i = 0; i < g_recyclers.Count; i++)
{
var item = g_recyclers[i];
if (item.Dirty)
{
item.Recycle();
}
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 90978b12434d48669352366cfb8b2744
timeCreated: 1681720507

View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// 可以自动回收的对象池
/// </summary>
/// <typeparam name="T"></typeparam>
public class AutoRecycleObjPool<T>
{
private Action<T> m_clearCallback;
private Func<T> m_newCallback;
private ITimeProvider m_timer;
private int m_lastUpdateTime;
private List<PoolVo> m_uses;
private List<PoolVo> m_frees;
public AutoRecycleObjPool(ITimeProvider timer,
Func<T> newCallback,
Action<T> clearCallback)
{
m_timer = timer;
m_newCallback = newCallback;
m_clearCallback = clearCallback;
m_uses = new List<PoolVo>();
m_frees = new List<PoolVo>();
}
public T Get()
{
Update();
PoolVo result;
if (m_frees.Count > 0)
{
result = m_frees.Pop();
}
else
{
result = new PoolVo();
result.Value = m_newCallback();
}
result.Timeout = m_timer.Time + 1;
m_uses.Add(result);
return result.Value;
}
public void Update()
{
var count = m_uses.Count;
if (count < 1)
{
return;
}
var curTime = m_timer.Time;
if (m_lastUpdateTime == curTime)
{
return;
}
m_lastUpdateTime = curTime;
//找出过时的对象
for (var i = 0; i < count; i++)
{
var poolVo = m_uses[i];
if (curTime >= poolVo.Timeout)
{
m_uses.FastRemoveAt(i);
m_clearCallback(poolVo.Value);
m_frees.Add(poolVo);
count--;
i--;
}
}
}
private class PoolVo
{
public int Timeout;
public T Value;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d63555742804916844a9d8873b1e924
timeCreated: 1681720507

View File

@@ -0,0 +1,31 @@
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// 字典的池
/// </summary>
public class DictionaryPool<TKey, TValue>
{
private static Stack<Dictionary<TKey, TValue>> g_pool = new Stack<Dictionary<TKey, TValue>>();
public static Dictionary<TKey, TValue> Get()
{
lock (g_pool)
{
return g_pool.Count == 0
? new Dictionary<TKey, TValue>()
: g_pool.Pop();
}
}
public static void Put(Dictionary<TKey, TValue> map)
{
map.Clear();
lock (g_pool)
{
g_pool.Push(map);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e3cb60974c6e4598822a56e8640ec898
timeCreated: 1681720507

View File

@@ -0,0 +1,63 @@
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// HashSet的池
/// </summary>
public class HashSetPool<T>
{
private static Stack<HashSet<T>> g_pool = new Stack<HashSet<T>>(5);
public static HashSet<T> Get()
{
lock (g_pool)
{
return g_pool.Count == 0
? new HashSet<T>()
: g_pool.Pop();
}
}
/// <summary>
/// 得到一个hashSet, 并帮你填充了data
/// </summary>
/// <param name="initData"></param>
/// <returns></returns>
public static HashSet<T> Get(T[] initData)
{
var map = Get();
for (int i = 0; i < initData.Length; i++)
{
map.Add(initData[i]);
}
return map;
}
/// <summary>
/// 得到一个hashSet, 并帮你填充了data
/// </summary>
/// <param name="initData"></param>
/// <returns></returns>
public static HashSet<T> Get(IList<T> initData)
{
var map = Get();
for (int i = 0; i < initData.Count; i++)
{
map.Add(initData[i]);
}
return map;
}
public static void Put(HashSet<T> list)
{
list.Clear();
lock (g_pool)
{
g_pool.Push(list);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7ae235583b5340aab9dccbc51499513c
timeCreated: 1681720507

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
namespace YIUIFramework
{
/*public static class ListPool<T>
{
private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>(null, l => l.Clear());
public static List<T> Get()
{
return s_ListPool.Get();
}
public static void Release(List<T> toRelease)
{
s_ListPool.Release(toRelease);
}
}*/
public static class LinkedListPool<T>
{
private static readonly ObjectPool<LinkedList<T>> s_ListPool =
new ObjectPool<LinkedList<T>>(null, l => l.Clear());
public static LinkedList<T> Get()
{
return s_ListPool.Get();
}
public static void Release(LinkedList<T> toRelease)
{
s_ListPool.Release(toRelease);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 212be9f6f4814d0e87332d23561af12c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,39 @@
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// 各种列表的池
/// </summary>
/// <typeparam name="T"></typeparam>
public static class ListPool<T>
{
private static Stack<List<T>> g_pool = new Stack<List<T>>(5);
public static List<T> Get()
{
lock (g_pool)
{
return g_pool.Count == 0
? new List<T>()
: g_pool.Pop();
}
}
public static void Put(List<T> list)
{
list.Clear();
lock (g_pool)
{
g_pool.Push(list);
}
}
public static T[] PutAndToArray(List<T> list)
{
var t = list.ToArray();
Put(list);
return t;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 801d2ad7c6c444e9961eb176e0ea8691
timeCreated: 1681720506

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace YIUIFramework
{
/// <summary>
/// 异步对象缓存池
/// </summary>
public class ObjAsyncCache<T>
{
private Stack<T> m_pool;
protected Func<UniTask<T>> m_createCallback;
public ObjAsyncCache(Func<UniTask<T>> createCallback, int capacity = 0)
{
m_pool = capacity > 0
? new Stack<T>(capacity)
: new Stack<T>();
m_createCallback = createCallback;
}
public async UniTask<T> Get()
{
return m_pool.Count > 0 ? m_pool.Pop() : await m_createCallback();
}
public void Put(T value)
{
m_pool.Push(value);
}
public void Clear(bool disposeItem = false)
{
if (disposeItem)
{
foreach (var item in m_pool)
{
if (item is IDisposer disposer)
{
disposer.Dispose();
}
else if (item is IDisposable disposer2)
{
disposer2.Dispose();
}
}
}
m_pool.Clear();
}
public void Clear(Action<T> disposeAction)
{
while (m_pool.Count >= 1)
{
disposeAction?.Invoke(m_pool.Pop());
}
m_pool.Clear();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 100e412be3594bb2b6a08fd501297d1f
timeCreated: 1684290887

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// 对象缓存池
/// </summary>
public class ObjCache<T>
{
private Stack<T> m_pool;
protected Func<T> m_createCallback;
public ObjCache(Func<T> createCallback, int capacity = 0)
{
m_pool = capacity > 0
? new Stack<T>(capacity)
: new Stack<T>();
m_createCallback = createCallback;
}
public T Get()
{
return m_pool.Count > 0 ? m_pool.Pop() : m_createCallback();
}
public void Put(T value)
{
m_pool.Push(value);
}
public void Clear(bool disposeItem = false)
{
if (disposeItem)
{
foreach (var item in m_pool)
{
if (item is IDisposer disposer)
{
disposer.Dispose();
}
else if (item is IDisposable disposer2)
{
disposer2.Dispose();
}
}
}
m_pool.Clear();
}
public void Clear(Action<T> disposeAction)
{
while (m_pool.Count >= 1)
{
disposeAction?.Invoke(m_pool.Pop());
}
m_pool.Clear();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ec17f07373554d608d685f879c6a9957
timeCreated: 1681720506

View File

@@ -0,0 +1,86 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace YIUIFramework
{
public class ObjectPool<T> where T : new()
{
#if UNITY_EDITOR
public static bool isRunning = false;
private readonly HashSet<T> Trace = new HashSet<T>();
#endif
private readonly Stack<T> m_Stack = new Stack<T>();
private readonly UnityAction<T> m_ActionOnGet;
private readonly UnityAction<T> m_ActionOnRelease;
public int countAll { get; private set; }
public int countActive
{
get { return countAll - countInactive; }
}
public int countInactive
{
get { return m_Stack.Count; }
}
public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
{
m_ActionOnGet = actionOnGet;
m_ActionOnRelease = actionOnRelease;
}
public T Get()
{
T element;
if (m_Stack.Count == 0)
{
element = new T();
countAll++;
#if UNITY_EDITOR
Trace.Add(element);
#endif
}
else
{
element = m_Stack.Pop();
}
if (m_ActionOnGet != null)
m_ActionOnGet(element);
return element;
}
public void Release(T element)
{
#if UNITY_EDITOR
if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
if (element == null)
{
Debug.LogError("Internal error. Release element is null.");
return;
}
if (ObjectPool<bool>.isRunning == false)
{
if (m_ActionOnRelease != null)
m_ActionOnRelease(element);
return;
}
if (Trace.Contains(element) == false)
{
Debug.LogError("Internal error. Release element is not from pool ");
}
#endif
if (m_ActionOnRelease != null)
m_ActionOnRelease(element);
m_Stack.Push(element);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6703f8ed118d4f56b759711a066e235c
timeCreated: 1679041134

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8104da227f5d4b31913d534031f9e44f
timeCreated: 1688349310

View File

@@ -0,0 +1,13 @@
namespace YIUIFramework
{
/// <summary>
/// 引用接口
/// </summary>
public interface IRefPool
{
/// <summary>
/// 被回收时 重置所有数据
/// </summary>
void Recycle();
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b4faaba43e964a7ca00f6a686626e180
timeCreated: 1688349310

View File

@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
namespace YIUIFramework
{
public static partial class RefPool
{
private sealed class RefCollection
{
private readonly Queue<IRefPool> m_Refs;
private readonly Type m_RefType;
public RefCollection(Type refType)
{
m_Refs = new Queue<IRefPool>();
m_RefType = refType;
}
public Type RefType
{
get { return m_RefType; }
}
public T Get<T>() where T : class, IRefPool, new()
{
if (typeof(T) != m_RefType)
{
throw new Exception("类型无效");
}
lock (m_Refs)
{
if (m_Refs.Count > 0)
{
return (T)m_Refs.Dequeue();
}
}
return new T();
}
public IRefPool Get()
{
lock (m_Refs)
{
if (m_Refs.Count > 0)
{
return m_Refs.Dequeue();
}
}
return (IRefPool)Activator.CreateInstance(m_RefType);
}
public bool Put(IRefPool iRef)
{
iRef.Recycle();
lock (m_Refs)
{
if (!m_Refs.Contains(iRef))
{
m_Refs.Enqueue(iRef);
return true;
}
}
return false;
}
public void Add<T>(int count) where T : class, IRefPool, new()
{
if (typeof(T) != m_RefType)
{
throw new Exception("类型无效。");
}
lock (m_Refs)
{
while (count-- > 0)
{
m_Refs.Enqueue(new T());
}
}
}
public void Add(int count)
{
lock (m_Refs)
{
while (count-- > 0)
{
m_Refs.Enqueue((IRefPool)Activator.CreateInstance(m_RefType));
}
}
}
public void Remove(int count)
{
lock (m_Refs)
{
if (count > m_Refs.Count)
{
count = m_Refs.Count;
}
while (count-- > 0)
{
m_Refs.Dequeue();
}
}
}
public void RemoveAll()
{
lock (m_Refs)
{
m_Refs.Clear();
}
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d41524c232e54efeb690d109acfea6a3
timeCreated: 1688349310

View File

@@ -0,0 +1,190 @@
using System.Collections.Generic;
using System;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// 引用池
/// </summary>
public static partial class RefPool
{
private static readonly Dictionary<Type, RefCollection>
s_RefCollections = new Dictionary<Type, RefCollection>();
public static int Count
{
get
{
lock (s_RefCollections)
{
return s_RefCollections.Count;
}
}
}
/// <summary>
/// 清除所有引用池
/// </summary>
public static void ClearAll()
{
lock (s_RefCollections)
{
foreach (KeyValuePair<Type, RefCollection> refCollection in s_RefCollections)
{
refCollection.Value.RemoveAll();
}
s_RefCollections.Clear();
}
}
/// <summary>
/// 从引用池获取引用
/// </summary>
/// <typeparam name="T">引用类型</typeparam>
/// <returns>引用</returns>
public static T Get<T>() where T : class, IRefPool, new()
{
return GetRefCollection(typeof(T))?.Get<T>();
}
/// <summary>
/// 从引用池获取引用
/// </summary>
/// <param name="refreceType">引用类型</param>
/// <returns>引用</returns>
public static IRefPool Get(Type refreceType)
{
return GetRefCollection(refreceType)?.Get();
}
/// <summary>
/// 回收引用
/// </summary>
public static bool Put(IRefPool iRef)
{
if (iRef == null)
{
Debug.LogError("refType == null 无效 请检查");
return false;
}
var collection = GetRefCollection(iRef.GetType());
return collection != null && collection.Put(iRef);
}
/// <summary>
/// 向引用池中追加指定数量的引用
/// </summary>
/// <typeparam name="T">引用类型</typeparam>
/// <param name="count">追加数量</param>
public static void Add<T>(int count) where T : class, IRefPool, new()
{
GetRefCollection(typeof(T))?.Add<T>(count);
}
/// <summary>
/// 向引用池中追加指定数量的引用
/// </summary>
/// <param name="refType">引用类型</param>
/// <param name="count">追加数量</param>
public static void Add(Type refType, int count)
{
GetRefCollection(refType)?.Add(count);
}
/// <summary>
/// 从引用池中移除指定数量的引用
/// </summary>
/// <typeparam name="T">引用类型</typeparam>
/// <param name="count">移除数量</param>
public static void Remove<T>(int count) where T : class, IRefPool, new()
{
GetRefCollection(typeof(T),true)?.Remove(count);
}
/// <summary>
/// 从引用池中移除指定数量的引用
/// </summary>
/// <param name="refType">引用类型</param>
/// <param name="count">移除数量</param>
public static void Remove(Type refType, int count)
{
GetRefCollection(refType,true)?.Remove(count);
}
/// <summary>
/// 从引用池中移除所有的引用
/// </summary>
/// <typeparam name="T">引用类型</typeparam>
public static void RemoveAll<T>() where T : class, IRefPool, new()
{
GetRefCollection(typeof(T),true)?.RemoveAll();
}
/// <summary>
/// 从引用池中移除所有的引用
/// </summary>
/// <param name="refType">引用类型</param>
public static void RemoveAll(Type refType)
{
GetRefCollection(refType,true)?.RemoveAll();
}
private static bool InternalCheckRefType(Type refType)
{
if (refType == null)
{
Debug.LogError("refType == null 无效 请检查");
return false;
}
if (!refType.IsClass || refType.IsAbstract)
{
Debug.LogError("引用类型不是非抽象类类型");
return false;
}
if (!typeof(IRefPool).IsAssignableFrom(refType))
{
Debug.LogError($"引用类型'{refType.FullName}'无效。");
return false;
}
return true;
}
private static RefCollection GetRefCollection(Type refType, bool allowNull = false)
{
if (refType == null)
{
Debug.LogError("refType == null 无效 请检查");
return null;
}
RefCollection refCollection = null;
lock (s_RefCollections)
{
if (!s_RefCollections.TryGetValue(refType, out refCollection))
{
if (allowNull)
{
return null;
}
if (!InternalCheckRefType(refType))
{
return null;
}
refCollection = new RefCollection(refType);
s_RefCollections.Add(refType, refCollection);
}
}
return refCollection;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: baa8034445504494a567cf5a6dcf12b5
timeCreated: 1688349310

View File

@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Text;
namespace YIUIFramework
{
/// <summary>
/// StringBuilder池
/// </summary>
public static class SbPool
{
private static Stack<StringBuilder> g_pool = new Stack<StringBuilder>(5);
public static StringBuilder Get()
{
lock (g_pool)
{
return g_pool.Count == 0
? new StringBuilder()
: g_pool.Pop();
}
}
public static void Put(StringBuilder sb)
{
sb.Clear();
lock (g_pool)
{
g_pool.Push(sb);
}
}
public static string PutAndToStr(StringBuilder sb)
{
var str = sb.ToString();
Put(sb);
return str;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2c28dc904d6441da8cbd9d1db473a283
timeCreated: 1681720506

View File

@@ -0,0 +1,15 @@
namespace YIUIFramework
{
public class SimplePool<T> : ObjCache<T> where T : new()
{
public SimplePool(int capacity = 0) : base(null, capacity)
{
m_createCallback = Create;
}
private T Create()
{
return new T();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ef154f446c7247d18c3b07df39be75cd
timeCreated: 1681720506

View File

@@ -0,0 +1,38 @@
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// 堆栈池
/// </summary>
public class StackPool<T>
{
private static Stack<Stack<T>> g_pool = new Stack<Stack<T>>(5);
public static Stack<T> Get()
{
lock (g_pool)
{
return g_pool.Count == 0
? new Stack<T>()
: g_pool.Pop();
}
}
public static void Put(Stack<T> list)
{
list.Clear();
lock (g_pool)
{
g_pool.Push(list);
}
}
public static T[] PutAndToArray(Stack<T> list)
{
var t = list.ToArray();
Put(list);
return t;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 404ba99bdbef49aa9cc9e1a75b4bc12e
timeCreated: 1681720506