From 7bf4512c4311605f2b5e32531ca6725cd658feb8 Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Fri, 19 May 2023 20:58:53 +0200 Subject: [PATCH] Working on selection --- Editor/MonoBehaviourExtensions.cs | 46 ++++++++++++++ Editor/MonoBehaviourExtensions.cs.meta | 3 + .../UnityUi/Buttons/CustomNavigationButton.cs | 63 +++++++++++++++++++ .../Buttons/CustomNavigationButton.cs.meta | 3 + NEG/UI/UnityUi/Editor.meta | 8 +++ NEG/UI/UnityUi/Editor/ButtonsExtensions.cs | 42 +++++++++++++ .../UnityUi/Editor/ButtonsExtensions.cs.meta | 11 ++++ .../Editor/CustomNavigationButtonEditor.cs | 37 +++++++++++ .../CustomNavigationButtonEditor.cs.meta | 3 + .../Editor/NEG.UI.UnityUi.Editor.asmdef | 18 ++++++ .../Editor/NEG.UI.UnityUi.Editor.asmdef.meta | 7 +++ NEG/UI/UnityUi/MonoUiInputManger.cs | 27 ++++---- 12 files changed, 255 insertions(+), 13 deletions(-) create mode 100644 Editor/MonoBehaviourExtensions.cs create mode 100644 Editor/MonoBehaviourExtensions.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs create mode 100644 NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs.meta create mode 100644 NEG/UI/UnityUi/Editor.meta create mode 100644 NEG/UI/UnityUi/Editor/ButtonsExtensions.cs create mode 100644 NEG/UI/UnityUi/Editor/ButtonsExtensions.cs.meta create mode 100644 NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs create mode 100644 NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs.meta create mode 100644 NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef create mode 100644 NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef.meta diff --git a/Editor/MonoBehaviourExtensions.cs b/Editor/MonoBehaviourExtensions.cs new file mode 100644 index 0000000..fb4464d --- /dev/null +++ b/Editor/MonoBehaviourExtensions.cs @@ -0,0 +1,46 @@ +using System.IO; +using UnityEngine; +using UnityEditor; + +namespace NEG.Utils.Editor +{ + public static class MonoBehaviourExtensions + { + [MenuItem("CONTEXT/MonoBehaviour/Change Script")] + public static void ChangeScript(MenuCommand command) + { + if (command.context == null) + return; + + var monoBehaviour = command.context as MonoBehaviour; + var monoScript = MonoScript.FromMonoBehaviour(monoBehaviour); + + string scriptPath = AssetDatabase.GetAssetPath(monoScript); + string directoryPath = new FileInfo(scriptPath).Directory?.FullName; + + // Allow the user to select which script to replace with + string newScriptPath = EditorUtility.OpenFilePanel("Select replacement script", directoryPath, "cs"); + + // Don't log anything if they cancelled the window + if (string.IsNullOrEmpty(newScriptPath)) return; + + // Load the selected asset + string relativePath = "Assets\\" + Path.GetRelativePath(Application.dataPath, newScriptPath); + var chosenTextAsset = AssetDatabase.LoadAssetAtPath(relativePath); + + if (chosenTextAsset == null) + { + Debug.LogWarning($"Selected script couldn't be loaded ({relativePath})"); + return; + } + + Undo.RegisterCompleteObjectUndo(command.context, "Changing component script"); + + var so = new SerializedObject(monoBehaviour); + var scriptProperty = so.FindProperty("m_Script"); + so.Update(); + scriptProperty.objectReferenceValue = chosenTextAsset; + so.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/Editor/MonoBehaviourExtensions.cs.meta b/Editor/MonoBehaviourExtensions.cs.meta new file mode 100644 index 0000000..c9d97d5 --- /dev/null +++ b/Editor/MonoBehaviourExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0501eedeaf1d4dcf8c4d0f1b7a0c1761 +timeCreated: 1684519404 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs new file mode 100644 index 0000000..d32918e --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs @@ -0,0 +1,63 @@ +using System; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace NEG.UI.UnityUi.Buttons +{ + public class CustomNavigationButton : Button + { + [SerializeField] private OverridableNavigation upOverride; + [SerializeField] private OverridableNavigation downOverride; + [SerializeField] private OverridableNavigation leftOverride; + [SerializeField] private OverridableNavigation rightOverride; + + + public override void OnMove(AxisEventData eventData) + { + switch (eventData.moveDir) + { + case MoveDirection.Left: + if(TryNavigate(eventData, leftOverride)) + return; + break; + case MoveDirection.Up: + if(TryNavigate(eventData, upOverride)) + return; + break; + case MoveDirection.Right: + if(TryNavigate(eventData, rightOverride)) + return; + break; + case MoveDirection.Down: + if(TryNavigate(eventData, downOverride)) + return; + break; + case MoveDirection.None: + break; + default: + throw new ArgumentOutOfRangeException(); + } + base.OnMove(eventData); + } + + public static bool TryNavigate(BaseEventData eventData, OverridableNavigation overrideNavigation) + { + if(!overrideNavigation.Override) + return false; + + if (overrideNavigation.Selectable == null || !overrideNavigation.Selectable.IsActive()) + return true; + + eventData.selectedObject = overrideNavigation.Selectable.gameObject; + return true; + } + } + + [Serializable] + public class OverridableNavigation + { + [field: SerializeField] public bool Override { get; private set; } + [field: SerializeField] public Selectable Selectable { get; private set; } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs.meta b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs.meta new file mode 100644 index 0000000..fdc8817 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d9b5ca8863c240f792232d6e276d8d56 +timeCreated: 1684520970 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Editor.meta b/NEG/UI/UnityUi/Editor.meta new file mode 100644 index 0000000..3368917 --- /dev/null +++ b/NEG/UI/UnityUi/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ffc2c67f6a714d441aa9dfd425e5937e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs b/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs new file mode 100644 index 0000000..5817242 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs @@ -0,0 +1,42 @@ +using NEG.UI.UnityUi.Buttons; +using System.Collections.Generic; +using System.IO; +using UnityEditor; +using UnityEngine; +using UnityEngine.UI; + +namespace NEG.UI.UnityUi.Editor +{ + public static class ButtonsExtensions + { + [MenuItem("CONTEXT/Button/Change To Navigation Button")] + public static void ChangeScript(MenuCommand command) + { + if (command.context == null) + return; + + var button = command.context as Button; + var go = new GameObject(); + var sourceScriptAsset = MonoScript.FromMonoBehaviour(go.AddComponent()); + string relativePath = AssetDatabase.GetAssetPath(sourceScriptAsset); + Object.DestroyImmediate(go); + + var chosenTextAsset = AssetDatabase.LoadAssetAtPath(relativePath); + + if (chosenTextAsset == null) + { + Debug.LogWarning($"Selected script couldn't be loaded ({relativePath})"); + return; + } + + Undo.RegisterCompleteObjectUndo(command.context, "Changing component script"); + + var so = new SerializedObject(button); + var scriptProperty = so.FindProperty("m_Script"); + so.Update(); + scriptProperty.objectReferenceValue = chosenTextAsset; + so.ApplyModifiedProperties(); + } + + } +} diff --git a/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs.meta b/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs.meta new file mode 100644 index 0000000..824b795 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4b09e744f7ec6704c95ec273584a964f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs new file mode 100644 index 0000000..0557b31 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs @@ -0,0 +1,37 @@ +using UnityEditor; +using UnityEditor.UI; +using UnityEngine.UIElements; + +namespace NEG.UI.UnityUi.Editor +{ + [CustomEditor(typeof(Buttons.CustomNavigationButton), true)] + public class CustomNavigationButtonEditor : ButtonEditor + { + private SerializedProperty upOverrideProperty; + private SerializedProperty downOverrideProperty; + private SerializedProperty leftOverrideProperty; + private SerializedProperty rightOverrideProperty; + + protected override void OnEnable() + { + base.OnEnable(); + upOverrideProperty = serializedObject.FindProperty("upOverride"); + downOverrideProperty = serializedObject.FindProperty("downOverride"); + leftOverrideProperty = serializedObject.FindProperty("leftOverride"); + rightOverrideProperty = serializedObject.FindProperty("rightOverride"); + } + + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + EditorGUILayout.Space(); + + serializedObject.Update(); + EditorGUILayout.PropertyField(upOverrideProperty); + EditorGUILayout.PropertyField(downOverrideProperty); + EditorGUILayout.PropertyField(leftOverrideProperty); + EditorGUILayout.PropertyField(rightOverrideProperty); + serializedObject.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs.meta b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs.meta new file mode 100644 index 0000000..f60cf54 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 162f4693585f414b85f0a4210a10a585 +timeCreated: 1684521873 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef new file mode 100644 index 0000000..e75674d --- /dev/null +++ b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef @@ -0,0 +1,18 @@ +{ + "name": "NEG.UI.UnityUi.Editor", + "rootNamespace": "", + "references": [ + "GUID:e2aaf8effe1c9634d87b2edda6988a6a" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef.meta b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef.meta new file mode 100644 index 0000000..3960400 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d7c723fd6c577334eb3ec43fa26c4a23 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/MonoUiInputManger.cs b/NEG/UI/UnityUi/MonoUiInputManger.cs index cdd3a99..1285f13 100644 --- a/NEG/UI/UnityUi/MonoUiInputManger.cs +++ b/NEG/UI/UnityUi/MonoUiInputManger.cs @@ -62,21 +62,22 @@ namespace NEG.UI.UnityUi { CurrentSelectionSource = SelectionSource.Pointer; Cursor.visible = true; - if (EventSystem.current.currentInputModule != null) + + if (EventSystem.current.currentInputModule == null) + return; + + var module = (InputSystemUIInputModule)EventSystem.current.currentInputModule; + var result = module.GetLastRaycastResult(0); + if(result.gameObject == null) + return; + + var data = new PointerEventData(EventSystem.current); + for (var current = result.gameObject.transform; + current != null; + current = current.parent) { - var module = (InputSystemUIInputModule)EventSystem.current.currentInputModule; - var result = module.GetLastRaycastResult(0); - var data = new PointerEventData(EventSystem.current); - for (var current = result.gameObject.transform; - current != null; - current = current.parent) - { - ExecuteEvents.Execute(current.gameObject, data, ExecuteEvents.pointerEnterHandler); - } - - //EventSystem.current.SetSelectedGameObject(result.gameObject); + ExecuteEvents.Execute(current.gameObject, data, ExecuteEvents.pointerEnterHandler); } - } } } \ No newline at end of file