Programming-Design-Patterns.../Assets/Patterns_And_Principles/Patterns/Object Pool Pattern/BasicPool/Script/BulletPoolExample.cs

31 lines
No EOL
797 B
C#

using System;
using UnityEngine;
namespace Patterns_And_Principles.Patterns.Object_Pool_Pattern.BasicPool.Script
{
public class BulletPoolExample : MonoBehaviour
{
[SerializeField]
private SimpleBullet prefab;
private ObjectPool<SimpleBullet> _bulletPool;
private void Start()
{
_bulletPool = new ObjectPool<SimpleBullet>(prefab, transform, 20, false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SimpleBullet bullet = _bulletPool.Get();
bullet.transform.position = transform.position;
}
}
public void ReturnBullet(SimpleBullet bullet)
{
_bulletPool.Release(bullet);
}
}
}