用户输入管理
用户输入管理
1虚拟轴
使用的API:.GetAxis("/*Edit>Project Settings>Input Manager>Axes中对应的名字*/");
1.1控制位置
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using UnityEngine; public class Test : MonoBehaviour { void Update() { float horizontal = Input.GetAxis( "Horizontal" ); float vertical = Input.GetAxis( "Vertical" ); if (horizontal != 0) { transform.position= new Vector3(transform.position.x+horizontal*0.1f, transform.position.y,transform.position.z); } if (vertical != 0) { transform.position= new Vector3(transform.position.x,transform.position.y+vertical*0.1f,transform.position.z); } } } |
该段代码实现的目标是在运行时可以通过WASD来控制前后左右,Horizontal和Vertical是Edit>Project Settings>Input Manager>Axes中Horizontal和Vertical的名字。
1.2控制旋转
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine; public class Test : MonoBehaviour { void Update() { float horizontal = Input.GetAxis( "Horizontal" ); if (horizontal != 0) { transform.eulerAngles= new Vector3(transform.eulerAngles.x, transform.eulerAngles.y+horizontal, transform.eulerAngles.z); } } } |
该段代码实现的功能是通过AD来控制左右旋转。
1.3控制其他
示例
1 2 3 4 5 6 7 8 9 | using UnityEngine; public class Test : MonoBehaviour { void Update() { Debug.Log(Input.GetAxis( "Mouse X" )); } } |
该段代码实现的功能是在控制台输出鼠标在X轴方向移动的值,Mouse X也可以换成Edit>Project Settings>Input Manager>Axes中的其他值来实现相应的控制。
2获取键盘事件
使用的API:GetKey("相应的键"/*也可以用KeyCode.A*/)
1 2 3 4 5 6 7 8 9 10 11 12 | using UnityEngine; public class Test : MonoBehaviour { void Update() { if (Input.GetKey( "a" /*也可以用KeyCode.A*/ )) { Debug.Log( "A键被按下了" ); } } } |
该段代码实现的功能是,在按住A键时控制台不停输出:A键被按下了。
若上面代码将GetKey换成GetKeyDown,则控制台会在按键按下时打印一次输出。
若上面代码将GetKey换成GetKeyUp,则控制台会在按键抬起时打印一次输出。
3获取鼠标事件
3.1获取鼠标按键
使用的API:GetMouseButtonDown(数字)、GetMouseButtonUp(数字)、GetMouseButton(数字)/*数字0代表鼠标左键,数字1代表鼠标右键,数字2代表鼠标滚轮*/
示例1
1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine; public class Test : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) { GameObject cube = GameObject.Find( "Cube" ); cube.transform.localScale= new Vector3(2,2,2); } } } |
画面中创建一个立方体,当上述代码执行时,按下鼠标左键,画面中的立方体会放大一倍。
若将上面代码中的GetMouseButtonDown换成GetMouseButtonUp则会在按键抬起时执行效果。示例2
1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine; public class Test : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) { GameObject cube = GameObject.Find( "Cube" ); cube.transform.localScale+= new Vector3(1,1,1); } } } |
上述代码的功能是,在播放时不停按下鼠标左键,立方体会被不断放大。
3.2获取鼠标位置
使用的API:Input.mousePosition;
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using UnityEngine; public class Test : MonoBehaviour { bool isDown= false ; void Update() { if (Input.GetMouseButtonDown(0)) { isDown= true ; } if (Input.GetMouseButtonUp(0)) { isDown = false ; } if (isDown) { GameObject go = GameObject.Find( "Image" ); go.transform.position=Input.mousePosition; } } } |
创建一个图片,该段代码实现的功能是,点击鼠标左键后,图片会回到鼠标指针所在的位置,当鼠标持续点击并移动时,图片会跟随鼠标指针。
4移动设备输入
使用的API:Input.touchCount
Input.touches[0].fingerId
Input.touches[0].position
Input.touches[0].deltaPosition;
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using UnityEngine; using UnityEngine.UI; public class MyText : MonoBehaviour { Text juse; void Start() { juse = GameObject.Find( "Text" ).GetComponent<Text>(); } void Update() { if (Input.touchCount >= 1) { juse.text= "当前有:" + Input.touchCount + "根手指触碰。" + "当前手指的ID是:" + Input.touches[0].fingerId + "当前手指所在位置是:" + Input.touches[0].position + "从上一帧到现在移动了:" + Input.touches[0].deltaPosition; } } } |
在场景中新建一个Text,将上面的代码写好并打包成apk文件,运行apk文件,在点击屏幕的时候,屏幕上会显示当前有几根手指触碰,当前手指的ID,当前手指所在位置,和从上一帧到现在移动了多少。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了