Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

79 行
2.2 KiB

using GameplayIngredients.Managers;
using NaughtyAttributes;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
#endif
namespace GameplayIngredients.Events
{
#if !ENABLE_INPUT_SYSTEM
[WarnDisabledModule("New Input System")]
#endif
[AddComponentMenu(ComponentMenu.eventsPath + "On Input Direct Event (New Input System)")]
public class OnInputDirectEvent : EventBase
{
[SerializeField]
Device device = Device.Keyboard;
[SerializeField, ShowIf("IsMouse")]
MouseButton mouseButton = MouseButton.Left;
[SerializeField, ShowIf("IsGamepad")]
GamepadButton gamepadButton = GamepadButton.A;
[SerializeField, ShowIf("IsKeyboard")]
Key keyboardKey = Key.Space;
public Callable[] onPressed;
public Callable[] onReleased;
bool IsMouse() => device == Device.Mouse;
bool IsGamepad() => device == Device.Gamepad;
bool IsKeyboard() => device == Device.Keyboard;
ButtonControl button;
#if ENABLE_INPUT_SYSTEM
private void OnEnable()
{
button = GetButton(device);
Manager.Get<SingleUpdateManager>().Register(SingleUpdate);
}
private void OnDisable()
{
Manager.Get<SingleUpdateManager>().Remove(SingleUpdate);
}
private void SingleUpdate()
{
if (button == null)
return;
if (button.wasPressedThisFrame)
Callable.Call(onPressed, null);
if (button.wasReleasedThisFrame)
Callable.Call(onReleased, null);
}
ButtonControl GetButton(Device d)
{
switch (d)
{
case Device.Gamepad:
return InputSystemUtility.GetButton(gamepadButton);
case Device.Keyboard:
return InputSystemUtility.GetButton(keyboardKey);
case Device.Mouse:
return InputSystemUtility.GetButton(mouseButton);
default:
throw new System.NotImplementedException();
}
}
#endif
}
}