/
dr.puzo
/
ProjectPractice
Обзор
Документация
Войти
/
dr.puzo
/
ProjectPractice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Assets/Scripts/Utilities/ObjectPool.cs
77 строк
2 KB
CRYZORE
Файлы проекта перенесены в папку src
12 май 2026, 01:46
12 май 2026, 01:46
b981065
Код
Авторство
О чём код?
using UnityEngine; using System.Collections.Generic; namespace RogueSharpTutorial.Utilities { /// <summary> /// Object pool code is entirely based on tutorial code from Catlike Coding: https://catlikecoding.com/unity/tutorials/object-management/reusing-objects/ /// </summary> /// <param name="prefab"></param> /// <returns></returns> public class ObjectPool : MonoBehaviour { private PooledObject prefab; private List<PooledObject> availableObjects = new List<PooledObject>(); /// <summary> /// Create if necessary and return the Object Pool. /// </summary> /// <param name="prefab"></param> /// <returns></returns> public static ObjectPool GetPool(PooledObject prefab) { GameObject obj; ObjectPool pool; if (Application.isEditor) { obj = GameObject.Find(prefab.name + " Pool"); if (obj) { pool = obj.GetComponent<ObjectPool>(); if (pool) { return pool; } } } obj = new GameObject(prefab.name + " Pool"); DontDestroyOnLoad(obj); pool = obj.AddComponent<ObjectPool>(); pool.prefab = prefab; return pool; } /// <summary> /// Get a pooled obect from the Object Pool. /// </summary> /// <returns></returns> public PooledObject GetObject() { PooledObject obj; int lastAvailableIndex = availableObjects.Count - 1; if (lastAvailableIndex >= 0) { obj = availableObjects[lastAvailableIndex]; availableObjects.RemoveAt(lastAvailableIndex); obj.gameObject.SetActive(true); } else { obj = Instantiate<PooledObject>(prefab); obj.transform.SetParent(transform, false); obj.Pool = this; } return obj; } /// <summary> /// Set pooled object inactive and return to the Object Pool. /// </summary> /// <param name="obj"></param> public void AddObject(PooledObject obj) { obj.gameObject.SetActive(false); availableObjects.Add(obj); } } }