/
KirRo
/
Test_1
Обзор
Документация
Войти
/
KirRo
/
Test_1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/_Project/Scripts/MoneyObjectPool.cs
110 строк
3 KB
KirRo
Правки. Разделена логика игрока, камеры, UI, Object Pool. Ожидание сервисов ввода и аудио
30 июл 2026, 22:37
30 июл 2026, 22:37
d3abbab
Код
Авторство
О чём код?
using System.Collections.Generic; using UnityEngine; public class MoneyObjectPool : MonoBehaviour { [SerializeField] private GameObject _cashPackPrefab; [SerializeField] private GameObject _moneyBagPrefab; [SerializeField] private int _cashPackPreloadCount = 8; [SerializeField] private int _moneyBagPreloadCount = 3; [SerializeField] private Transform _container; private readonly Queue<CollectibleMoney> _cashPackPool = new(); private readonly Queue<CollectibleMoney> _moneyBagPool = new(); private readonly Dictionary<CollectibleMoney, Queue<CollectibleMoney>> _poolByCollectible = new(); private void Awake() { Preload(_cashPackPrefab, _cashPackPreloadCount, _cashPackPool); Preload(_moneyBagPrefab, _moneyBagPreloadCount, _moneyBagPool); } public CollectibleMoney GetCashPack(Vector3 position, Quaternion rotation) { return Get(_cashPackPrefab, _cashPackPool, position, rotation); } public CollectibleMoney GetMoneyBag(Vector3 position, Quaternion rotation) { return Get(_moneyBagPrefab, _moneyBagPool, position, rotation); } public void Return(CollectibleMoney collectible) { if (collectible == null) { return; } if (!_poolByCollectible.TryGetValue(collectible, out var pool)) { Debug.LogError("Collectible does not belong to this pool.", collectible); return; } collectible.gameObject.SetActive(false); pool.Enqueue(collectible); } private void Preload(GameObject prefab, int count, Queue<CollectibleMoney> pool) { for (var i = 0; i < count; i++) { var collectible = CreateCollectible(prefab, pool); if (collectible == null) { return; } collectible.gameObject.SetActive(false); pool.Enqueue(collectible); } } private CollectibleMoney Get( GameObject prefab, Queue<CollectibleMoney> pool, Vector3 position, Quaternion rotation ) { var collectible = pool.Count > 0 ? pool.Dequeue() : CreateCollectible(prefab, pool); if (collectible == null) { return null; } collectible.ResetCollectible(); collectible.transform.SetPositionAndRotation(position, rotation); collectible.gameObject.SetActive(true); return collectible; } private CollectibleMoney CreateCollectible(GameObject prefab, Queue<CollectibleMoney> pool) { if (prefab == null) { Debug.LogError("Money prefab is not assigned.", this); return null; } var instance = Instantiate(prefab, _container); var collectible = instance.GetComponent<CollectibleMoney>(); if (collectible == null) { Debug.LogError("Money prefab must have CollectibleMoney component.", instance); Destroy(instance); return null; } _poolByCollectible.Add(collectible, pool); return collectible; } }