快速上手
以下内容旨在快速在您的项目中使用 winS.Unity.Modules.Input。
创建输入模块
想要在项目中使用输入模块,您必须定义一个继承自winS.Unity.Modules.Input.InputModuleBase的类型:
InputModule.cs
using winS.Unity.Modules.Input;
public class InputModule : InputModuleBase
{
}
此类型将由 winS for Unity 创建并管理,在运行时,您可以通过winS.Unity.Runtime.GetModule来获取InputModule。
添加输入动作
现在我们已经拥有了一个输入模块,想要使用输入,我们需要在模块中定义输入动作。 我们对上文的 InputModule 进行更新:
InputModule.cs
using winS.Unity.Modules.Input;
public class InputModule : InputModuleBase
{
public InputActionClick click { get; private set; } = new InputActionClick();
public override void Enable()
{
base.Enable();
click.Enable();
}
public override void Disable()
{
base.Disable();
click.Disable();
}
}
在上述代码中,我们在 InputModule 中定义了一个模块内置输入动作 InputActionClick 用于点击的输入检测。
同时我们覆写Enable和Disable方法,使模块可以正确控制该输入动作的启用/禁用。
使用输入
现在我们已经拥有了一个带点击的输入模块,我们可以用如下方式监听点击输入:
using UnityEngine;
using winS.Unity.Modules.Input;
public class Example_UseInput : MonoBehaviour
{
private void Start()
{
InputModule inputModule = winS.Unity.Runtime.GetModule<InputModule>();
inputModule.click.onClickWithScreenPoint.Add(OnClick);
}
private void OnDestroy()
{
InputModule inputModule = winS.Unity.Runtime.GetModule<InputModule>();
inputModule.click.onClickWithScreenPoint.Remove(OnClick);
}
private void OnClick(ScreenPoint screenPoint)
{
Debug.Log($"Click: {screenPoint}");
}
}
在上述代码中,我们通过winS.Unity.Runtime.GetModule方法来获取输入模块,并订阅了InputActionClick.onClickWithScreenPoint输入事件。 一旦用户有任何点击行为,OnClick方法就会被调用。