【m】1111

This commit is contained in:
2025-09-23 11:18:38 +08:00
parent b04e548850
commit a2e0062a34
202 changed files with 13296 additions and 56 deletions

View File

@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Stary.Evo
{
public class InstancePool
{
private static string GameObjectName = "GameObjectPool";
private static string RecycleName = "RecyclePool";
private Dictionary<string, Stack<GameObject>> _allInstances = new Dictionary<string, Stack<GameObject>>();
private Transform _instancePoolTransRoot = null;
private Transform _recyclePoolTransRoot = null;
public InstancePool()
{
var go = new GameObject(GameObjectName);
GameObject.DontDestroyOnLoad(go);
_instancePoolTransRoot = go.transform;
go.SetActive(true);
_recyclePoolTransRoot = _instancePoolTransRoot.Find(RecycleName);
if (_recyclePoolTransRoot == null)
{
_recyclePoolTransRoot = new GameObject(RecycleName).transform;
_recyclePoolTransRoot.SetParent(_instancePoolTransRoot);
}
_recyclePoolTransRoot.gameObject.SetActive(false);
}
public GameObject Get(string key)
{
Stack<GameObject> objects = null;
if (!_allInstances.TryGetValue(key, out objects))
{
return null;
}
else
{
if (objects == null || objects.Count == 0)
{
return null;
}
return objects.Pop();
}
}
public void Recycle(string key, GameObject obj, bool forceDestroy = false)
{
//强制删除
if (forceDestroy)
{
if (Application.isPlaying)
{
GameObject.Destroy(obj);
}
else
{
GameObject.DestroyImmediate(obj);
}
return;
}
Stack<GameObject> objects = null;
if (!_allInstances.TryGetValue(key, out objects))
{
objects = new Stack<GameObject>();
_allInstances[key] = objects;
}
InitInst(obj, false);
objects.Push(obj);
}
public void Clear(string key)
{
Stack<GameObject> objects = null;
if (_allInstances.TryGetValue(key, out objects))
{
while (objects.Count > 0)
{
GameObject objToDestroy = objects.Pop();
if (Application.isPlaying)
{
GameObject.Destroy(objToDestroy);
}
else
{
GameObject.DestroyImmediate(objToDestroy);
}
}
}
}
public void ClearAll()
{
foreach (var item in _allInstances)
{
while (item.Value.Count > 0)
{
GameObject objToDestroy = item.Value.Pop();
if (Application.isPlaying)
{
GameObject.Destroy(objToDestroy);
}
else
{
GameObject.DestroyImmediate(objToDestroy);
}
}
}
_allInstances.Clear();
}
public void InitInst(GameObject inst, bool active = true)
{
if (inst != null)
{
if (active)
{
inst.transform.SetParent(_instancePoolTransRoot, true);
}
else
{
inst.transform.SetParent(_recyclePoolTransRoot, true);
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b78b11e8c4a134f478ab61d5c6334deb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: