增加A2WToolBox工具集,增加Launch场景核LaunchPanel
This commit is contained in:
98
Assets/A2WToolBox/Runtime/PoolManager/ObjectPool.cs
Normal file
98
Assets/A2WToolBox/Runtime/PoolManager/ObjectPool.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MonsterLove.Collections
|
||||
{
|
||||
public class ObjectPool<T>
|
||||
{
|
||||
private List<ObjectPoolContainer<T>> list;
|
||||
private Dictionary<T, ObjectPoolContainer<T>> lookup;
|
||||
private Func<T> factoryFunc;
|
||||
private int lastIndex = 0;
|
||||
|
||||
public ObjectPool(Func<T> factoryFunc, int initialSize)
|
||||
{
|
||||
this.factoryFunc = factoryFunc;
|
||||
|
||||
list = new List<ObjectPoolContainer<T>>(initialSize);
|
||||
lookup = new Dictionary<T, ObjectPoolContainer<T>>(initialSize);
|
||||
|
||||
Warm(initialSize);
|
||||
}
|
||||
|
||||
private void Warm(int capacity)
|
||||
{
|
||||
for (int i = 0; i < capacity; i++)
|
||||
{
|
||||
CreateContainer();
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectPoolContainer<T> CreateContainer()
|
||||
{
|
||||
var container = new ObjectPoolContainer<T>();
|
||||
container.Item = factoryFunc();
|
||||
list.Add(container);
|
||||
return container;
|
||||
}
|
||||
|
||||
public T GetItem()
|
||||
{
|
||||
ObjectPoolContainer<T> container = null;
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
lastIndex++;
|
||||
if (lastIndex > list.Count - 1) lastIndex = 0;
|
||||
|
||||
if (list[lastIndex].Used)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
container = list[lastIndex];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (container == null)
|
||||
{
|
||||
container = CreateContainer();
|
||||
}
|
||||
|
||||
container.Consume();
|
||||
lookup.Add(container.Item, container);
|
||||
return container.Item;
|
||||
}
|
||||
|
||||
public void ReleaseItem(object item)
|
||||
{
|
||||
ReleaseItem((T) item);
|
||||
}
|
||||
|
||||
public void ReleaseItem(T item)
|
||||
{
|
||||
if (lookup.ContainsKey(item))
|
||||
{
|
||||
var container = lookup[item];
|
||||
container.Release();
|
||||
lookup.Remove(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("This object pool does not contain the item provided: " + item);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return list.Count; }
|
||||
}
|
||||
|
||||
public int CountUsedItems
|
||||
{
|
||||
get { return lookup.Count; }
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Assets/A2WToolBox/Runtime/PoolManager/ObjectPool.cs.meta
Normal file
8
Assets/A2WToolBox/Runtime/PoolManager/ObjectPool.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abe90b2b30f65a548825fbeba21cf864
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
32
Assets/A2WToolBox/Runtime/PoolManager/ObjectPoolContainer.cs
Normal file
32
Assets/A2WToolBox/Runtime/PoolManager/ObjectPoolContainer.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
namespace MonsterLove.Collections
|
||||
{
|
||||
public class ObjectPoolContainer<T>
|
||||
{
|
||||
private T item;
|
||||
|
||||
public bool Used { get; private set; }
|
||||
|
||||
public void Consume()
|
||||
{
|
||||
Used = true;
|
||||
}
|
||||
|
||||
public T Item
|
||||
{
|
||||
get
|
||||
{
|
||||
return item;
|
||||
}
|
||||
set
|
||||
{
|
||||
item = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Release()
|
||||
{
|
||||
Used = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64373aeb4afc30d43a9cbef9f4e90778
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
113
Assets/A2WToolBox/Runtime/PoolManager/PoolManager.cs
Normal file
113
Assets/A2WToolBox/Runtime/PoolManager/PoolManager.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MonsterLove.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace A2W
|
||||
{
|
||||
public class PoolManager : MonoBehaviour
|
||||
{
|
||||
public bool logStatus;
|
||||
public Transform root;
|
||||
|
||||
private Dictionary<GameObject, ObjectPool<GameObject>> prefabLookup;
|
||||
private Dictionary<GameObject, ObjectPool<GameObject>> instanceLookup;
|
||||
|
||||
private bool dirty = false;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
prefabLookup = new Dictionary<GameObject, ObjectPool<GameObject>>();
|
||||
instanceLookup = new Dictionary<GameObject, ObjectPool<GameObject>>();
|
||||
}
|
||||
|
||||
public void WarmPool(GameObject prefab, int size)
|
||||
{
|
||||
if (prefabLookup is null)
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
if (prefabLookup.ContainsKey(prefab))
|
||||
{
|
||||
throw new Exception("Pool for prefab " + prefab.name + " has already been created");
|
||||
}
|
||||
var pool = new ObjectPool<GameObject>(() => { return instantiatePrefab(prefab); }, size);
|
||||
prefabLookup[prefab] = pool;
|
||||
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
public GameObject SpawnObject(GameObject prefab)
|
||||
{
|
||||
return SpawnObject(prefab, Vector3.zero, Quaternion.identity);
|
||||
}
|
||||
|
||||
public GameObject SpawnObject(GameObject prefab, Vector3 position, Quaternion rotation)
|
||||
{
|
||||
if (!prefabLookup.ContainsKey(prefab))
|
||||
{
|
||||
WarmPool(prefab, 1);
|
||||
}
|
||||
|
||||
var pool = prefabLookup[prefab];
|
||||
|
||||
var clone = pool.GetItem();
|
||||
clone.transform.SetPositionAndRotation(position, rotation);
|
||||
clone.SetActive(true);
|
||||
|
||||
instanceLookup.Add(clone, pool);
|
||||
dirty = true;
|
||||
return clone;
|
||||
}
|
||||
|
||||
public void ReleaseObject(GameObject clone)
|
||||
{
|
||||
clone.SetActive(false);
|
||||
if (root != null) clone.transform.SetParent(root, false);
|
||||
|
||||
if (instanceLookup.ContainsKey(clone))
|
||||
{
|
||||
instanceLookup[clone].ReleaseItem(clone);
|
||||
instanceLookup.Remove(clone);
|
||||
dirty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("No pool contains the object: " + clone.name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private GameObject instantiatePrefab(GameObject prefab)
|
||||
{
|
||||
var go = Instantiate(prefab) as GameObject;
|
||||
//if (root != null) go.transform.parent = root;
|
||||
if (root != null) go.transform.SetParent(root, false);
|
||||
go.SetActive(false);
|
||||
return go;
|
||||
}
|
||||
|
||||
private void logUpdate()
|
||||
{
|
||||
if (logStatus && dirty)
|
||||
{
|
||||
printStatus();
|
||||
dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void printStatus()
|
||||
{
|
||||
foreach (KeyValuePair<GameObject, ObjectPool<GameObject>> keyVal in prefabLookup)
|
||||
{
|
||||
Debug.Log(string.Format("Object Pool for Prefab: {0} In Use: {1} Total {2}", keyVal.Key.name, keyVal.Value.CountUsedItems, keyVal.Value.Count));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 430d968e63581624e84fc43c08982663
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
Reference in New Issue
Block a user