【Unity学习笔记】New Input System 部分源码和测试用例补充
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
namespace Scenes.TestAction
{
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
public class TestAction : InputTestFixture
{
[Test]
[TestCase(InputActionType.Value)]
[TestCase(InputActionType.Button)]
[TestCase(InputActionType.PassThrough)]
public void Actions_PressOnly(InputActionType type)
{
var gamepad = InputSystem.AddDevice<Gamepad>();
InputSystem.settings.defaultButtonPressPoint = 0.5f;
InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.
var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=0)");
action.Enable();
using (var trace = new InputActionTrace(action))
{
Set(gamepad.leftTrigger, 0.35f);
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));
trace.Clear();
Set(gamepad.leftTrigger, 0.5f);
Assert.That(trace, Performed(action, control: gamepad.leftTrigger, value: 0.5f));
trace.Clear();
Set(gamepad.leftTrigger, 0.6f);
Assert.That(trace, Is.Empty);
Set(gamepad.leftTrigger, 0.4f);
Assert.That(trace, Is.Empty);
Set(gamepad.leftTrigger, 0.3f);
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.3f));
trace.Clear();
Set(gamepad.leftTrigger, 0);
Assert.That(trace, Canceled(action, control: gamepad.leftTrigger, value: 0));
}
}
[Test]
[TestCase(InputActionType.Value)]
[TestCase(InputActionType.Button)]
[TestCase(InputActionType.PassThrough)]
public void Actions_RealeasOnly(InputActionType type)
{
var gamepad = InputSystem.AddDevice<Gamepad>();
InputSystem.settings.defaultButtonPressPoint = 0.5f;
InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.
var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=1)");
action.Enable();
using (var trace = new InputActionTrace(action))
{
Set(gamepad.leftTrigger, 0.35f);
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));
trace.Clear();
Set(gamepad.leftTrigger, 0.5f);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.6f);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.4f);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.3f);
Assert.That(trace,
Performed(action, control: gamepad.leftTrigger, value: 0.3f)
.AndThen(Canceled(action, control: gamepad.leftTrigger, value: 0)));
trace.Clear();
Set(gamepad.leftTrigger, 0.1f);
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.1f));
trace.Clear();
Set(gamepad.leftTrigger, 0);
Assert.That(trace, Canceled(action, control: gamepad.leftTrigger, value: 0));
trace.Clear();
Set(gamepad.leftTrigger, 0.4f);
Set(gamepad.leftTrigger, 0.3f);//onlyRelease 不超过press-threshold只回落release是不会触发的
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.4f));
trace.Clear();
}
}
[Test]
[TestCase(InputActionType.Value)]
[TestCase(InputActionType.Button)]
[TestCase(InputActionType.PassThrough)]
public void Actions_PressAndRelease(InputActionType type)
{
var gamepad = InputSystem.AddDevice<Gamepad>();
InputSystem.settings.defaultButtonPressPoint = 0.5f;
InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.
var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=2)");
action.Enable();
using (var trace = new InputActionTrace(action))
{
Set(gamepad.leftTrigger, 0.35f);
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));
trace.Clear();
Set(gamepad.leftTrigger, 0.5f);
Assert.That(trace, Performed(action, control: gamepad.leftTrigger, value: 0.5f));
trace.Clear();
Set(gamepad.leftTrigger, 0.6f);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.4f);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.3f);
Assert.That(trace,
Performed(action, control: gamepad.leftTrigger, value: 0.3f));
trace.Clear();
Set(gamepad.leftTrigger, 0);
Assert.That(trace, Is.Empty);
trace.Clear();
Set(gamepad.leftTrigger, 0.9f);
Assert.That(trace,
Started(action, control: gamepad.leftTrigger, value: 0.9f)
.AndThen(Performed(action, control: gamepad.leftTrigger, value: 0.9f)));
trace.Clear();
Set(gamepad.leftTrigger, 0);
Assert.That(trace,
Performed(action, control: gamepad.leftTrigger, value: 0f)
.AndThen(Canceled(action, control: gamepad.leftTrigger, value: 0f)));
trace.Clear();
Set(gamepad.leftTrigger, 0.4f);
Set(gamepad.leftTrigger, 0.3f);//press&release 不超过press-threshold只回落release是不会触发的
Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.4f));
trace.Clear();
}
}
}
}