浏览代码

OnPlayerInputAction

/feature-new-input-system
Thomas ICHÉ 3 年前
当前提交
56bf0966
共有 7 个文件被更改,包括 178 次插入1 次删除
  1. 19
      Runtime/LevelScripting/Events/OnInputActionEvent.cs
  2. 73
      Editor/PropertyDrawers/PlayerInputActionPropertyDrawer.cs
  3. 11
      Editor/PropertyDrawers/PlayerInputActionPropertyDrawer.cs.meta
  4. 14
      Runtime/Attributes/PlayerInputActionAttribute.cs
  5. 11
      Runtime/Attributes/PlayerInputActionAttribute.cs.meta
  6. 40
      Runtime/LevelScripting/Events/OnPlayerInputActionEvent.cs
  7. 11
      Runtime/LevelScripting/Events/OnPlayerInputActionEvent.cs.meta

19
Runtime/LevelScripting/Events/OnInputActionEvent.cs


public class OnInputActionEvent : EventBase
{
#if ENABLE_INPUT_SYSTEM
public InputAction inputAction;
[SerializeField]
InputAction inputAction;
public Callable[] onButtonDown;
private void OnEnable()
{
inputAction.performed += InputAction_performed;
}
private void OnDisable()
{
inputAction.performed -= InputAction_performed;
}
private void InputAction_performed(InputAction.CallbackContext obj)
{
Callable.Call(onButtonDown, gameObject);
}
#endif
}
}

73
Editor/PropertyDrawers/PlayerInputActionPropertyDrawer.cs


#if ENABLE_INPUT_SYSTEM
using UnityEngine;
using UnityEditor;
using UnityEngine.InputSystem;
using System;
using System.Reflection;
namespace GameplayIngredients.Editor
{
[CustomPropertyDrawer(typeof(PlayerInputActionAttribute))]
public class PlayerInputActionPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
PlayerInputActionAttribute pia = attribute as PlayerInputActionAttribute;
var obj = property.serializedObject.targetObject;
var fi = obj.GetType().GetField(pia.name, BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.Instance);
if(fi != null)
{
var playerInput = fi.GetValue(obj) as PlayerInput;
if(playerInput != null)
{
if (playerInput.actions != null)
{
var field = obj.GetType().GetField(property.name, BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.Instance);
var fieldValue = field.GetValue(property.serializedObject.targetObject);
var actionMap = playerInput.actions.FindActionMap(playerInput.defaultActionMap);
int selected = -1;
string[] names = new string[actionMap.actions.Count];
for(int i = 0; i < actionMap.actions.Count; i ++)
{
if (fieldValue == actionMap.actions[i])
selected = i;
names[i] = actionMap.actions[i].name;
}
selected = EditorGUI.Popup(position, ObjectNames.NicifyVariableName(property.name), selected, names);
if(GUI.changed)
{
Undo.RecordObject(property.serializedObject.targetObject, "Set Action Map");
property.serializedObject.Update();
field.SetValue(property.serializedObject.targetObject, actionMap.actions[selected]);
EditorUtility.SetDirty(property.serializedObject.targetObject);
property.serializedObject.ApplyModifiedProperties();
}
}
else
EditorGUI.HelpBox(position, $"Target Player Input had no actions configured", MessageType.Warning);
}
else
EditorGUI.HelpBox(position, $"Property {pia.name} is null or not of PlayerInput type", MessageType.Error);
}
else
EditorGUI.HelpBox(position, $"Could not find field {pia.name}", MessageType.Error);
}
}
}
#endif

11
Editor/PropertyDrawers/PlayerInputActionPropertyDrawer.cs.meta


fileFormatVersion: 2
guid: 857cf4d1897170945a9897f28a323452
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

14
Runtime/Attributes/PlayerInputActionAttribute.cs


using UnityEngine;
namespace GameplayIngredients
{
public class PlayerInputActionAttribute : PropertyAttribute
{
public readonly string name;
public PlayerInputActionAttribute(string propertyName)
{
this.name = propertyName;
}
}
}

11
Runtime/Attributes/PlayerInputActionAttribute.cs.meta


fileFormatVersion: 2
guid: c57940c734ebd694eaf5a668fd2b344a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

40
Runtime/LevelScripting/Events/OnPlayerInputActionEvent.cs


using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace GameplayIngredients.Events
{
#if !ENABLE_INPUT_SYSTEM
[WarnDisabledModule("New Input System")]
#endif
[AddComponentMenu(ComponentMenu.eventsPath + "On Player Input Action Event (New Input System)")]
public class OnPlayerInputActionEvent : EventBase
{
#if ENABLE_INPUT_SYSTEM
[SerializeField]
public PlayerInput playerInput;
[SerializeField, PlayerInputAction("playerInput")]
InputAction inputAction;
public Callable[] onButtonDown;
private void OnEnable()
{
inputAction.performed += InputAction_performed;
}
private void OnDisable()
{
inputAction.performed -= InputAction_performed;
}
private void InputAction_performed(InputAction.CallbackContext obj)
{
Callable.Call(onButtonDown, gameObject);
}
#endif
}
}

11
Runtime/LevelScripting/Events/OnPlayerInputActionEvent.cs.meta


fileFormatVersion: 2
guid: 8e4443e2a5662e440999cb1f3de312a8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存