备份CatanBuilding瘦身独立工程
This commit is contained in:
115
Assets/Scripts/Pool/GameObjectPool.cs
Normal file
115
Assets/Scripts/Pool/GameObjectPool.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace asap.core.common
|
||||
{
|
||||
public class GameObjectPool<T> : AObjectPool
|
||||
where T : Component
|
||||
{
|
||||
private T prefab;
|
||||
|
||||
public GameObjectPool(T prefab, int preloadNum, int maxSize) : base(typeof(T), preloadNum, maxSize)
|
||||
{
|
||||
this.prefab = prefab;
|
||||
}
|
||||
|
||||
protected override object newObject()
|
||||
{
|
||||
var obj = GameObject.Instantiate(prefab);
|
||||
GameObject.DontDestroyOnLoad(obj);
|
||||
onCreateObject(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected override void destroyObject(object obj)
|
||||
{
|
||||
if (obj.Equals(null))
|
||||
return;
|
||||
|
||||
onDestroyObject(obj);
|
||||
|
||||
GameObject gameObject = (obj as T).gameObject;
|
||||
GameObject.Destroy(gameObject);
|
||||
}
|
||||
|
||||
protected override void onCreateObject(object obj)
|
||||
{
|
||||
GameObject gameObject = (obj as T).gameObject;
|
||||
gameObject.SetActive(false);
|
||||
|
||||
var comps = gameObject.GetComponents<IPoolableObject>();
|
||||
for (int i = 0; i < comps.Length; i++)
|
||||
{
|
||||
comps[i].OnPoolCreate(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void onDestroyObject(object obj)
|
||||
{
|
||||
GameObject gameObject = (obj as T).gameObject;
|
||||
if (gameObject.Equals(null))
|
||||
return;
|
||||
|
||||
var comps = gameObject.GetComponents<IPoolableObject>();
|
||||
for (int i = 0; i < comps.Length; i++)
|
||||
{
|
||||
comps[i].OnPoolDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void onSpawnObject(object obj)
|
||||
{
|
||||
GameObject gameObject = (obj as T).gameObject;
|
||||
if (!gameObject.activeSelf)
|
||||
gameObject.SetActive(true);
|
||||
|
||||
var comps = gameObject.GetComponents<IPoolableObject>();
|
||||
for(int i =0;i< comps.Length; i++)
|
||||
{
|
||||
comps[i].OnSpawn();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void onDespawnObject(object obj)
|
||||
{
|
||||
GameObject gameObject = (obj as T).gameObject;
|
||||
|
||||
//var ps = gameObject.GetComponent<ParticleSystem>();
|
||||
//if (ps != null)
|
||||
//{
|
||||
// ps.Stop(true);
|
||||
//}
|
||||
|
||||
//var audioSource = gameObject.GetComponent<AudioSource>();
|
||||
//if (audioSource != null)
|
||||
//{
|
||||
// audioSource.Stop();
|
||||
// audioSource.volume = 1f;
|
||||
// audioSource.priority = 128;
|
||||
// audioSource.loop = false;
|
||||
// audioSource.playOnAwake = false;
|
||||
// audioSource.clip = null;
|
||||
// audioSource.time = 0f;
|
||||
// audioSource.outputAudioMixerGroup = null;
|
||||
//}
|
||||
|
||||
var comps = gameObject.GetComponents<IPoolableObject>();
|
||||
for (int i = 0; i < comps.Length; i++)
|
||||
{
|
||||
comps[i].OnDespawn();
|
||||
}
|
||||
|
||||
gameObject.transform.SetParent(null, false);
|
||||
if (gameObject.activeSelf)
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void Release()
|
||||
{
|
||||
base.Release();
|
||||
|
||||
prefab = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Pool/GameObjectPool.cs.meta
Normal file
11
Assets/Scripts/Pool/GameObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c121bca0b60eb664ab6b5fb7569a5513
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
226
Assets/Scripts/Pool/ObjectPool.cs
Normal file
226
Assets/Scripts/Pool/ObjectPool.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace asap.core.common
|
||||
{
|
||||
public interface IObjectPool
|
||||
{
|
||||
// string PoolId { get; }
|
||||
|
||||
Type ObjectType { get; }
|
||||
int PoolObjectNum { get; }
|
||||
|
||||
object SpawnObject();
|
||||
|
||||
bool DespawnObject(object obj, float delay = 0f);
|
||||
|
||||
void Initialize();
|
||||
void Release();
|
||||
}
|
||||
|
||||
// public interface IObjectPool<T> : IObjectPool where T : class
|
||||
// {
|
||||
// new T SpawnObject();
|
||||
// void DespawnObject(T obj);
|
||||
// }
|
||||
|
||||
public interface IPoolableObject
|
||||
{
|
||||
void OnSpawn();
|
||||
void OnDespawn();
|
||||
|
||||
void OnPoolCreate(IObjectPool objectPool);
|
||||
//void Destroy();
|
||||
void OnPoolDestroy();
|
||||
void ReturnPool();
|
||||
}
|
||||
|
||||
public abstract class AObjectPool : IObjectPool
|
||||
{
|
||||
protected Queue<object> objectQuene = new Queue<object>();
|
||||
|
||||
protected Type objType;
|
||||
protected int maxSize;
|
||||
protected int preloadNum;
|
||||
|
||||
public Type ObjectType { get { return objType; } }
|
||||
|
||||
public int PoolObjectNum { get { return (null == objectQuene) ? 0 : objectQuene.Count; } }
|
||||
|
||||
protected string poolId;
|
||||
public string PoolId => poolId;
|
||||
|
||||
public AObjectPool(Type objType, int preloadNum, int maxSize)
|
||||
{
|
||||
this.objType = objType;
|
||||
this.maxSize = maxSize;
|
||||
this.preloadNum = preloadNum;
|
||||
|
||||
//Initialize();
|
||||
}
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
if (objType == null)
|
||||
{
|
||||
throw (new ArgumentNullException("objType"));
|
||||
}
|
||||
|
||||
if (preloadNum > 0)
|
||||
{
|
||||
generateObjects(preloadNum);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Release()
|
||||
{
|
||||
foreach (var obj in objectQuene)
|
||||
{
|
||||
destroyObject(obj);
|
||||
}
|
||||
|
||||
objectQuene.Clear();
|
||||
objectQuene = null;
|
||||
}
|
||||
|
||||
protected void generateObjects(int num)
|
||||
{
|
||||
object obj;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
obj = newObject();
|
||||
addObject(obj);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract object newObject();
|
||||
|
||||
protected abstract void destroyObject(object obj);
|
||||
|
||||
public virtual object SpawnObject()
|
||||
{
|
||||
object obj = getObject();
|
||||
|
||||
onSpawnObject(obj);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public virtual bool DespawnObject(object obj, float delay = 0f)
|
||||
{
|
||||
addObject(obj);
|
||||
|
||||
onDespawnObject(obj);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void onCreateObject(object obj)
|
||||
{
|
||||
if (obj is IPoolableObject)
|
||||
{
|
||||
var poolableObj = obj as IPoolableObject;
|
||||
poolableObj.OnPoolCreate(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void onSpawnObject(object obj)
|
||||
{
|
||||
if (obj is IPoolableObject)
|
||||
{
|
||||
var poolableObj = obj as IPoolableObject;
|
||||
poolableObj.OnSpawn();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void onDespawnObject(object obj)
|
||||
{
|
||||
if (obj is IList list)
|
||||
{
|
||||
list.Clear();
|
||||
}
|
||||
else if (obj is IDictionary dict)
|
||||
{
|
||||
dict.Clear();
|
||||
}
|
||||
else if (obj is ICollection collection)
|
||||
{
|
||||
var type = obj.GetType();
|
||||
var methodInfo = type.GetMethod("Clear");
|
||||
if (methodInfo != null)
|
||||
{
|
||||
// Debug.Log($"onDespawnObject: {collection.Count}, {methodInfo}");
|
||||
methodInfo.Invoke(obj, null);
|
||||
// Debug.Log($"onDespawnObject: {collection.Count}");
|
||||
}
|
||||
}
|
||||
else if (obj is IPoolableObject poolableObj)
|
||||
{
|
||||
poolableObj.OnDespawn();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void onDestroyObject(object obj)
|
||||
{
|
||||
if (obj is IPoolableObject)
|
||||
{
|
||||
var poolableObj = obj as IPoolableObject;
|
||||
poolableObj.OnPoolDestroy();
|
||||
}
|
||||
if (obj is IDisposable)
|
||||
{
|
||||
(obj as IDisposable).Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual object getObject()
|
||||
{
|
||||
object obj;
|
||||
if (objectQuene.Count > 0)
|
||||
{
|
||||
obj = objectQuene.Dequeue();
|
||||
// onSpawnObject(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = newObject();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected virtual void addObject(object obj)
|
||||
{
|
||||
if(!objType.IsAssignableFrom(obj.GetType()))
|
||||
return;
|
||||
#if UNITY_EDITOR
|
||||
if (objectQuene.Contains(obj))
|
||||
{
|
||||
UnityEngine.Debug.LogError($"Return obj {obj.GetType().FullName} is already in pool");
|
||||
}
|
||||
#endif
|
||||
objectQuene.Enqueue(obj);
|
||||
// onDespawnObject(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public class ObjectPool : AObjectPool
|
||||
{
|
||||
public ObjectPool(Type objType, int preloadNum, int maxSize) : base(objType, preloadNum, maxSize)
|
||||
{
|
||||
}
|
||||
|
||||
protected override object newObject()
|
||||
{
|
||||
object obj = Activator.CreateInstance(objType);
|
||||
onCreateObject(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected override void destroyObject(object obj)
|
||||
{
|
||||
onDestroyObject(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Pool/ObjectPool.cs.meta
Normal file
11
Assets/Scripts/Pool/ObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b78137820e08d7408daff7c8176b5d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
131
Assets/Scripts/Pool/ObjectPoolService.cs
Normal file
131
Assets/Scripts/Pool/ObjectPoolService.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using asap.core.common;
|
||||
using UnityEngine;
|
||||
|
||||
namespace asap.core.common
|
||||
{
|
||||
public interface IObjectPoolService
|
||||
{
|
||||
IObjectPool CreatePool(Type objType, int preloadNum = 0, int maxSize = 0);
|
||||
void DestroyPool(Type objType);
|
||||
// void DestroyPool(IObjectPool pool);
|
||||
|
||||
IObjectPool GetPool(Type type);
|
||||
|
||||
// IObjectPool GetPool<T>();
|
||||
object SpawnObject(Type type);
|
||||
void DespawnObject(object obj);
|
||||
|
||||
GameObjectPool<T> CreatePool<T>(T prefab, int preloadNum = 0, int maxSize = 0) where T : Component;
|
||||
}
|
||||
|
||||
public static class ObjectPoolServiceExtensions
|
||||
{
|
||||
public static IObjectPool CreatePool<T>(this IObjectPoolService service, int preloadNum = 0, int maxSize = 0)
|
||||
{
|
||||
return service.CreatePool(typeof(T), preloadNum, maxSize);
|
||||
}
|
||||
public static void DestroyPool<T>(this IObjectPoolService service)
|
||||
{
|
||||
service.DestroyPool(typeof(T));
|
||||
}
|
||||
|
||||
public static IObjectPool GetPool<T>(this IObjectPoolService service)
|
||||
{
|
||||
var type = typeof(T);
|
||||
return service.GetPool(type);
|
||||
}
|
||||
|
||||
public static T SpawnObject<T>(this IObjectPoolService service)
|
||||
{
|
||||
return (T)service.SpawnObject(typeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
public class ObjectPoolService : IObjectPoolService
|
||||
{
|
||||
private Dictionary<Type, IObjectPool> objectPoolDict;
|
||||
|
||||
public ObjectPoolService()
|
||||
{
|
||||
objectPoolDict = new Dictionary<Type, IObjectPool>();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
foreach (var pool in objectPoolDict.Values)
|
||||
{
|
||||
pool.Release();
|
||||
}
|
||||
objectPoolDict.Clear();
|
||||
objectPoolDict = null;
|
||||
}
|
||||
|
||||
public IObjectPool CreatePool(Type objType, int preloadNum = 0, int maxSize = 0)
|
||||
{
|
||||
if (objectPoolDict.TryGetValue(objType, out var pool))
|
||||
return pool;
|
||||
|
||||
pool = new ObjectPool(objType, preloadNum, maxSize);
|
||||
pool.Initialize();
|
||||
objectPoolDict.Add(objType, pool);
|
||||
return pool;
|
||||
}
|
||||
|
||||
public void DestroyPool(Type type)
|
||||
{
|
||||
if (!objectPoolDict.TryGetValue(type, out var pool))
|
||||
return;
|
||||
|
||||
pool.Release();
|
||||
objectPoolDict.Remove(type);
|
||||
}
|
||||
|
||||
public IObjectPool GetPool(Type type)
|
||||
{
|
||||
if(!objectPoolDict.TryGetValue(type, out var pool))
|
||||
pool = CreatePool(type);
|
||||
return pool;
|
||||
}
|
||||
|
||||
public object SpawnObject(Type type)
|
||||
{
|
||||
var pool = GetPool(type);
|
||||
if (pool == null)
|
||||
{
|
||||
pool = CreatePool(type);
|
||||
// return null;
|
||||
}
|
||||
return pool.SpawnObject();
|
||||
}
|
||||
|
||||
public void DespawnObject(object obj)
|
||||
{
|
||||
var pool = GetPool(obj.GetType());
|
||||
if (pool == null)
|
||||
return;
|
||||
|
||||
pool.DespawnObject(obj);
|
||||
}
|
||||
|
||||
public GameObjectPool<T> CreatePool<T>(T prefab, int preloadNum = 0, int maxSize = 0)
|
||||
where T : Component
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (!objectPoolDict.TryGetValue(type, out var pool))
|
||||
{
|
||||
pool = new GameObjectPool<T>(prefab, preloadNum, maxSize);
|
||||
pool.Initialize();
|
||||
objectPoolDict.Add(type, pool);
|
||||
}
|
||||
return pool as GameObjectPool<T>;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Pool/ObjectPoolService.cs.meta
Normal file
11
Assets/Scripts/Pool/ObjectPoolService.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1acf5c43f7a54ac418ad6a62930c2c72
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user