提交
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae1c8a81733497d4c8640487571d4772
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public class Jcna<T> where T : new()
|
||||
{
|
||||
private Stack<T> m_Stack = new();
|
||||
|
||||
private Action<T> m_ActionOnNew;
|
||||
private Action<T> m_ActionOnGet;
|
||||
private Action<T> m_ActionOnRelease;
|
||||
|
||||
public int CountAll { get; private set; }
|
||||
|
||||
public int CountInactive
|
||||
{
|
||||
get { return m_Stack.Count; }
|
||||
}
|
||||
|
||||
public int CountActive
|
||||
{
|
||||
get { return CountAll - CountInactive; }
|
||||
}
|
||||
|
||||
public Jcna()
|
||||
{
|
||||
}
|
||||
|
||||
public Jcna(Action<T> actionOnGet, Action<T> actionOnRelease)
|
||||
{
|
||||
m_ActionOnGet = actionOnGet;
|
||||
m_ActionOnRelease = actionOnRelease;
|
||||
}
|
||||
|
||||
public Jcna(Action<T> actionOnNew, Action<T> actionOnGet, Action<T> actionOnRelease)
|
||||
{
|
||||
m_ActionOnNew = actionOnNew;
|
||||
m_ActionOnGet = actionOnGet;
|
||||
m_ActionOnRelease = actionOnRelease;
|
||||
}
|
||||
|
||||
public T Get()
|
||||
{
|
||||
T element;
|
||||
if (m_Stack.Count == 0)
|
||||
{
|
||||
element = new T();
|
||||
CountAll++;
|
||||
|
||||
if (m_ActionOnNew != null)
|
||||
{
|
||||
m_ActionOnNew(element);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
element = m_Stack.Pop();
|
||||
}
|
||||
|
||||
if (m_ActionOnGet != null)
|
||||
m_ActionOnGet(element);
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
public void Release(T element)
|
||||
{
|
||||
if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
|
||||
Debug.LogError("[ObjectPool]Error: Trying to destroy object that is already released to pool!");
|
||||
|
||||
if (m_ActionOnRelease != null)
|
||||
{
|
||||
m_ActionOnRelease(element);
|
||||
}
|
||||
|
||||
m_Stack.Push(element);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_Stack.Clear();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_Stack.Clear();
|
||||
m_Stack = null;
|
||||
|
||||
m_ActionOnNew = null;
|
||||
m_ActionOnGet = null;
|
||||
m_ActionOnRelease = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a28a8a5d75b01a048af3d789b3c0f265
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public static class Uncc<T>
|
||||
{
|
||||
private static readonly Jcna<List<T>> s_ListPool = new(null, Clear);
|
||||
|
||||
private static void Clear(List<T> l)
|
||||
{
|
||||
l.Clear();
|
||||
}
|
||||
|
||||
public static List<T> Get()
|
||||
{
|
||||
return s_ListPool.Get();
|
||||
}
|
||||
|
||||
public static void Release(List<T> toRelease)
|
||||
{
|
||||
s_ListPool.Release(toRelease);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2d8e7c78227e3f43bffd43d95be542d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22593bf013269694ea57d36cc1159151
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public class ShungTik
|
||||
{
|
||||
private Transform m_releaseRoot;
|
||||
private Transform m_getRoot;
|
||||
private Action<GameObject> m_onNew;
|
||||
private Action<GameObject> m_onGet;
|
||||
private Action<GameObject> m_onRelease;
|
||||
|
||||
private Dictionary<string, Shungbass> m_pools = new();
|
||||
|
||||
public int CountAll
|
||||
{
|
||||
get
|
||||
{
|
||||
int countAll = 0;
|
||||
foreach (Shungbass poolItem in m_pools.Values)
|
||||
{
|
||||
countAll += poolItem.CountAll;
|
||||
}
|
||||
|
||||
return countAll;
|
||||
}
|
||||
}
|
||||
|
||||
public int CountInactive
|
||||
{
|
||||
get
|
||||
{
|
||||
int countInactive = 0;
|
||||
foreach (Shungbass poolItem in m_pools.Values)
|
||||
{
|
||||
countInactive += poolItem.CountInactive;
|
||||
}
|
||||
|
||||
return countInactive;
|
||||
}
|
||||
}
|
||||
|
||||
public int CountActive
|
||||
{
|
||||
get
|
||||
{
|
||||
int countActive = 0;
|
||||
foreach (Shungbass poolItem in m_pools.Values)
|
||||
{
|
||||
countActive += poolItem.CountActive;
|
||||
}
|
||||
|
||||
return countActive;
|
||||
}
|
||||
}
|
||||
|
||||
public void InitRoot(Transform releaseRoot = null, Transform getRoot = null)
|
||||
{
|
||||
m_releaseRoot = releaseRoot;
|
||||
m_getRoot = getRoot;
|
||||
}
|
||||
|
||||
public void InitCallBack(Action<GameObject> onNew, Action<GameObject> onGet, Action<GameObject> onRelease)
|
||||
{
|
||||
m_onNew = onNew;
|
||||
m_onGet = onGet;
|
||||
m_onRelease = onRelease;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public GameObject Get(string prefabPath, string assetName)
|
||||
{
|
||||
var poolName = $"{prefabPath} | {assetName}";
|
||||
if (!m_pools.ContainsKey(poolName))
|
||||
{
|
||||
RegisterNew(prefabPath, assetName);
|
||||
}
|
||||
|
||||
Shungbass pool = m_pools[poolName];
|
||||
return pool.Get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Release(string prefabPath, GameObject obj)
|
||||
{
|
||||
Shungbass pool = null;
|
||||
if (m_pools.TryGetValue(prefabPath, out pool))
|
||||
{
|
||||
pool.Release(obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Release(GameObject obj)
|
||||
{
|
||||
Shungbass pool = null;
|
||||
foreach (Shungbass poolItem in m_pools.Values)
|
||||
{
|
||||
if (poolItem.Contains(obj))
|
||||
{
|
||||
pool = poolItem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pool.Release(obj);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
foreach (Shungbass poolItem in m_pools.Values)
|
||||
{
|
||||
poolItem.ReleaseAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void RegisterNew(string prefabPath, string assetName)
|
||||
{
|
||||
Shungbass pool = new Shungbass();
|
||||
pool.Init(prefabPath, assetName, m_releaseRoot, m_getRoot);
|
||||
pool.InitCallBack(m_onNew, m_onGet, m_onRelease);
|
||||
m_pools.Add($"{prefabPath} | {assetName}", pool);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ClearPool(string prefabPath)
|
||||
{
|
||||
Shungbass pool = null;
|
||||
if (m_pools.TryGetValue(prefabPath, out pool))
|
||||
{
|
||||
pool.Clear();
|
||||
m_pools.Remove(prefabPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (Shungbass pool in m_pools.Values)
|
||||
{
|
||||
pool.Clear();
|
||||
}
|
||||
|
||||
m_pools.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Clear();
|
||||
m_pools = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bee36af719023d5418e02e67aa18ba0f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using BingoBrain.Asset;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public class Shungbass
|
||||
{
|
||||
private Transform m_prefabRoot;
|
||||
private Transform m_releaseRoot;
|
||||
private Transform m_getRoot;
|
||||
|
||||
private Action<GameObject> m_onNew;
|
||||
private Action<GameObject> m_onGet;
|
||||
private Action<GameObject> m_onRelease;
|
||||
|
||||
private List<GameObject> m_objects;
|
||||
private GameObject m_prefab;
|
||||
|
||||
public int CountAll { get; private set; }
|
||||
public int CountInactive { get; private set; }
|
||||
|
||||
public int CountActive
|
||||
{
|
||||
get { return CountAll - CountInactive; }
|
||||
}
|
||||
|
||||
public void Init(string prefabPath, string assetName, Transform releaseRoot = null, Transform getRoot = null)
|
||||
{
|
||||
m_objects = new List<GameObject>();
|
||||
m_prefab = BetKit.Instance.LoadGameObject(prefabPath, assetName);
|
||||
|
||||
m_prefabRoot = m_prefab.transform.parent;
|
||||
m_releaseRoot = releaseRoot;
|
||||
m_getRoot = getRoot;
|
||||
}
|
||||
|
||||
public void Init(GameObject prefab, Transform releaseRoot = null, Transform getRoot = null)
|
||||
{
|
||||
m_objects = new List<GameObject>();
|
||||
m_prefab = prefab;
|
||||
|
||||
m_prefabRoot = m_prefab.transform.parent;
|
||||
m_releaseRoot = releaseRoot;
|
||||
m_getRoot = getRoot;
|
||||
}
|
||||
|
||||
public void InitCallBack(Action<GameObject> onNew, Action<GameObject> onGet, Action<GameObject> onRelease)
|
||||
{
|
||||
m_onNew = onNew;
|
||||
m_onGet = onGet;
|
||||
m_onRelease = onRelease;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public GameObject Get()
|
||||
{
|
||||
GameObject obj = null;
|
||||
for (int i = m_objects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
GameObject objItem = m_objects[i];
|
||||
if (!objItem)
|
||||
{
|
||||
m_objects.Remove(objItem);
|
||||
CountAll--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!objItem.activeSelf)
|
||||
{
|
||||
obj = objItem;
|
||||
CountInactive--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
obj = Uvsjk.Instantiate(m_prefab);
|
||||
m_objects.Add(obj);
|
||||
CountAll++;
|
||||
|
||||
if (m_onNew != null)
|
||||
{
|
||||
m_onNew(obj);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_getRoot)
|
||||
{
|
||||
obj.transform.SetParent(m_getRoot, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_prefabRoot)
|
||||
{
|
||||
obj.transform.SetParent(m_prefabRoot, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_onGet != null)
|
||||
{
|
||||
m_onGet(obj);
|
||||
}
|
||||
|
||||
obj.SetActive(true);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Release(GameObject obj)
|
||||
{
|
||||
if (Contains(obj))
|
||||
{
|
||||
if (m_releaseRoot)
|
||||
{
|
||||
obj.transform.SetParent(m_releaseRoot, false);
|
||||
}
|
||||
|
||||
obj.SetActive(false);
|
||||
CountInactive++;
|
||||
|
||||
if (m_onRelease != null)
|
||||
{
|
||||
m_onRelease(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public bool Contains(GameObject obj)
|
||||
{
|
||||
return m_objects.Contains(obj);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
for (int i = m_objects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
GameObject objItem = m_objects[i];
|
||||
if (!objItem)
|
||||
{
|
||||
m_objects.Remove(objItem);
|
||||
CountAll--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (objItem.activeSelf)
|
||||
{
|
||||
Release(objItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ClearNotActive()
|
||||
{
|
||||
for (int i = m_objects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
GameObject objItem = m_objects[i];
|
||||
if (!objItem.activeSelf)
|
||||
{
|
||||
DestroyObj(objItem);
|
||||
m_objects.Remove(objItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
for (int i = m_objects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
GameObject objItem = m_objects[i];
|
||||
if (!objItem)
|
||||
{
|
||||
m_objects.Remove(objItem);
|
||||
CountAll--;
|
||||
continue;
|
||||
}
|
||||
|
||||
DestroyObj(objItem);
|
||||
}
|
||||
|
||||
m_objects.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void DestroyObj(GameObject obj)
|
||||
{
|
||||
Uvsjk.Destroy(obj.gameObject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Clear();
|
||||
m_prefab = null;
|
||||
m_objects = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eea317353b47da1469658e1c85c9750c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user