浏览代码

found fix for todo #2

/main
kgc00 4 年前
当前提交
7aa245f6
共有 6 个文件被更改,包括 65 次插入35 次删除
  1. 73
      UOP1_Project/Assets/Scripts/Menu/MenuInput.cs
  2. 3
      UOP1_Project/Assets/Scripts/Menu/README.md
  3. 7
      UOP1_Project/Assets/Scripts/Menu/SelectableUIElement.cs
  4. 2
      UOP1_Project/Packages/packages-lock.json
  5. 8
      UOP1_Project/Assets/Prefabs/Menu.meta
  6. 7
      UOP1_Project/Assets/Prefabs/Menu/debug menu controls.prefab.meta

73
UOP1_Project/Assets/Scripts/Menu/MenuInput.cs


using UnityEngine;
using System;
using UnityEngine;
private GameObject _currentSelection;
private GameObject _mouseSelection;
[SerializeField] private GameObject _currentSelection;
[SerializeField] private GameObject _mouseSelection;
private void OnEnable()
{
_inputReader.Menu.MouseMoveMenuEvent += HandleMoveCursor;

_inputReader.Menu.MoveSelectionMenuEvent -= HandleMoveSelection;
}
/// <summary>
/// Fired by keyboard and gamepad inputs. current selected UI element will be the ui Element that was selected
/// when the event was fired. The _currentSelection is updated later on, after the EventSystem moves to the
/// desired UI element, the UI element will call into UpdateSelection()
/// </summary>
// _currentSelection will be the start UI element, destination is taken care of by the EventSystem for us
Cursor.visible = false;
DisableCursor();
// Handle case where no UI element is selected because mouse left selectable bounds
if (EventSystem.current.currentSelectedGameObject == null)
EventSystem.current.SetSelectedGameObject(_currentSelection);
// occurs when mouse is on top of some button, and we hit a gamepad or keyboard key to change the selection
var mouseIsOverSelectionStart = _mouseSelection == _currentSelection;

// fire pointer exit event because we don't want the button to be in the 'highlighted' state
ExecuteEvents.Execute(EventSystem.current.currentSelectedGameObject, new PointerEventData(EventSystem.current),
ExecuteEvents.Execute(EventSystem.current.currentSelectedGameObject,
new PointerEventData(EventSystem.current),
}
// if mouse has moved from a button to empty space we store its last interactable
// if we receive a move command, we use that stored position to recenter focus before the move is executed
if (EventSystem.current.currentSelectedGameObject == null)
EventSystem.current.SetSelectedGameObject(_mouseSelection);
}
// update the selection
EventSystem.current.SetSelectedGameObject(_currentSelection);
private void DisableCursor()
{
Cursor.visible = false;
// the event we fired earlier clears _mouseSelection, reset it here because our cursor is still over the button
_mouseSelection = _currentSelection;
}
EnableCursor();
}
if (_mouseSelection != null)
{
EventSystem.current.SetSelectedGameObject(_mouseSelection);
}
private void EnableCursor()
{
private void StoreSelection(GameObject uiElement)
public void HandleMouseEnter(GameObject uiElement)
_mouseSelection = uiElement;
_currentSelection = uiElement;
_mouseSelection = uiElement;
public void HandleMouseEnter(GameObject uiElement)
public void HandleMouseExit(GameObject uiElement)
StoreSelection(uiElement);
if (EventSystem.current.currentSelectedGameObject != uiElement) return;
// deselect UI element if mouse moves away from it
_mouseSelection = null;
EventSystem.current.SetSelectedGameObject(null);
public void HandleMouseExit(GameObject uiElement)
/// <summary>
/// Fired by gamepad or keyboard navigation inputs
/// </summary>
/// <param name="uiElement"></param>
public void UpdateSelection(GameObject uiElement) => _currentSelection = uiElement;
private void OnGUI()
// deselect UI element if mouse moves away from it
if (EventSystem.current.currentSelectedGameObject == uiElement)
{
EventSystem.current.SetSelectedGameObject(null);
}
GUILayout.Box($"_currentSelection: {(_currentSelection != null ? _currentSelection.name : "null")}");
GUILayout.Box($"_mouseSelection: {(_mouseSelection != null ? _mouseSelection.name : "null")}");
}
}

3
UOP1_Project/Assets/Scripts/Menu/README.md


- they find the MenuInput component by checking the root transform for children with the MenuInput class (can be refactored later)
##TODO
- mouse click activates the currently selected UI element (even if the mouse is not over it)
- if the keyboard or gamepad navigates the selection away from a UI element which the mouse is over, you must move the mouse out of the game object's bounds and re-enter to select it again.
- mouse click activates the currently selected UI element (even if the mouse is not over it)

7
UOP1_Project/Assets/Scripts/Menu/SelectableUIElement.cs


using UnityEngine;
using UnityEngine.EventSystems;
public class SelectableUIElement : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
public class SelectableUIElement : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler
{
private MenuInput _menuInput;

public void OnPointerExit(PointerEventData eventData)
{
_menuInput.HandleMouseExit(gameObject);
}
public void OnSelect(BaseEventData eventData)
{
_menuInput.UpdateSelection(gameObject);
}
}

2
UOP1_Project/Packages/packages-lock.json


"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.18",
"version": "1.1.16",
"depth": 1,
"source": "registry",
"dependencies": {

8
UOP1_Project/Assets/Prefabs/Menu.meta


fileFormatVersion: 2
guid: 2511b99d5e527884599f3f6931c9ca20
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
UOP1_Project/Assets/Prefabs/Menu/debug menu controls.prefab.meta


fileFormatVersion: 2
guid: b32688bd582a7ad479d51d1547d5b22f
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存