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

91 行
2.8 KiB

using System;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace GameplayIngredients.Events
{
#if ENABLE_INPUT_SYSTEM
[Serializable]
public struct PlayerInputAction
{
public PlayerInput playerInput;
public string path;
public InputAction action
{
get
{
if (playerInput == null)
return null;
else
{
if(m_CachedPath != path || m_CachedInputAction == null)
{
string[] split = path.Split(pathSeparator);
if(split.Length != 2)
{
Debug.LogWarning($"Invalid Path '{path}'", playerInput);
return null;
}
int mapIdx = playerInput.actions.actionMaps.IndexOf(o => o.name == split[0]);
if(mapIdx == -1) // not found
{
Debug.LogWarning($"Could not find action map '{split[0]}' in asset {playerInput.name}", playerInput);
return null;
}
var map = playerInput.actions.actionMaps[mapIdx];
int actionIdx = map.actions.IndexOf(o => o.name == split[1]);
if(actionIdx == -1) // not found
{
Debug.LogWarning($"Could not find action '{split[1]}' of map '{map.name}' in asset {playerInput.name}", playerInput);
return null;
}
m_CachedInputAction = map.actions[actionIdx];
m_CachedPath = path;
}
return m_CachedInputAction;
}
}
}
public const char pathSeparator = '/';
string m_CachedPath;
InputAction m_CachedInputAction;
}
#endif
#if !ENABLE_INPUT_SYSTEM
[WarnDisabledModule("New Input System")]
#endif
[AddComponentMenu(ComponentMenu.eventsPath + "On Input Asset Action Event (New Input System)")]
public class OnPlayerInputActionEvent : EventBase
{
#if ENABLE_INPUT_SYSTEM
[SerializeField]
PlayerInputAction inputAction;
[Header("Action")]
public Callable[] onButtonDown;
private void OnEnable()
{
InputActionManager.Request(inputAction.action, InputAction_performed);
}
private void OnDisable()
{
InputActionManager.Release(inputAction.action, InputAction_performed);
}
private void InputAction_performed(InputAction.CallbackContext obj)
{
Callable.Call(onButtonDown, gameObject);
}
#endif
}
}