59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class RadialMenuItem : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Button _radialMenuItemPrefab;
|
|
private Action<RadialMenuActionSO> _menuButtonClick=null;
|
|
List<RadialMenuActionSO> _actions;
|
|
|
|
public void ShowButtons(List<RadialMenuActionSO> actions, Action<RadialMenuActionSO> menuButtonClick)
|
|
{
|
|
_actions = actions;
|
|
_menuButtonClick = menuButtonClick;
|
|
for (int buttonsCount = 0; buttonsCount < actions.Count; buttonsCount++)
|
|
{
|
|
var button = Instantiate(_radialMenuItemPrefab);
|
|
button.transform.SetParent(transform, false);
|
|
|
|
float theta = (2 * Mathf.PI / actions.Count) * buttonsCount;
|
|
float posX=Mathf.Sin(theta);
|
|
float posY=Mathf.Cos(theta);
|
|
|
|
button.transform.localPosition = new Vector3(posX, posY, 0)*100f;
|
|
var textMeshPro = button.GetComponentInChildren<TextMeshProUGUI>();
|
|
if (textMeshPro != null)
|
|
{
|
|
textMeshPro.text = actions[buttonsCount].ActionName;
|
|
}
|
|
|
|
AddEvent(button, buttonsCount);
|
|
}
|
|
}
|
|
|
|
void AddEvent(Button b, int i)
|
|
{
|
|
b.onClick.AddListener(() =>
|
|
{
|
|
Hide();
|
|
_menuButtonClick?.Invoke(_actions[i]);
|
|
CloseDialog();
|
|
});
|
|
}
|
|
|
|
private void CloseDialog()
|
|
{
|
|
UIManager.Instance.Unfreeze();
|
|
Destroy(this);
|
|
}
|
|
|
|
private void Hide()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|