Files
MinFt/Client/Assets/Scripts/Pool/GameObjectPool.cs
Liubing\LB 3d8d4d18f3 first commit
先修复一下,错误的场景

删除不必要的钓场资产

修复into场景

update:更新meta文件,修复报错

update:修复资源

修复第一章节建造

修复建造第三章

update:删除多余内容

update:更新README.md

update:还原图标

update:更新README

update:更新配置
2026-04-28 02:17:22 +08:00

116 lines
3.2 KiB
C#

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;
}
}
}