ball 项目提交
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22593bf013269694ea57d36cc1159151
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class GObjectPool
|
||||
{
|
||||
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 void Init(string prefabPath, string assetName, Transform releaseRoot = null, Transform getRoot = null)
|
||||
{
|
||||
m_objects = new List<GameObject>();
|
||||
m_prefab = LoadKit.Instance.LoadGameObject(prefabPath, assetName);
|
||||
|
||||
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 = GeneralKit.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);
|
||||
}
|
||||
|
||||
private void DestroyObj(GameObject obj)
|
||||
{
|
||||
GeneralKit.Destroy(obj.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eea317353b47da1469658e1c85c9750c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class GObjectsPool
|
||||
{
|
||||
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, GObjectPool> m_pools = new();
|
||||
|
||||
public GameObject Get(string prefabPath, string assetName)
|
||||
{
|
||||
var poolName = $"{prefabPath} | {assetName}";
|
||||
if (!m_pools.ContainsKey(poolName))
|
||||
{
|
||||
RegisterNew(prefabPath, assetName);
|
||||
}
|
||||
|
||||
GObjectPool pool = m_pools[poolName];
|
||||
return pool.Get();
|
||||
}
|
||||
|
||||
private void RegisterNew(string prefabPath, string assetName)
|
||||
{
|
||||
GObjectPool pool = new GObjectPool();
|
||||
pool.Init(prefabPath, assetName, m_releaseRoot, m_getRoot);
|
||||
pool.InitCallBack(m_onNew, m_onGet, m_onRelease);
|
||||
m_pools.Add($"{prefabPath} | {assetName}", pool);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bee36af719023d5418e02e67aa18ba0f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae1c8a81733497d4c8640487571d4772
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public static class ListPool<T>
|
||||
{
|
||||
private static readonly ObjectPool<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,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class ObjectPool<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 ObjectPool()
|
||||
{
|
||||
}
|
||||
|
||||
public ObjectPool(Action<T> actionOnGet, Action<T> actionOnRelease)
|
||||
{
|
||||
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_ActionOnRelease != null)
|
||||
{
|
||||
m_ActionOnRelease(element);
|
||||
}
|
||||
|
||||
m_Stack.Push(element);
|
||||
}
|
||||
|
||||
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:
|
||||
Reference in New Issue
Block a user