Unity Android設備的輸入
1依據屏幕位置輸入
有的時候也許是為了整個有些風格的干凈,減少屏幕上的UI圖標,以至於摒棄了虛擬按鍵這種常用的輸入方式。為了替代虛擬按鍵的輸入方式而選擇了依據點擊事件發生在屏幕中的位置而控制對象的移動等。比如將整個手機屏幕划分為左右兩個區域,左邊區域負責移動控制,右邊區域負責技能釋放。
這里我們需要處理的問題分別是點擊事件的獲取及屏幕信息的獲取,下面這兩個方面的API文檔。
點擊事件:http://wiki.ceeger.com/script/unityengine/classes/touch/touch
屏幕信息:http://wiki.ceeger.com/script/unityengine/classes/screen/screen
1 void Update () { 2 3 if (Input.touchCount > 0)//判斷是否有觸摸信息 4 { 5 for (int i = 0; i < Input.touchCount; i++) 6 { 7 Touch touch = Input.touches[i];//將觸摸信息傳遞給touch 8 9 if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)//touch.phase是觸摸的相位信息,通過TouchPhase枚舉中來確定當前的狀態,Began:一個手指開始接觸;Moved:一個手指發生了移動;Stationary:一個手指接觸了屏幕但是沒有發生移動;Ended:一個手指離開屏幕,也是最后的狀態;Cnaceled:系統取消對觸摸監聽,如用戶把屏幕放到他臉上或超過五個接觸同時發生。 10 { 11 touchPosition = touch.position;//獲取觸摸的位置信息,該坐標與屏幕坐標一致。 12 if (touchPosition.x < screenWidth/4)//screenWidth在Start中已經賦值=>screenWidth = Screen.width; 13 { 14 SomeMethod1();//某方法1 15 beeTransform.Translate(new Vector3(-MoveSpeed, -DownSpeed, 0),Space.World); //beeTransform是游戲對象的位置信息,已在Start中賦值 16 } 17 else if (touchPosition.x>=screenWidth/4&& touchPosition.x <screenWidth / 2) 18 { 19 SomeMethod1(); 20 beeTransform.Translate(new Vector3(MoveSpeed, -DownSpeed, 0), Space.World); 21 22 } 23 } 24 else if (touch.phase == TouchPhase.Ended) 25 SomeMethod2();//某方法 26 } 27 } 28 29 if (Input.touchCount == 0)//未觸摸時 30 SomeMethod2(); 31 }