Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/InputSystem/BITInputSystem.cs

131 lines
4.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.InputSystem;
#endif
namespace BITKit
{
public class BITInputSystem
{
[RuntimeInitializeOnLoadMethod]
static void Reload()
{
AllowInput = new();
}
public static ValidHandle AllowInput = new();
}
#if UNITY_EDITOR
public class BITInputSystemEditor : EditorWindow
{
private Toggle allowInput;
private Toggle allowInputLog;
private InputAction _inputAction;
private ObjectField inputActionField;
private Label nameLabel;
private Label valueLabel;
[MenuItem("Tools/InputSystemEditor")]
private static void Open()
{
GetWindow<BITInputSystemEditor>().Show();
}
private void OnEnable()
{
rootVisualElement.style.paddingLeft =
rootVisualElement.style.paddingTop =
rootVisualElement.style.paddingRight =
rootVisualElement.style.paddingBottom = 16;
rootVisualElement.Add(allowInput =new Toggle());
allowInput.text = "Enable Input";
allowInput.SetEnabled(false);
allowInput.style.opacity = 1;
allowInputLog = rootVisualElement.Create<Toggle>();
allowInputLog.text = "Enable Input Log";
inputActionField = rootVisualElement.Create<ObjectField>();
inputActionField.label = "Debug InputAction";
inputActionField.RegisterValueChangedCallback(NewValue);
inputActionField.objectType = typeof(InputActionReference);
var hContainer = rootVisualElement.Create<VisualElement>();
hContainer.style.flexDirection = FlexDirection.Row;
nameLabel = hContainer.Create<Label>();
valueLabel = hContainer.Create<Label>();
valueLabel.style.paddingLeft = 8;
nameLabel.text = "Wait";
valueLabel.text ="Input";
}
private void OnDisable()
{
_inputAction?.Dispose();
}
private void NewValue(ChangeEvent<Object> evt)
{
_inputAction?.Dispose();
_inputAction = null;
if (evt.newValue is not InputActionReference inputActionReference) return;
_inputAction = inputActionReference.action.Clone();
_inputAction.Enable();
_inputAction.RegisterCallback(x =>
{
if (allowInputLog.value)
{
Debug.Log(x);
}
});
}
private void Update()
{
allowInput.value = BITInputSystem.AllowInput;
if (_inputAction is null)
{
nameLabel.text = "Wait";
valueLabel.text = "Input";
return;
}
var nameBuilder = new System.Text.StringBuilder();
var valueBuilder = new System.Text.StringBuilder();
nameBuilder.AppendLine(nameof(InputAction.enabled));
valueBuilder.AppendLine(_inputAction.enabled.ToString());
nameBuilder.AppendLine(nameof(InputAction.IsPressed));
valueBuilder.AppendLine(_inputAction.IsPressed().ToString());
nameBuilder.AppendLine(nameof(InputAction.ReadValue));
valueBuilder.AppendLine(_inputAction.ReadValue<float>().ToString());
nameBuilder.AppendLine(nameof(InputAction.ReadValueAsObject));
valueBuilder.AppendLine(_inputAction.ReadValueAsObject()?.ToString());
nameBuilder.AppendLine(nameof(InputAction.triggered));
valueBuilder.AppendLine(_inputAction.triggered.ToString());
nameBuilder.AppendLine(nameof(InputAction.phase));
valueBuilder.AppendLine(_inputAction.phase.ToString());
nameLabel.text = nameBuilder.ToString();
valueLabel.text = valueBuilder.ToString();
}
}
#endif
}