Working on selection

This commit is contained in:
Hubert Mattusch 2023-05-19 20:58:53 +02:00
parent 7aecbeb240
commit 7bf4512c43
12 changed files with 255 additions and 13 deletions

View File

@ -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<TextAsset>(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();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0501eedeaf1d4dcf8c4d0f1b7a0c1761
timeCreated: 1684519404

View File

@ -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; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d9b5ca8863c240f792232d6e276d8d56
timeCreated: 1684520970

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ffc2c67f6a714d441aa9dfd425e5937e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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<CustomNavigationButton>());
string relativePath = AssetDatabase.GetAssetPath(sourceScriptAsset);
Object.DestroyImmediate(go);
var chosenTextAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(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();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b09e744f7ec6704c95ec273584a964f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 162f4693585f414b85f0a4210a10a585
timeCreated: 1684521873

View File

@ -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
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d7c723fd6c577334eb3ec43fa26c4a23
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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);
}
}
}
}