当前位置:   article > 正文

Unity3D基础9:获取鼠标键盘输入_unity input.getkeydown(keycode)

unity input.getkeydown(keycode)

 

前文:https://blog.csdn.net/Jaihk662/article/details/86751079(C#脚本)

一、获取键盘输入

相关API(其中KeyCode为键码,例如空格键就是KeyCode.Space):

  • Input.GetKey(KeyCode):按下某键后,持续返回 ture
  • Input.GetKeyDown(KeyCode):按下某键的一瞬间返回 true
  • Input.GetKeyUp(KeyCode):松开某键的一瞬间返回 true
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Text1: MonoBehaviour
  5. {
  6. // Use this for initialization
  7. void Start()
  8. {
  9. Debug.Log("Start"); //输出调试
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. if (Input.GetKey(KeyCode.Space))
  15. {
  16. Debug.Log("你压着空格不放");
  17. }
  18. if (Input.GetKeyDown(KeyCode.Space))
  19. {
  20. Debug.Log("你按下了空格");
  21. }
  22. if (Input.GetKeyUp(KeyCode.Space))
  23. {
  24. Debug.Log("你松开了空格");
  25. }
  26. if (Input.GetKey(KeyCode.A))
  27. {
  28. Debug.Log("你压着A不放");
  29. }
  30. if (Input.GetKeyDown(KeyCode.A))
  31. {
  32. Debug.Log("你按下了A");
  33. }
  34. if (Input.GetKeyUp(KeyCode.A))
  35. {
  36. Debug.Log("你松开了A");
  37. }
  38. }
  39. }

很显然必须在Update()中使用这些方法,不然没有意义

运行后效果如下:

 

二、获取鼠标输入

相关API(0:鼠标左键;1:鼠标右键;2:鼠标中键)

  • Input.GetMouseButton(0/1/2):按下某键后,持续返回 true
  • Input.GetMouseButtonDown(0/1/2):按下某键的一瞬间,返回 true
  • Input.GetMouseButtonUp(0/1/2):松开某键的一瞬间,返回 true
  1. if (Input.GetMouseButton(1))
  2. {
  3. Debug.Log("你压着鼠标右键不放");
  4. }
  5. if (Input.GetKeyDown(KeyCode.Space))
  6. {
  7. Debug.Log("你按下了鼠标右键");
  8. }
  9. if (Input.GetKeyUp(KeyCode.Space))
  10. {
  11. Debug.Log("你松开了鼠标右键");
  12. }

一个Unity3D错误:error CS0101: The namespace 'global::' already contains a definition for `xxxxx'

解决方案:编译错误,某个命名空间下有重复的标识符,看下函数名是否冲突等等,例如两个相同的类名、脚本名和类对不上等等

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/91767
推荐阅读
相关标签
  

闽ICP备14008679号