refactor radial menu, every click work as cancel - problem with invoke in radial button add listener
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class PopupItemMenu : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private Button _popupMenuItemButtonPrefab;
|
||||
|
||||
public void ShowButtons(List<ItemsMenuActionSO> actions, Action menuButtonClick)
|
||||
{
|
||||
var button = Instantiate(_popupMenuItemButtonPrefab) as Button;
|
||||
button.transform.SetParent(transform, false);
|
||||
button.transform.localPosition = new Vector3(0, 100f, 0);
|
||||
|
||||
var textMeshPro = button.GetComponent<TextMeshPro>();
|
||||
if (textMeshPro != null)
|
||||
{
|
||||
textMeshPro.text = actions[0].ActionName;
|
||||
}
|
||||
|
||||
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
menuButtonClick?.Invoke();
|
||||
Hide();
|
||||
CloseDialog();
|
||||
});
|
||||
}
|
||||
private void CloseDialog()
|
||||
{
|
||||
UIManager.Instance.Unfreeze();
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class RadialMenuItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private Button _radialMenuItemPrefab;
|
||||
|
||||
public void ShowButtons(List<RadialMenuActionSO> actions, Action<RadialMenuActionSO> 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;
|
||||
}
|
||||
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
Hide();
|
||||
CloseDialog();
|
||||
menuButtonClick?.Invoke(actions[1]);
|
||||
});
|
||||
}
|
||||
}
|
||||
private void CloseDialog()
|
||||
{
|
||||
UIManager.Instance.Unfreeze();
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0dea2e9a092ecf643963bb697b9963ca
|
||||
guid: 0652e5472d3afcf4f8d99f61ca2dc62e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -11,7 +11,7 @@ public class UIManager : MonoBehaviour
|
||||
[SerializeField]
|
||||
private GameObject _blurOverlay;
|
||||
[SerializeField]
|
||||
private PopupItemMenu _itemPopupMenuPrefab;
|
||||
private RadialMenuItem _radialMenuItemPrefab;
|
||||
|
||||
public static UIManager Instance { get; private set; }
|
||||
|
||||
@@ -41,9 +41,9 @@ public class UIManager : MonoBehaviour
|
||||
jobSelector.ShowJobSelectionDialog(title, onCancel, onConfirm);
|
||||
}
|
||||
|
||||
public void ShowItemsMenu(List<ItemsMenuActionSO> actions,Action itemsMenuCallback)
|
||||
public void ShowItemsMenu(List<RadialMenuActionSO> actions,Action<RadialMenuActionSO> itemsMenuCallback)
|
||||
{
|
||||
var popupMenu = Instantiate(_itemPopupMenuPrefab) as PopupItemMenu;
|
||||
var popupMenu = Instantiate(_radialMenuItemPrefab);
|
||||
popupMenu.transform.transform.SetParent(transform, false);
|
||||
popupMenu.transform.position = Input.mousePosition;
|
||||
popupMenu.ShowButtons(actions,itemsMenuCallback);
|
||||
|
||||
Reference in New Issue
Block a user