【Unity3D入门教程】鼠标和键盘输入与控制
生活随笔
收集整理的這篇文章主要介紹了
【Unity3D入门教程】鼠标和键盘输入与控制
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文講述了怎樣進行鼠標和鍵盤的輸入信息檢測。外部設備輸入檢測需要每一幀運行,所以檢測的函數(shù)需要寫在Update函數(shù)中。本文講的內(nèi)容比較簡單,直接上代碼吧。
using UnityEngine; using System.Collections;public class InputMethod : MonoBehaviour {int mPressMouseLeft = 0;int mPressMouseRight = 0;int mPressMouseMiddle = 0;void Start () {}void Update () {//鼠標按下事件if (Input.GetMouseButtonDown(0)){Debug.Log("按下了鼠標左鍵");}if (Input.GetMouseButtonDown(1)){Debug.Log("按下了鼠標右鍵");}if (Input.GetMouseButtonDown(2)){Debug.Log("按下了鼠標中鍵");}//鼠標抬起事件if (Input.GetMouseButtonUp(0)){Debug.Log("抬起了鼠標左鍵");}if (Input.GetMouseButtonUp(1)){Debug.Log("抬起了鼠標右鍵");}if (Input.GetMouseButtonUp(2)){Debug.Log("抬起了鼠標中鍵");}//鼠標長按事件if (Input.GetMouseButton(0)){mPressMouseLeft++; }else{if (mPressMouseLeft > 0){Debug.Log("鼠標左鍵按下的幀數(shù)為: " + mPressMouseLeft.ToString());}mPressMouseLeft = 0;}if (Input.GetMouseButton(1)){mPressMouseRight++; }else{if (mPressMouseRight > 0){Debug.Log("鼠標右鍵按下的幀數(shù)為: " + mPressMouseRight.ToString()); }mPressMouseRight = 0;}if (Input.GetMouseButton(2)){mPressMouseMiddle++; }else{if (mPressMouseMiddle > 0){Debug.Log("鼠標中鍵按下的幀數(shù)為: " + mPressMouseMiddle.ToString());}mPressMouseMiddle = 0;}//鍵盤按下事件if (Input.GetKeyDown(KeyCode.Space)){Debug.Log("按下了空格");}//鍵盤抬起事件if (Input.GetKeyUp(KeyCode.Space)){Debug.Log("抬起了空格");}//鍵盤長按事件if (Input.GetKey(KeyCode.Space)){Debug.Log("空格正在被按下狀態(tài)");}} }
運行后,點擊鼠標和空格鍵,會看到如下結果。
總結
以上是生活随笔為你收集整理的【Unity3D入门教程】鼠标和键盘输入与控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医学影像三维可视化软件
- 下一篇: 家用电器用户行为分析与事件识别