【m】1111
This commit is contained in:
15
Assets/06.UIFarme/RunTime/UIFramework/Other/Layer.cs
Normal file
15
Assets/06.UIFarme/RunTime/UIFramework/Other/Layer.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public static class Layer
|
||||
{
|
||||
public const int Default = 0;
|
||||
public const int TransparentFX = 1;
|
||||
public const int IgnoreRaycast = 2;
|
||||
public const int Water = 4;
|
||||
public const int UI = 5;
|
||||
public const int UIRenderToTarget = 6;
|
||||
}
|
||||
}
|
||||
11
Assets/06.UIFarme/RunTime/UIFramework/Other/Layer.cs.meta
Normal file
11
Assets/06.UIFarme/RunTime/UIFramework/Other/Layer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f57fecb20b667bb45a6240fd6b16fc80
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public static class ObjectExtension
|
||||
{
|
||||
private static readonly List<Transform> s_CachedTransforms = new List<Transform>();
|
||||
|
||||
public static T GetOrAddComponent<T>(this Component obj) where T : Component
|
||||
{
|
||||
return obj.gameObject.GetOrAddComponent<T>();
|
||||
}
|
||||
|
||||
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
|
||||
{
|
||||
T t = gameObject.GetComponent<T>();
|
||||
if (t == null)
|
||||
t = gameObject.AddComponent<T>();
|
||||
return t;
|
||||
}
|
||||
|
||||
public static Component GetOrAddComponent(this Component obj, Type type)
|
||||
{
|
||||
return obj.gameObject.GetOrAddComponent(type);
|
||||
}
|
||||
|
||||
public static Component GetOrAddComponent(this GameObject gameObject, Type type)
|
||||
{
|
||||
if (gameObject == null) return null;
|
||||
|
||||
Component component = gameObject.GetComponent(type);
|
||||
if (component == null)
|
||||
component = gameObject.AddComponent(type);
|
||||
return component;
|
||||
}
|
||||
|
||||
public static void SetParentEx(this Transform transform, Transform parent)
|
||||
{
|
||||
transform.SetParent(parent);
|
||||
transform.localPosition = Vector3.zero;
|
||||
transform.localRotation = Quaternion.identity;
|
||||
transform.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
public static void SetLayerRecursively(this GameObject gameObject, int layer)
|
||||
{
|
||||
gameObject.GetComponentsInChildren(true, s_CachedTransforms);
|
||||
for (int i = 0; i < s_CachedTransforms.Count; i++)
|
||||
{
|
||||
s_CachedTransforms[i].gameObject.layer = layer;
|
||||
}
|
||||
s_CachedTransforms.Clear();
|
||||
}
|
||||
|
||||
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue value)
|
||||
{
|
||||
if (dict.ContainsKey(key))
|
||||
dict[key] = value;
|
||||
else
|
||||
dict.Add(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1255735e746b217478dc63262723567a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
194
Assets/06.UIFarme/RunTime/UIFramework/Other/ObjectPool.cs
Normal file
194
Assets/06.UIFarme/RunTime/UIFramework/Other/ObjectPool.cs
Normal file
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public static class Pool
|
||||
{
|
||||
public readonly static List<PoolBase> AllPool = new List<PoolBase>();
|
||||
|
||||
public static void ReleaseAll()
|
||||
{
|
||||
foreach (var pool in AllPool)
|
||||
{
|
||||
pool.Dispose();
|
||||
}
|
||||
AllPool.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IObject
|
||||
{
|
||||
void OnRelease();
|
||||
}
|
||||
|
||||
public interface PoolBase
|
||||
{
|
||||
void Dispose();
|
||||
}
|
||||
|
||||
public class ObjectPool<T> : PoolBase where T : new()
|
||||
{
|
||||
private static ObjectPool<T> Instance;
|
||||
|
||||
private Stack<T> _pool;
|
||||
|
||||
private ObjectPool() { }
|
||||
|
||||
|
||||
private static void Init()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = new ObjectPool<T>();
|
||||
Instance._pool = new Stack<T>();
|
||||
Pool.AllPool.Add(Instance);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Get()
|
||||
{
|
||||
Init();
|
||||
|
||||
if (Instance._pool.Count > 0)
|
||||
{
|
||||
return Instance._pool.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Release(T obj)
|
||||
{
|
||||
if (obj == null || Instance == null) return;
|
||||
|
||||
if (obj is IObject interfac)
|
||||
{
|
||||
interfac.OnRelease();
|
||||
}
|
||||
Instance._pool.Push(obj);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
if (Instance._pool != null)
|
||||
{
|
||||
Instance._pool.Clear();
|
||||
Instance._pool = null;
|
||||
}
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ListPool<T> : PoolBase
|
||||
{
|
||||
private static ListPool<T> Instance;
|
||||
|
||||
private Stack<List<T>> _pool;
|
||||
|
||||
private ListPool() { }
|
||||
|
||||
private static void Init()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = new ListPool<T>();
|
||||
Instance._pool = new Stack<List<T>>();
|
||||
Pool.AllPool.Add(Instance);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<T> Get()
|
||||
{
|
||||
Init();
|
||||
|
||||
if (Instance._pool.Count > 0)
|
||||
{
|
||||
return Instance._pool.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<T>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Release(List<T> list)
|
||||
{
|
||||
if (list == null || Instance == null) return;
|
||||
list.Clear();
|
||||
Instance._pool.Push(list);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
if (Instance._pool != null)
|
||||
{
|
||||
Instance._pool.Clear();
|
||||
Instance._pool = null;
|
||||
}
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DictionaryPool<Key, Value> : PoolBase
|
||||
{
|
||||
private static DictionaryPool<Key, Value> Instance;
|
||||
|
||||
private Stack<Dictionary<Key, Value>> _pool;
|
||||
|
||||
private DictionaryPool() { }
|
||||
|
||||
private static void Init()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = new DictionaryPool<Key, Value>();
|
||||
Instance._pool = new Stack<Dictionary<Key, Value>>();
|
||||
Pool.AllPool.Add(Instance);
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<Key, Value> Get()
|
||||
{
|
||||
Init();
|
||||
|
||||
if (Instance._pool.Count > 0)
|
||||
{
|
||||
return Instance._pool.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Dictionary<Key, Value>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Release(Dictionary<Key, Value> dict)
|
||||
{
|
||||
if (dict == null || Instance == null) return;
|
||||
dict.Clear();
|
||||
Instance._pool.Push(dict);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
if (Instance._pool != null)
|
||||
{
|
||||
Instance._pool.Clear();
|
||||
Instance._pool = null;
|
||||
}
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59e9f74cfa6aa344c91bcdcc6db78ac6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
140
Assets/06.UIFarme/RunTime/UIFramework/Other/PrefabPool.cs
Normal file
140
Assets/06.UIFarme/RunTime/UIFramework/Other/PrefabPool.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class PrefabPool
|
||||
{
|
||||
/// <summary>
|
||||
/// 如果名字一样,则使用同一个池子
|
||||
/// </summary>
|
||||
public static Dictionary<string, PrefabPool> s_Pools = new Dictionary<string, PrefabPool>();
|
||||
|
||||
private string _poolName;
|
||||
private GameObject _prefab;
|
||||
private List<GameObject> _pool;
|
||||
private List<GameObject> _useList;
|
||||
|
||||
public GameObject Prefab => _prefab;
|
||||
public List<GameObject> UseList => _useList;
|
||||
|
||||
private PrefabPool() { }
|
||||
|
||||
private void Init(string poolName, GameObject prefab)
|
||||
{
|
||||
_pool = ListPool<GameObject>.Get();
|
||||
_useList = ListPool<GameObject>.Get();
|
||||
_prefab = prefab;
|
||||
_poolName = poolName;
|
||||
}
|
||||
|
||||
public static PrefabPool Get(string poolName)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(poolName))
|
||||
{
|
||||
if (s_Pools.TryGetValue(poolName, out var prefabPool))
|
||||
{
|
||||
return prefabPool;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PrefabPool Create(GameObject prefab, string poolName = null)
|
||||
{
|
||||
if (prefab == null) return null;
|
||||
if (!string.IsNullOrEmpty(poolName))
|
||||
{
|
||||
if (s_Pools.TryGetValue(poolName, out var prefabPool))
|
||||
{
|
||||
return prefabPool;
|
||||
}
|
||||
}
|
||||
var pool = new PrefabPool();
|
||||
pool.Init(poolName, prefab);
|
||||
if (!string.IsNullOrEmpty(poolName))
|
||||
{
|
||||
s_Pools.Add(poolName, pool);
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
public GameObject Get(Transform parent = null)
|
||||
{
|
||||
if (_prefab == null) return null;
|
||||
|
||||
GameObject go = null;
|
||||
if (_pool.Count > 0)
|
||||
{
|
||||
go = _pool[0];
|
||||
_pool.RemoveAt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
go = GameObject.Instantiate(_prefab);
|
||||
}
|
||||
go.transform.parent = parent;
|
||||
go.transform.localScale = Vector3.one;
|
||||
go.transform.localPosition = Vector3.zero;
|
||||
go.transform.localRotation = Quaternion.identity;
|
||||
go.SetActive(true);
|
||||
go.transform.SetAsLastSibling();
|
||||
_useList.Add(go);
|
||||
return go;
|
||||
}
|
||||
|
||||
public void Recycle(GameObject go)
|
||||
{
|
||||
if (go != null)
|
||||
{
|
||||
go.SetActive(false);
|
||||
_pool.Add(go);
|
||||
_useList.Remove(go);
|
||||
}
|
||||
}
|
||||
|
||||
public void RecycleUseList()
|
||||
{
|
||||
foreach (var go in _useList)
|
||||
{
|
||||
if (go != null)
|
||||
{
|
||||
go.SetActive(false);
|
||||
_pool.Add(go);
|
||||
}
|
||||
}
|
||||
_useList.Clear();
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
foreach (var go in _pool)
|
||||
{
|
||||
if (go != null)
|
||||
{
|
||||
GameObject.Destroy(go);
|
||||
}
|
||||
}
|
||||
_pool.Clear();
|
||||
foreach (var go in _useList)
|
||||
{
|
||||
if (go != null)
|
||||
{
|
||||
GameObject.Destroy(go);
|
||||
}
|
||||
}
|
||||
_useList.Clear();
|
||||
|
||||
ListPool<GameObject>.Release(_pool);
|
||||
ListPool<GameObject>.Release(_useList);
|
||||
|
||||
s_Pools.Remove(_poolName);
|
||||
|
||||
_pool = null;
|
||||
_useList = null;
|
||||
_prefab = null;
|
||||
_poolName = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02cdac12da067fa479f36e34fafd0987
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/06.UIFarme/RunTime/UIFramework/Other/Singleton.cs
Normal file
71
Assets/06.UIFarme/RunTime/UIFramework/Other/Singleton.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Singleton<T> where T : new()
|
||||
{
|
||||
private static T _ServiceContext;
|
||||
private readonly static object lockObj = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 禁止外部进行实例化
|
||||
/// </summary>
|
||||
protected Singleton()
|
||||
{
|
||||
OnInitialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取唯一实例,双锁定防止多线程并发时重复创建实例
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_ServiceContext == null)
|
||||
{
|
||||
lock (lockObj)
|
||||
{
|
||||
if (_ServiceContext == null)
|
||||
{
|
||||
_ServiceContext = new T();
|
||||
}
|
||||
}
|
||||
}
|
||||
return _ServiceContext;
|
||||
}
|
||||
}
|
||||
public virtual void OnInitialize() { }
|
||||
|
||||
public void Init() { }
|
||||
}
|
||||
|
||||
|
||||
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = (T)FindObjectOfType(typeof(T));
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject singleton = new GameObject();
|
||||
_instance = singleton.AddComponent<T>();
|
||||
singleton.name = typeof(T).ToString();
|
||||
DontDestroyOnLoad(singleton);
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnDestroy()
|
||||
{
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 26a0ecd7bd1fa9441a7229d39408180c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
52
Assets/06.UIFarme/RunTime/UIFramework/Other/TreeNode.cs
Normal file
52
Assets/06.UIFarme/RunTime/UIFramework/Other/TreeNode.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class TreeNode
|
||||
{
|
||||
public Dictionary<ulong, TreeNode> childs;
|
||||
public ulong id;
|
||||
public object data;
|
||||
|
||||
public TreeNode Get(ulong id)
|
||||
{
|
||||
if (childs == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
childs.TryGetValue(id, out var node);
|
||||
return node;
|
||||
}
|
||||
|
||||
public TreeNode GetOrAdd(ulong id)
|
||||
{
|
||||
if (childs == null)
|
||||
{
|
||||
childs = new Dictionary<ulong, TreeNode>();
|
||||
}
|
||||
if (!childs.TryGetValue(id, out var node))
|
||||
{
|
||||
node = ObjectPool<TreeNode>.Get();
|
||||
node.id = id;
|
||||
childs.Add(id, node);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
public void CleanUp()
|
||||
{
|
||||
if (childs != null)
|
||||
{
|
||||
foreach (var item in childs.Values)
|
||||
{
|
||||
item.CleanUp();
|
||||
ObjectPool<TreeNode>.Release(item);
|
||||
}
|
||||
childs.Clear();
|
||||
}
|
||||
id = 0;
|
||||
data = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/06.UIFarme/RunTime/UIFramework/Other/TreeNode.cs.meta
Normal file
11
Assets/06.UIFarme/RunTime/UIFramework/Other/TreeNode.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc97d6286a198a946a032cc4fa7fe2cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user