简单键鼠事件 — “Test_03”
KeyTest
键鼠事件每帧都要监听,要放在Update()中处理
public class KeyTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){// 【鼠标点击事件】 0左键、1右键、2中键(滚轮)// 按下鼠标左键if (Input.GetMouseButtonDown(0)){Debug.Log("点击鼠标左键");}// 持续按下鼠标左键if (Input.GetMouseButton(0)){Debug.Log("持续按下了鼠标左键");}if (Input.GetMouseButtonUp(0)){Debug.Log("抬起了鼠标左键");}// 【键盘点击事件】// 按下按键if (Input.GetKeyDown(KeyCode.A)){Debug.Log("按下了A键");}// 持续按下按键if (Input.GetKey(KeyCode.A)){Debug.Log("持续按下了A键");}// 抬起按键if (Input.GetKeyUp(KeyCode.A)){Debug.Log("松开了A键");}}
}
虚拟轴 — “AxisTest”
编辑 -> 项目设置 -> 输入管理器
public class AxisTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){// 获取水平轴,通过虚拟轴名称获取float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");Debug.Log(horizontal + " " +vertical);// 虚拟按键,通过虚拟按键名称定位,这里是设置了跳跃按键为空格if (Input.GetButtonDown("Jump")){Debug.Log("按下空格");}if (Input.GetButton("Jump")){Debug.Log("持续按下空格");}if (Input.GetButtonUp("Jump")){Debug.Log("松开空格");}}
}