基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
190
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuContent.cs
Normal file
190
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuContent.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[AddComponentMenu("Modern UI Pack/Context Menu/Context Menu Content")]
|
||||
public class ContextMenuContent : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
// Resources
|
||||
public ContextMenuManager contextManager;
|
||||
public Transform itemParent;
|
||||
|
||||
// Settings
|
||||
public bool useIn3D = false;
|
||||
|
||||
// Items
|
||||
public List<ContextItem> contexItems = new List<ContextItem>();
|
||||
|
||||
GameObject selectedItem;
|
||||
Image setItemImage;
|
||||
TextMeshProUGUI setItemText;
|
||||
Sprite imageHelper;
|
||||
string textHelper;
|
||||
|
||||
[System.Serializable]
|
||||
public class ContextItem
|
||||
{
|
||||
[Header("Information")]
|
||||
[Space(-5)]
|
||||
public string itemText = "Item Text";
|
||||
public Sprite itemIcon;
|
||||
public ContextItemType contextItemType;
|
||||
|
||||
[Header("Sub Menu")]
|
||||
public List<SubMenuItem> subMenuItems = new List<SubMenuItem>();
|
||||
|
||||
[Header("Events")]
|
||||
public UnityEvent onClick;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SubMenuItem
|
||||
{
|
||||
public string itemText = "Item Text";
|
||||
public Sprite itemIcon;
|
||||
public ContextItemType contextItemType;
|
||||
public UnityEvent onClick;
|
||||
}
|
||||
|
||||
public enum ContextItemType { Button, Separator }
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (contextManager == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
#if UNITY_2023_2_OR_NEWER
|
||||
contextManager = FindObjectsByType<ContextMenuManager>(FindObjectsSortMode.None)[0];
|
||||
#else
|
||||
contextManager = (ContextMenuManager)FindObjectsOfType(typeof(ContextMenuManager))[0];
|
||||
#endif
|
||||
itemParent = contextManager.transform.Find("Content/Item List").transform;
|
||||
}
|
||||
|
||||
catch { Debug.LogError("<b>[Context Menu]</b> Context Manager is missing.", this); return; }
|
||||
}
|
||||
|
||||
foreach (Transform child in itemParent)
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
public void ProcessContent()
|
||||
{
|
||||
foreach (Transform child in itemParent) { Destroy(child.gameObject); }
|
||||
for (int i = 0; i < contexItems.Count; ++i)
|
||||
{
|
||||
bool nulLVariable = false;
|
||||
|
||||
if (contexItems[i].contextItemType == ContextItemType.Button && contextManager.contextButton != null)
|
||||
selectedItem = contextManager.contextButton;
|
||||
else if (contexItems[i].contextItemType == ContextItemType.Separator && contextManager.contextSeparator != null)
|
||||
selectedItem = contextManager.contextSeparator;
|
||||
else
|
||||
{
|
||||
Debug.LogError("<b>[Context Menu]</b> At least one of the item presets is missing. " +
|
||||
"You can assign a new variable in Resources (Context Menu) tab. All default presets can be found in " +
|
||||
"<b>Modern UI Pack > Prefabs > Context Menu</b> folder.", this);
|
||||
nulLVariable = true;
|
||||
}
|
||||
|
||||
if (nulLVariable == false)
|
||||
{
|
||||
if (contexItems[i].subMenuItems.Count == 0)
|
||||
{
|
||||
GameObject go = Instantiate(selectedItem, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
|
||||
go.transform.SetParent(itemParent, false);
|
||||
|
||||
if (contexItems[i].contextItemType == ContextItemType.Button)
|
||||
{
|
||||
setItemText = go.GetComponentInChildren<TextMeshProUGUI>();
|
||||
textHelper = contexItems[i].itemText;
|
||||
setItemText.text = textHelper;
|
||||
|
||||
Transform goImage = go.gameObject.transform.Find("Icon");
|
||||
setItemImage = goImage.GetComponent<Image>();
|
||||
imageHelper = contexItems[i].itemIcon;
|
||||
setItemImage.sprite = imageHelper;
|
||||
|
||||
if (imageHelper == null)
|
||||
setItemImage.color = new Color(0, 0, 0, 0);
|
||||
|
||||
Button itemButton = go.GetComponent<Button>();
|
||||
itemButton.onClick.AddListener(contexItems[i].onClick.Invoke);
|
||||
itemButton.onClick.AddListener(contextManager.Close);
|
||||
}
|
||||
}
|
||||
|
||||
else if (contextManager.contextSubMenu != null && contexItems[i].subMenuItems.Count != 0)
|
||||
{
|
||||
GameObject go = Instantiate(contextManager.contextSubMenu, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
|
||||
go.transform.SetParent(itemParent, false);
|
||||
|
||||
ContextMenuSubMenu subMenu = go.GetComponent<ContextMenuSubMenu>();
|
||||
subMenu.cmManager = contextManager;
|
||||
subMenu.cmContent = this;
|
||||
subMenu.subMenuIndex = i;
|
||||
|
||||
setItemText = go.GetComponentInChildren<TextMeshProUGUI>();
|
||||
textHelper = contexItems[i].itemText;
|
||||
setItemText.text = textHelper;
|
||||
|
||||
Transform goImage;
|
||||
goImage = go.gameObject.transform.Find("Icon");
|
||||
setItemImage = goImage.GetComponent<Image>();
|
||||
imageHelper = contexItems[i].itemIcon;
|
||||
setItemImage.sprite = imageHelper;
|
||||
}
|
||||
|
||||
StopCoroutine("ExecuteAfterTime");
|
||||
StartCoroutine("ExecuteAfterTime", 0.01f);
|
||||
}
|
||||
}
|
||||
|
||||
contextManager.SetContextMenuPosition();
|
||||
contextManager.Open();
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (contextManager.isOn == true) { contextManager.Close(); }
|
||||
else if (eventData.button == PointerEventData.InputButton.Right && contextManager.isOn == false) { ProcessContent(); }
|
||||
}
|
||||
|
||||
IEnumerator ExecuteAfterTime(float time)
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(time);
|
||||
itemParent.gameObject.SetActive(false);
|
||||
itemParent.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
#if !UNITY_IOS && !UNITY_ANDROID
|
||||
public void OnMouseOver()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
if (useIn3D == true && Input.GetMouseButtonDown(1))
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
if (useIn3D == true && Mouse.current.rightButton.wasPressedThisFrame)
|
||||
#endif
|
||||
{
|
||||
ProcessContent();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public void AddNewItem()
|
||||
{
|
||||
ContextItem item = new ContextItem();
|
||||
contexItems.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe4f202dc85015b48aa586eba6d38692
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: cceded5b1d0834e48849fb152ac8e53d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuContent.cs
|
||||
uploadId: 628721
|
||||
@@ -0,0 +1,86 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CustomEditor(typeof(ContextMenuContent))]
|
||||
public class ContextMenuContentEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private ContextMenuContent cmcTarget;
|
||||
private int currentTab;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
cmcTarget = (ContextMenuContent)target;
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "CM Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[3];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Resources");
|
||||
toolbarTabs[2] = new GUIContent("Settings");
|
||||
|
||||
currentTab = MUIPEditorHandler.DrawTabs(currentTab, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
currentTab = 0;
|
||||
if (GUILayout.Button(new GUIContent("Resources", "Resources"), customSkin.FindStyle("Tab Resources")))
|
||||
currentTab = 1;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
currentTab = 2;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var contextManager = serializedObject.FindProperty("contextManager");
|
||||
var itemParent = serializedObject.FindProperty("itemParent");
|
||||
var contexItems = serializedObject.FindProperty("contexItems");
|
||||
var useIn3D = serializedObject.FindProperty("useIn3D");
|
||||
|
||||
switch (currentTab)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Content Header", 6);
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
GUILayout.BeginVertical();
|
||||
#else
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
#endif
|
||||
EditorGUI.indentLevel = 1;
|
||||
|
||||
EditorGUILayout.PropertyField(contexItems, new GUIContent("Context Menu Items"), true);
|
||||
contexItems.isExpanded = true;
|
||||
|
||||
EditorGUI.indentLevel = 0;
|
||||
|
||||
if (GUILayout.Button("+ Add a new item", customSkin.button))
|
||||
cmcTarget.AddNewItem();
|
||||
|
||||
GUILayout.EndVertical();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Core Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(contextManager, customSkin, "Context Manager");
|
||||
MUIPEditorHandler.DrawProperty(itemParent, customSkin, "Item Parent");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Options Header", 6);
|
||||
useIn3D.boolValue = MUIPEditorHandler.DrawToggle(useIn3D.boolValue, customSkin, "Use In 3D");
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6027ca9dc17f95b4f8e126a1326329ea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuContentEditor.cs
|
||||
uploadId: 628721
|
||||
@@ -0,0 +1,159 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[AddComponentMenu("Modern UI Pack/Context Menu/Context Menu Content (Mobile)")]
|
||||
public class ContextMenuContentMobile : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
[Header("Resources")]
|
||||
public ContextMenuManager contextManager;
|
||||
public Transform itemParent;
|
||||
|
||||
[Header("Settings")]
|
||||
[Range(0.1f, 6)] public float holdToOpen = 0.75f;
|
||||
|
||||
[Header("Items")]
|
||||
public List<ContextItem> contexItems = new List<ContextItem>();
|
||||
|
||||
Animator contextAnimator;
|
||||
GameObject selectedItem;
|
||||
Image setItemImage;
|
||||
TextMeshProUGUI setItemText;
|
||||
Sprite imageHelper;
|
||||
string textHelper;
|
||||
float timer;
|
||||
bool timerEnabled;
|
||||
|
||||
[System.Serializable]
|
||||
public class ContextItem
|
||||
{
|
||||
public string itemText = "Item Text";
|
||||
public Sprite itemIcon;
|
||||
public ContextItemType contextItemType;
|
||||
public UnityEvent onClick;
|
||||
}
|
||||
|
||||
public enum ContextItemType
|
||||
{
|
||||
BUTTON
|
||||
// SUB_MENU
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (contextManager == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
contextManager = GameObject.Find("Context Menu").GetComponent<ContextMenuManager>();
|
||||
itemParent = contextManager.transform.Find("Content/Item List").transform;
|
||||
}
|
||||
|
||||
catch { Debug.Log("<b>[Context Menu]</b> Context Manager is missing.", this); return; }
|
||||
}
|
||||
|
||||
contextAnimator = contextManager.contextAnimator;
|
||||
|
||||
foreach (Transform child in itemParent)
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (timerEnabled == true)
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
|
||||
if (timer >= holdToOpen)
|
||||
{
|
||||
CheckForTimer();
|
||||
timerEnabled = false;
|
||||
timer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
timerEnabled = true;
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
timerEnabled = false;
|
||||
timer = 0;
|
||||
}
|
||||
|
||||
public void CheckForTimer()
|
||||
{
|
||||
if (timer <= holdToOpen)
|
||||
return;
|
||||
|
||||
if (contextManager.isOn == true)
|
||||
{
|
||||
contextAnimator.Play("Menu Out");
|
||||
contextManager.isOn = false;
|
||||
}
|
||||
|
||||
else if (contextManager.isOn == false)
|
||||
{
|
||||
foreach (Transform child in itemParent)
|
||||
Destroy(child.gameObject);
|
||||
|
||||
for (int i = 0; i < contexItems.Count; ++i)
|
||||
{
|
||||
if (contexItems[i].contextItemType == ContextItemType.BUTTON)
|
||||
selectedItem = contextManager.contextButton;
|
||||
|
||||
GameObject go = Instantiate(selectedItem, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
|
||||
go.transform.SetParent(itemParent, false);
|
||||
|
||||
setItemText = go.GetComponentInChildren<TextMeshProUGUI>();
|
||||
textHelper = contexItems[i].itemText;
|
||||
setItemText.text = textHelper;
|
||||
|
||||
Transform goImage;
|
||||
goImage = go.gameObject.transform.Find("Icon");
|
||||
setItemImage = goImage.GetComponent<Image>();
|
||||
imageHelper = contexItems[i].itemIcon;
|
||||
setItemImage.sprite = imageHelper;
|
||||
|
||||
if (imageHelper == null)
|
||||
setItemImage.color = new Color(0, 0, 0, 0);
|
||||
|
||||
Button itemButton;
|
||||
itemButton = go.GetComponent<Button>();
|
||||
itemButton.onClick.AddListener(contexItems[i].onClick.Invoke);
|
||||
itemButton.onClick.AddListener(CloseOnClick);
|
||||
StartCoroutine(ExecuteAfterTime(0.01f));
|
||||
}
|
||||
|
||||
contextManager.SetContextMenuPosition();
|
||||
contextAnimator.Play("Menu In");
|
||||
contextManager.isOn = true;
|
||||
contextManager.SetContextMenuPosition();
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator ExecuteAfterTime(float time)
|
||||
{
|
||||
yield return new WaitForSeconds(time);
|
||||
itemParent.gameObject.SetActive(false);
|
||||
itemParent.gameObject.SetActive(true);
|
||||
StopCoroutine(ExecuteAfterTime(0.01f));
|
||||
StopCoroutine("ExecuteAfterTime");
|
||||
}
|
||||
|
||||
public void CloseOnClick()
|
||||
{
|
||||
contextAnimator.Play("Menu Out");
|
||||
contextManager.isOn = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f93b797e33a2590489a95b86d8490887
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: cceded5b1d0834e48849fb152ac8e53d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuContentMobile.cs
|
||||
uploadId: 628721
|
||||
196
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuManager.cs
Normal file
196
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuManager.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using UnityEngine;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class ContextMenuManager : MonoBehaviour
|
||||
{
|
||||
// Resources
|
||||
public Canvas mainCanvas;
|
||||
public Camera targetCamera;
|
||||
public GameObject contextContent;
|
||||
public Animator contextAnimator;
|
||||
public GameObject contextButton;
|
||||
public GameObject contextSeparator;
|
||||
public GameObject contextSubMenu;
|
||||
|
||||
// Settings
|
||||
[SerializeField] private bool debugMode;
|
||||
public bool autoSubMenuPosition = true;
|
||||
public SubMenuBehaviour subMenuBehaviour;
|
||||
public CameraSource cameraSource = CameraSource.Main;
|
||||
|
||||
// Bounds
|
||||
public CursorBoundHorizontal horizontalBound;
|
||||
public CursorBoundVertical verticalBound;
|
||||
[Range(-50, 50)] public int vBorderTop = -10;
|
||||
[Range(-50, 50)] public int vBorderBottom = 10;
|
||||
[Range(-50, 50)] public int hBorderLeft = 15;
|
||||
[Range(-50, 50)] public int hBorderRight = -15;
|
||||
|
||||
Vector2 uiPos;
|
||||
Vector3 cursorPos;
|
||||
Vector3 contentPos = new Vector3(0, 0, 0);
|
||||
Vector3 contextVelocity = Vector3.zero;
|
||||
|
||||
RectTransform contextRect;
|
||||
RectTransform contentRect;
|
||||
|
||||
[HideInInspector] public bool isOn;
|
||||
|
||||
public enum CameraSource { Main, Custom }
|
||||
public enum SubMenuBehaviour { Hover, Click }
|
||||
public enum CursorBoundHorizontal { Left, Right }
|
||||
public enum CursorBoundVertical { Bottom, Top }
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (mainCanvas == null) { mainCanvas = gameObject.GetComponentInParent<Canvas>(); }
|
||||
if (contextAnimator == null) { contextAnimator = gameObject.GetComponent<Animator>(); }
|
||||
if (cameraSource == CameraSource.Main) { targetCamera = Camera.main; }
|
||||
|
||||
contextRect = gameObject.GetComponent<RectTransform>();
|
||||
contentRect = contextContent.GetComponent<RectTransform>();
|
||||
contentPos = new Vector3(vBorderTop, hBorderLeft, 0);
|
||||
gameObject.transform.SetAsLastSibling();
|
||||
#if UNITY_2022_1_OR_NEWER
|
||||
subMenuBehaviour = SubMenuBehaviour.Click;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void CheckForBound()
|
||||
{
|
||||
if (uiPos.x <= -100)
|
||||
{
|
||||
horizontalBound = CursorBoundHorizontal.Left;
|
||||
contentPos = new Vector3(hBorderLeft, contentPos.y, 0); contentRect.pivot = new Vector2(0f, contentRect.pivot.y);
|
||||
}
|
||||
|
||||
else if (uiPos.x >= 100)
|
||||
{
|
||||
horizontalBound = CursorBoundHorizontal.Right;
|
||||
contentPos = new Vector3(hBorderRight, contentPos.y, 0); contentRect.pivot = new Vector2(1f, contentRect.pivot.y);
|
||||
}
|
||||
|
||||
if (uiPos.y <= -75)
|
||||
{
|
||||
verticalBound = CursorBoundVertical.Bottom;
|
||||
contentPos = new Vector3(contentPos.x, vBorderBottom, 0); contentRect.pivot = new Vector2(contentRect.pivot.x, 0f);
|
||||
}
|
||||
|
||||
else if (uiPos.y >= 75)
|
||||
{
|
||||
verticalBound = CursorBoundVertical.Top;
|
||||
contentPos = new Vector3(contentPos.x, vBorderTop, 0); contentRect.pivot = new Vector2(contentRect.pivot.x, 1f);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetContextMenuPosition()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
cursorPos = Input.mousePosition;
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
cursorPos = Mouse.current.position.ReadValue();
|
||||
#endif
|
||||
|
||||
if (mainCanvas.renderMode == RenderMode.ScreenSpaceCamera || mainCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
contextRect.position = targetCamera.ScreenToWorldPoint(cursorPos);
|
||||
contextRect.localPosition = new Vector3(contextRect.localPosition.x, contextRect.localPosition.y, 0);
|
||||
contextContent.transform.localPosition = Vector3.SmoothDamp(contextContent.transform.localPosition, contentPos, ref contextVelocity, 0);
|
||||
}
|
||||
|
||||
else if (mainCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
contextRect.position = cursorPos;
|
||||
contextContent.transform.position = new Vector3(cursorPos.x + contentPos.x, cursorPos.y + contentPos.y, 0);
|
||||
}
|
||||
|
||||
uiPos = contextRect.anchoredPosition;
|
||||
CheckForBound();
|
||||
|
||||
if (debugMode == true)
|
||||
{
|
||||
PrintDebug();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFixedPosition()
|
||||
{
|
||||
#if ENABLE_LEGACY_INPUT_MANAGER
|
||||
cursorPos = Input.mousePosition;
|
||||
#elif ENABLE_INPUT_SYSTEM
|
||||
cursorPos = Mouse.current.position.ReadValue();
|
||||
#endif
|
||||
SetContextMenuPosition();
|
||||
|
||||
if (mainCanvas.renderMode == RenderMode.ScreenSpaceCamera || mainCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
contextRect.position = targetCamera.ScreenToWorldPoint(cursorPos);
|
||||
contextRect.localPosition = new Vector3(contextRect.localPosition.x, contextRect.localPosition.y, 0);
|
||||
contextContent.transform.localPosition = Vector3.SmoothDamp(contextContent.transform.localPosition, contentPos, ref contextVelocity, 0);
|
||||
}
|
||||
|
||||
else if (mainCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
contextRect.position = cursorPos;
|
||||
contextContent.transform.position = new Vector3(cursorPos.x + contentPos.x, cursorPos.y + contentPos.y, 0);
|
||||
}
|
||||
|
||||
uiPos = contextRect.anchoredPosition;
|
||||
CheckForBound();
|
||||
|
||||
if (debugMode == true)
|
||||
{
|
||||
PrintDebug();
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessContextRect()
|
||||
{
|
||||
if (mainCanvas.renderMode == RenderMode.ScreenSpaceCamera || mainCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
contextRect.position = targetCamera.ScreenToWorldPoint(cursorPos);
|
||||
contextRect.localPosition = new Vector3(contextRect.localPosition.x, contextRect.localPosition.y, 0);
|
||||
contextContent.transform.localPosition = Vector3.SmoothDamp(contextContent.transform.localPosition, contentPos, ref contextVelocity, 0);
|
||||
}
|
||||
|
||||
else if (mainCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
contextRect.position = cursorPos;
|
||||
contextContent.transform.position = new Vector3(cursorPos.x + contentPos.x, cursorPos.y + contentPos.y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintDebug()
|
||||
{
|
||||
Debug.Log("<b>[Context Menu]</b> UI Pos: " + uiPos + ", H: " + horizontalBound + ", V: " + verticalBound, this);
|
||||
}
|
||||
|
||||
public void Open()
|
||||
{
|
||||
contextAnimator.Play("Menu In");
|
||||
isOn = true;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
contextAnimator.Play("Menu Out");
|
||||
isOn = false;
|
||||
}
|
||||
|
||||
public void OpenInFixedPosition()
|
||||
{
|
||||
SetFixedPosition();
|
||||
Open();
|
||||
}
|
||||
|
||||
#region Obsolote
|
||||
public void OpenContextMenu() { Open(); }
|
||||
public void CloseOnClick() { Close(); }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9021c5c7bd04ec245bf10c985a37c848
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: cceded5b1d0834e48849fb152ac8e53d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuManager.cs
|
||||
uploadId: 628721
|
||||
@@ -0,0 +1,123 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CustomEditor(typeof(ContextMenuManager))]
|
||||
public class ContextMenuManagerEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private ContextMenuManager cmTarget;
|
||||
private UIManagerContextMenu tempUIM;
|
||||
private int currentTab;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
cmTarget = (ContextMenuManager)target;
|
||||
|
||||
try { tempUIM = cmTarget.GetComponent<UIManagerContextMenu>(); }
|
||||
catch { }
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "CM Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[3];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Resources");
|
||||
toolbarTabs[2] = new GUIContent("Settings");
|
||||
|
||||
currentTab = MUIPEditorHandler.DrawTabs(currentTab, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
currentTab = 0;
|
||||
if (GUILayout.Button(new GUIContent("Resources", "Resources"), customSkin.FindStyle("Tab Resources")))
|
||||
currentTab = 1;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
currentTab = 2;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var contextContent = serializedObject.FindProperty("contextContent");
|
||||
var contextAnimator = serializedObject.FindProperty("contextAnimator");
|
||||
var contextButton = serializedObject.FindProperty("contextButton");
|
||||
var contextSeparator = serializedObject.FindProperty("contextSeparator");
|
||||
var contextSubMenu = serializedObject.FindProperty("contextSubMenu");
|
||||
var autoSubMenuPosition = serializedObject.FindProperty("autoSubMenuPosition");
|
||||
var subMenuBehaviour = serializedObject.FindProperty("subMenuBehaviour");
|
||||
var vBorderTop = serializedObject.FindProperty("vBorderTop");
|
||||
var vBorderBottom = serializedObject.FindProperty("vBorderBottom");
|
||||
var hBorderLeft = serializedObject.FindProperty("hBorderLeft");
|
||||
var hBorderRight = serializedObject.FindProperty("hBorderRight");
|
||||
var cameraSource = serializedObject.FindProperty("cameraSource");
|
||||
var targetCamera = serializedObject.FindProperty("targetCamera");
|
||||
var debugMode = serializedObject.FindProperty("debugMode");
|
||||
|
||||
switch (currentTab)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Content Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(vBorderTop, customSkin, "Vertical Top");
|
||||
MUIPEditorHandler.DrawProperty(vBorderBottom, customSkin, "Vertical Bottom");
|
||||
MUIPEditorHandler.DrawProperty(hBorderLeft, customSkin, "Horizontal Left");
|
||||
MUIPEditorHandler.DrawProperty(hBorderRight, customSkin, "Horizontal Right");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Core Header", 6);
|
||||
MUIPEditorHandler.DrawProperty(contextContent, customSkin, "Context Content");
|
||||
MUIPEditorHandler.DrawProperty(contextAnimator, customSkin, "Context Animator");
|
||||
MUIPEditorHandler.DrawProperty(contextButton, customSkin, "Button Preset");
|
||||
MUIPEditorHandler.DrawProperty(contextSeparator, customSkin, "Seperator Preset");
|
||||
MUIPEditorHandler.DrawProperty(contextSubMenu, customSkin, "Sub Menu Preset");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Options Header", 6);
|
||||
debugMode.boolValue = MUIPEditorHandler.DrawToggle(debugMode.boolValue, customSkin, "Debug Mode");
|
||||
autoSubMenuPosition.boolValue = MUIPEditorHandler.DrawToggle(autoSubMenuPosition.boolValue, customSkin, "Auto Sub Menu Position");
|
||||
MUIPEditorHandler.DrawProperty(subMenuBehaviour, customSkin, "Sub Menu Behaviour");
|
||||
#if UNITY_2022_1_OR_NEWER
|
||||
EditorGUILayout.HelpBox("Due to an issue with the event system, the 'Hover' option will be temporarily disabled in Unity 2022.1.", MessageType.Info);
|
||||
#endif
|
||||
MUIPEditorHandler.DrawProperty(cameraSource, customSkin, "Camera Source");
|
||||
|
||||
if (cmTarget.cameraSource == ContextMenuManager.CameraSource.Custom)
|
||||
MUIPEditorHandler.DrawProperty(targetCamera, customSkin, "Target Camera");
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "UIM Header", 10);
|
||||
|
||||
if (tempUIM != null)
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerConnectedHeader();
|
||||
|
||||
if (GUILayout.Button("Open UI Manager", customSkin.button))
|
||||
EditorApplication.ExecuteMenuItem("Tools/Modern UI Pack/Show UI Manager");
|
||||
|
||||
if (GUILayout.Button("Disable UI Manager Connection", customSkin.button))
|
||||
{
|
||||
if (EditorUtility.DisplayDialog("Modern UI Pack", "Are you sure you want to disable UI Manager connection with the object? " +
|
||||
"This operation cannot be undone.", "Yes", "Cancel"))
|
||||
{
|
||||
try { DestroyImmediate(tempUIM); }
|
||||
catch { Debug.LogError("<b>[Context Menu]</b> Failed to delete UI Manager connection.", this); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (tempUIM == null) { MUIPEditorHandler.DrawUIManagerDisconnectedHeader(); }
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 527585204cee0694b9800f8f51baace0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuManagerEditor.cs
|
||||
uploadId: 628721
|
||||
137
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuSubMenu.cs
Normal file
137
Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuSubMenu.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
public class ContextMenuSubMenu : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
public ContextMenuManager cmManager;
|
||||
public ContextMenuContent cmContent;
|
||||
public Animator subMenuAnimator;
|
||||
public Transform itemParent;
|
||||
public GameObject trigger;
|
||||
[HideInInspector] public int subMenuIndex;
|
||||
|
||||
GameObject selectedItem;
|
||||
Image setItemImage;
|
||||
TextMeshProUGUI setItemText;
|
||||
Sprite imageHelper;
|
||||
string textHelper;
|
||||
RectTransform listParent;
|
||||
|
||||
[HideInInspector] public bool enableFadeOut = true;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (itemParent == null) { Debug.Log("<b>[Context Menu]</b> Item Parent is missing.", this); return; }
|
||||
|
||||
listParent = itemParent.parent.gameObject.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (cmManager.subMenuBehaviour == ContextMenuManager.SubMenuBehaviour.Click)
|
||||
{
|
||||
if (subMenuAnimator.GetCurrentAnimatorStateInfo(0).IsName("Menu In"))
|
||||
{
|
||||
subMenuAnimator.Play("Menu Out");
|
||||
if (trigger != null) { trigger.SetActive(false); }
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
subMenuAnimator.Play("Menu In");
|
||||
if (trigger != null) { trigger.SetActive(true); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
foreach (Transform child in itemParent)
|
||||
Destroy(child.gameObject);
|
||||
|
||||
for (int i = 0; i < cmContent.contexItems[subMenuIndex].subMenuItems.Count; ++i)
|
||||
{
|
||||
bool nulLVariable = false;
|
||||
|
||||
if (cmContent.contexItems[subMenuIndex].subMenuItems[i].contextItemType == ContextMenuContent.ContextItemType.Button && cmManager.contextButton != null)
|
||||
selectedItem = cmManager.contextButton;
|
||||
else if (cmContent.contexItems[subMenuIndex].subMenuItems[i].contextItemType == ContextMenuContent.ContextItemType.Separator && cmManager.contextSeparator != null)
|
||||
selectedItem = cmManager.contextSeparator;
|
||||
else
|
||||
{
|
||||
Debug.LogError("<b>[Context Menu]</b> At least one of the item presets is missing. " +
|
||||
"You can assign a new variable in Resources (Context Menu) tab. All default presets can be found in " +
|
||||
"<b>Modern UI Pack > Prefabs > Context Menu</b> folder.", this);
|
||||
nulLVariable = true;
|
||||
}
|
||||
|
||||
if (nulLVariable == false)
|
||||
{
|
||||
GameObject go = Instantiate(selectedItem, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
|
||||
go.transform.SetParent(itemParent, false);
|
||||
|
||||
if (cmContent.contexItems[subMenuIndex].subMenuItems[i].contextItemType == ContextMenuContent.ContextItemType.Button)
|
||||
{
|
||||
setItemText = go.GetComponentInChildren<TextMeshProUGUI>();
|
||||
textHelper = cmContent.contexItems[subMenuIndex].subMenuItems[i].itemText;
|
||||
setItemText.text = textHelper;
|
||||
|
||||
Transform goImage = go.gameObject.transform.Find("Icon");
|
||||
setItemImage = goImage.GetComponent<Image>();
|
||||
imageHelper = cmContent.contexItems[subMenuIndex].subMenuItems[i].itemIcon;
|
||||
setItemImage.sprite = imageHelper;
|
||||
|
||||
if (imageHelper == null)
|
||||
setItemImage.color = new Color(0, 0, 0, 0);
|
||||
|
||||
Button itemButton = go.GetComponent<Button>();
|
||||
itemButton.onClick.AddListener(cmContent.contexItems[subMenuIndex].subMenuItems[i].onClick.Invoke);
|
||||
itemButton.onClick.AddListener(CloseOnClick);
|
||||
StartCoroutine(ExecuteAfterTime(0.01f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cmManager.autoSubMenuPosition == true)
|
||||
{
|
||||
if (cmManager.horizontalBound == ContextMenuManager.CursorBoundHorizontal.Left) { listParent.pivot = new Vector2(0f, listParent.pivot.y); }
|
||||
else if (cmManager.horizontalBound == ContextMenuManager.CursorBoundHorizontal.Right) { listParent.pivot = new Vector2(1f, listParent.pivot.y); }
|
||||
|
||||
if (cmManager.verticalBound == ContextMenuManager.CursorBoundVertical.Top) { listParent.pivot = new Vector2(listParent.pivot.x, 0f); }
|
||||
else if (cmManager.verticalBound == ContextMenuManager.CursorBoundVertical.Bottom) { listParent.pivot = new Vector2(listParent.pivot.x, 1f); }
|
||||
}
|
||||
|
||||
if (cmManager.subMenuBehaviour == ContextMenuManager.SubMenuBehaviour.Hover)
|
||||
subMenuAnimator.Play("Menu In");
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
#if !UNITY_2022_1_OR_NEWER
|
||||
if (cmManager.subMenuBehaviour == ContextMenuManager.SubMenuBehaviour.Hover && !subMenuAnimator.GetCurrentAnimatorStateInfo(0).IsName("Start"))
|
||||
subMenuAnimator.Play("Menu Out");
|
||||
#endif
|
||||
}
|
||||
|
||||
IEnumerator ExecuteAfterTime(float time)
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(time);
|
||||
itemParent.gameObject.SetActive(false);
|
||||
itemParent.gameObject.SetActive(true);
|
||||
StopCoroutine(ExecuteAfterTime(0.01f));
|
||||
StopCoroutine("ExecuteAfterTime");
|
||||
}
|
||||
|
||||
public void CloseOnClick()
|
||||
{
|
||||
cmManager.contextAnimator.Play("Menu Out");
|
||||
cmManager.isOn = false;
|
||||
trigger.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d93fb32bb7e8384d863c98e08445c2d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Context Menu/ContextMenuSubMenu.cs
|
||||
uploadId: 628721
|
||||
Reference in New Issue
Block a user