BlackLambert_unity-di-package/Samples/Pooling/Scripts/FooCreator.cs
using System;
using UnityEngine;
using UnityEngine.UI;

namespace SBaier.DI.Examples.Pooling
{
    internal class FooCreator : MonoBehaviour, Injectable
    {
        [SerializeField] private Button _addButton;
        [SerializeField] private Button _removeButton;
        [SerializeField] private Transform _fooHook;

        private Pool<Foo, PrefabInstantiationArguments> _pool;
        private Foo _currentFoo;
        private bool HasFoo => _currentFoo != null;

        public void Inject(Resolver resolver)
        {
            _pool = resolver.Resolve<Pool<Foo, PrefabInstantiationArguments>>();
        }

        private void OnEnable()
        {
            _addButton.onClick.AddListener(AddFoo);
            _removeButton.onClick.AddListener(RemoveFoo);
            UpdateInteractable();
        }

        private void OnDisable()
        {
            _addButton.onClick.RemoveListener(AddFoo);
            _removeButton.onClick.RemoveListener(RemoveFoo);
        }

        private void AddFoo()
        {
            if (HasFoo)
                return;
            _currentFoo = _pool.Request(PrefabInstantiationArguments.CreateFittedUIArgs(_fooHook));
            UpdateInteractable();
        }

        private void RemoveFoo()
        {
            if (!HasFoo)
                return;
            _pool.Return(_currentFoo);
            _currentFoo = null;
            UpdateInteractable();
        }

        private void UpdateInteractable()
        {
            _addButton.interactable = !HasFoo;
            _removeButton.interactable = HasFoo;
        }
    }
}