这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

89 行
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;
public class InputReader : MonoBehaviour, GameInput.IGameplayActions
{
public UnityAction jumpEvent;
public UnityAction jumpCanceledEvent;
public UnityAction attackEvent;
public UnityAction interactEvent;
public UnityAction extraActionEvent;
public UnityAction pauseEvent;
public UnityAction<Vector2> moveEvent;
public UnityAction<Vector2> cameraMoveEvent;
GameInput gameInput;
private void OnEnable()
{
if(gameInput == null)
{
gameInput = new GameInput();
gameInput.Gameplay.SetCallbacks(this);
}
gameInput.Gameplay.Enable();
}
private void OnDisable()
{
gameInput.Gameplay.Disable();
}
public void OnAttack(InputAction.CallbackContext context)
{
if(attackEvent != null
&& context.phase == InputActionPhase.Started)
attackEvent.Invoke();
}
public void OnExtraAction(InputAction.CallbackContext context)
{
if(extraActionEvent != null
&& context.phase == InputActionPhase.Started)
extraActionEvent.Invoke();
}
public void OnInteract(InputAction.CallbackContext context)
{
if(interactEvent != null
&& context.phase == InputActionPhase.Started)
interactEvent.Invoke();
}
public void OnJump(InputAction.CallbackContext context)
{
if(jumpEvent != null
&& context.phase == InputActionPhase.Started)
jumpEvent.Invoke();
if(jumpCanceledEvent != null
&& context.phase == InputActionPhase.Canceled)
jumpCanceledEvent.Invoke();
}
public void OnMove(InputAction.CallbackContext context)
{
if(moveEvent != null)
{
moveEvent.Invoke(context.ReadValue<Vector2>());
}
}
public void OnPause(InputAction.CallbackContext context)
{
if(pauseEvent != null
&& context.phase == InputActionPhase.Started)
pauseEvent.Invoke();
}
public void OnRotateCamera(InputAction.CallbackContext context)
{
if(cameraMoveEvent != null)
{
cameraMoveEvent.Invoke(context.ReadValue<Vector2>());
}
}
}