You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
2 months ago
|
using UnityEngine;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
namespace D2D
|
||
|
{
|
||
|
public class PoolManager : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
[Tooltip("List of prefabs to spawn.")]
|
||
|
[SerializeField] private List<PoolMember> _prefabs; // List of prefabs
|
||
|
|
||
|
[Tooltip("List of UI buttons.")]
|
||
|
[SerializeField] private List<Button> _buttons; // List of buttons
|
||
|
|
||
|
private PoolMember _selectedPrefab; // Currently selected prefab
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
InitButtons(); // Initialize the buttons
|
||
|
}
|
||
|
|
||
|
// Initialize buttons with listeners
|
||
|
private void InitButtons()
|
||
|
{
|
||
|
for (int i = 0; i < _buttons.Count; i++)
|
||
|
{
|
||
|
int index = i; // Capture index for button listener
|
||
|
_buttons[i].onClick.AddListener(() => OnButtonPressed(index));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Triggered when a button is pressed
|
||
|
private void OnButtonPressed(int index)
|
||
|
{
|
||
|
if (index < _prefabs.Count)
|
||
|
{
|
||
|
_selectedPrefab = _prefabs[index]; // Assign the prefab corresponding to the button index
|
||
|
Debug.Log("Prefab selected: " + _selectedPrefab.name);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogWarning("No prefab available for this button.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Example method to spawn the selected prefab at a given position
|
||
|
public void SpawnSelectedPrefab(Vector3 position)
|
||
|
{
|
||
|
if (_selectedPrefab != null)
|
||
|
{
|
||
|
Instantiate(_selectedPrefab, position, Quaternion.identity);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogWarning("No prefab selected.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|