Unity SKFramework框架(九)、Input 输入管理模块
【摘要】
目录
一、Key Input
二、Mouse Input
三、Axis Input
四、Input Master
五、Input Trigger
一、Key Input
通过编辑器设置一个键盘按键:
using UnityEngine;using SK.Framework; public class Examp...
目录
一、Key Input
通过编辑器设置一个键盘按键:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
[SerializeField] private KeyInput aInput;
}
通过代码设置一个键盘按键:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private KeyInput aInput;
private void Start()
{
aInput = new KeyInput(KeyCode.A);
}
}
开启按键监听:
//开启按键监听
aInput.BeginListening();
停止按键监听:
//停止按键监听
aInput.StopListening();
按键按下、持续按下、抬起:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private KeyInput aInput;
private void Start()
{
aInput = new KeyInput(KeyCode.A);
//开启按键监听
aInput.BeginListening();
}
private void Update()
{
if (aInput.IsPressed) Debug.Log("A键 按下");
if (aInput.IsHeld) Debug.Log("A键 持续按下");
if (aInput.IsReleased) Debug.Log("A键 抬起");
}
}
二、Mouse Input
通过编辑器设置一个鼠标按键:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
[SerializeField] private MouseButtonInput leftMouseButtonInput;
}
通过代码设置一个鼠标按键:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private MouseButtonInput leftMouseButtonInput;
private void Start()
{
leftMouseButtonInput = new MouseButtonInput(MouseButtonCode.Left);
}
}
开启按键监听:
//开启按键监听
leftMouseButtonInput.BeginListening();
停止按键监听:
//停止按键监听
leftMouseButtonInput.StopListening();
按键按下、持续按下、抬起:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private MouseButtonInput leftMouseButtonInput;
private void Start()
{
leftMouseButtonInput = new MouseButtonInput(MouseButtonCode.Left);
//开启按键监听
leftMouseButtonInput.BeginListening();
}
private void Update()
{
if (leftMouseButtonInput.IsPressed) Debug.Log("鼠标左键 按下");
if (leftMouseButtonInput.IsHeld) Debug.Log("鼠标左键 持续按下");
if (leftMouseButtonInput.IsReleased) Debug.Log("鼠标左键 抬起");
}
}
三、Axis Input
通过编辑器设置一个轴输入:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
[SerializeField] private AxisInput horizontal;
}
通过代码设置一个轴输入:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private AxisInput horizontal;
private void Start()
{
horizontal = new AxisInput(AxisNames.Horizontal);
}
}
开启监听:
//开启监听
horizontal.BeginListening();
停止监听:
//停止监听
horizontal.StopListening();
读取轴输入值:
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private AxisInput horizontal;
private void Start()
{
horizontal = new AxisInput(AxisNames.Horizontal);
//开启监听
horizontal.BeginListening();
}
private void Update()
{
float hValue = horizontal.ReadValue();
float hRawValue = horizontal.ReadRawValue();
}
}
四、Input Master
1.InputMaster是整个输入模块的管理器,其核心属性Toggle是整个系统的开关,将其设为false时,将不再监听任何输入:
//关闭系统开关
InputMaster.Toggle = false;
2.可以通过如下属性判断是否有任意按键输入:
bool isAnyKey = InputMaster.IsAnyKey;
bool isAnyKeyDown = InputMaster.IsAnyKeyDown;
3.可以通过如下方式获取鼠标位置的偏移量:
//鼠标位置偏移量
Vector2 mouseDelta = InputMaster.Mouse.Delta;
五、Input Trigger
using UnityEngine;
using SK.Framework;
public class Example : MonoBehaviour
{
private KeyInput aInput;
private void Start()
{
aInput = new KeyInput(KeyCode.A);
//开启监听
aInput.BeginListening();
}
private void Update()
{
if (aInput.IsPressed) Debug.Log("Pressed");
}
}
如上述代码所示,我们监听了键盘A键的输入,当A键按下时,将打印日志"Pressed"。Trigger用于触发按键的输入,表示我们无需真实的去按下键盘A键,通过调用如下代码,打印日志"Pressed"的逻辑也会被执行。
//触发按键A按下
InputMaster.Key.Trigger(KeyCode.A, InputTriggerType.Pressed);
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/124884667
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)