InputSystem学习

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Unity InputSystem学习记录
参考:
转载
官方文档
转载


一、输入系统切换

二、两种使用InputSystem的方法

1.直接引用

不需要InputAction,4类的各种按键均有默认值;
代码如下(示例):

private void Update()
    {

        var gamepad = Gamepad.current;//手柄
        var keyboard = Keyboard.current;//键盘
        var mouse = Mouse.current;//鼠标
        var pointer = Pointer.current;//指针

        if (gamepad != null)
        {
            Debug.Log(gamepad.leftStick.ReadValue());//手柄遥感的偏移
            if (gamepad.bButton.wasPressedThisFrame)
                Debug.Log("按下B键");
        }
        if (keyboard != null)
        {
            //执行顺序 isPressed = false -> 按下:wasPressedThisFrame = true -> 中途:isPressed = true -> 松开:wasReleasedThisFrame = true -> isPressed = false
            if (keyboard.wKey.wasPressedThisFrame)
                Debug.Log("w键按下(一直按住w键的话,也只执行一次)");
            if (keyboard.wKey.wasReleasedThisFrame)
                Debug.Log("w键松开");
            Debug.Log("是否按住w键:" + keyboard.wKey.isPressed);
        }
        if (mouse != null)
        {
            Debug.Log(mouse.scroll.ReadValue());//滚轮的滚动值,向前滚Y的值为正,向后滚为负

            if (mouse.leftButton.wasPressedThisFrame)
                Debug.Log("按鼠标左键");
            if (mouse.rightButton.wasPressedThisFrame)
                Debug.Log("按鼠标右键");
            if (mouse.middleButton.wasPressedThisFrame)
                Debug.Log("按滚轮键");
        }
        if (pointer != null)
        {
                Debug.Log(pointer.delta.ReadValue());//与上一帧的偏移
              Debug.Log(pointer.position.ReadValue());//在空间中的坐标
        }

2.间接引用

创建InputAction, 其中Move,Look是Value(Vector2类型),Jump是Button,Sprint是PassThrough类型;
在这里插入图片描述

(1)通过PlayerInput

在这里插入图片描述
通过绑定Actions,来获取输入;Behaviors一栏有四种,当为SendMessage/BroadcastMessage时可通过挂在该物体上的脚本的方法回调;

    public void OnMove(InputValue value)//形参可为空
    {
        var v = value.Get<Vector2>()//Todo
    }
       public void OnJump(InputValue value)//Button;形参可为空,按下为ture;松开不会变成false;
    {
        var v = value.isPressed;
        //Todo
    }
    public void OnSprint(InputValue value)//PassThrough;形参可为空,按下为ture;松开会变成false;
	{
		var v = value.isPressed;
		//Todo
	}

当为UnityEvent时,其他基本相同,回调参数改变

    public void OnMove(InputAction.CallbackContext context)
    {
        var value = context.ReadValue<Vector2>();
        //Todo
    } 

(2)通过C#

在这里插入图片描述
Apply后,自动生成脚本(生成的文件名和类名可能不一致,如果你改名字的话);通过其他脚本来调用;

     private StarterInput StarterInput;
     private void OnEnable()
    {
        if (StarterInput == null)
        {
            StarterInput = new StarterInput();
            //Todo
        }
        StarterInput.Enable();
    }
    private OnDisable()
	{
    StarterInput.Disable();
	}
Value类

如move;要在Update里读取;

     private void Update()
    {
      var  move = StarterInput.Player.Move.ReadValue<Vector2>();  
    }
Button类

如jump;回调有顺序 started=>performed=>canceled,在按下期间(started,performed)ReadValueAsButton始终为true;松开后(canceled)ReadValueAsButton为false;
如果想达到按下变true然后马上变回false;建议改成isjump=false或者在lateUpdate中设置isJump=false

	public bool isJump;
	 private void OnEnable()
    {
     StarterInput.Player.Jump.started += ctx => {isJump = ctx.ReadValueAsButton();};
     StarterInput.Player.Jump.performed += ctx =>
      {//isJump = ctx.ReadValueAsButton();
      	isJump =false};
     StarterInput.Player.Jump.canceled += ctx => {isJump = ctx.ReadValueAsButton();};
    }
PassThrough类

如Sprint;PassThrough类不响应started ,canceled 回调,只响应performed 回调,

performed回调;按下ReadValueAsButton变true,松开变ReadValueAsButtonfalse

	public bool isSprint;
	 private void OnEnable()
    {
     StarterInput.Player.isSprint.performed += ctx =>{isSprint= ctx.ReadValueAsButton();};    
    }
注意

需要注意的是我们必须手动的设置其Enable和Disable,来启动或关闭InputAction


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我无法找到与"python英语单词学习系统"相关的具体信息。但是,我可以为您提供一个基本的Python英语单词学习系统的设计思路。 设计思路如下: 1. 创建一个包含英语单词和对应中文释义的字典。 2. 随机选择一个单词,并显示其中文释义。 3. 用户输入该单词的英文翻译。 4. 检查用户输入是否与字典中的对应值匹配。 5. 根据用户的回答给予相应的反馈,例如正确或错误。 6. 统计用户的答题情况,包括正确率和答题次数。 7. 提供菜单选项,让用户选择继续学习或退出系统。 以下是一个简单的示例代码: ```python import random # 英语单词字典 word_dict = { 'apple': '苹果', 'banana': '香蕉', 'cat': '猫', # 其他单词... } # 统计信息 total_attempts = 0 correct_attempts = 0 def english_word_learning_system(): global total_attempts, correct_attempts while True: # 随机选择一个单词 word = random.choice(list(word_dict.keys())) translation = word_dict[word] # 显示中文释义并获取用户输入 print("中文释义:", translation) user_input = input("请输入该单词的英文翻译:") # 检查用户输入是否正确 if user_input.lower() == word: print("回答正确!") correct_attempts += 1 else: print("回答错误!正确答案是:", word) total_attempts += 1 # 统计信息 print("总答题次数:", total_attempts) print("正确率:", correct_attempts / total_attempts * 100, "%") # 询问用户是否继续学习 choice = input("是否继续学习?(输入'是'或'否'):") if choice.lower() != '是': break english_word_learning_system() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值