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