两个页面接入完成
This commit is contained in:
37
Assets/Script/Pool/GameObjectPool.cs
Normal file
37
Assets/Script/Pool/GameObjectPool.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class GameObjectPool : MonoBehaviour
|
||||
{
|
||||
[Tooltip("预制体")] public GameObject prefab;
|
||||
[Tooltip("初始预加载数量")] public int preload = 10;
|
||||
|
||||
private Stack<GameObject> _pool = new();
|
||||
public Transform parent;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
for (var i = 0; i < preload; i++)
|
||||
{
|
||||
var go = Instantiate(prefab, transform);
|
||||
go.SetActive(false);
|
||||
_pool.Push(go);
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject Get()
|
||||
{
|
||||
var go = _pool.Count > 0 ? _pool.Pop() : Instantiate(prefab, parent);
|
||||
go.transform.parent = parent;
|
||||
go.SetActive(true);
|
||||
return go;
|
||||
}
|
||||
|
||||
public void Release(GameObject go)
|
||||
{
|
||||
if (go == null) return;
|
||||
go.SetActive(false);
|
||||
go.transform.SetParent(transform, false);
|
||||
_pool.Push(go);
|
||||
}
|
||||
}
|
||||
11
Assets/Script/Pool/GameObjectPool.cs.meta
Normal file
11
Assets/Script/Pool/GameObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1970feb7fdb702645b753cd8ed1be4ea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user