您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 6浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity SKFramework框架(九)、Input 输入管理模块

CoderZ1010 发布时间:2022-05-20 16:42:46 ,浏览量:6

目录

一、Key Input

二、Mouse Input

三、Axis Input

四、Input Master

五、Input Trigger

一、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);
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.2656s