unity3d 手機中Input類中touch詳解


1.Input.touchCount 觸摸隨之增長  一秒50次增量
2.Input.GetTouch(0).phase==TouchPhase.Moved   手指滑動中最后一幀滑動的狀態 是 運動的
3.TouchPhase  觸摸的幾個狀態
4.Touch.deltaPosition 增量位置   (Input.GetTouch(0).deltaPosition)最后一幀滑動的值  只返回xy軸坐標 也可用vector3(z軸為0) 所以一般用vector2接收

  • static var aa:int;
  • function Update () {
  •         if(Input.touchCount>0)
  •         {
  •                 print(Input.touchCount);
  •         }
  • }
  • function OnGUI()
  • {
  •         GUI.Label(Rect(34,34,34,34),"sdff");
  • }

touchCount指的是觸摸幀的數量。
要注意的是:touch事件 只能在模擬器或者真機上運行(已測試通過) 大約一秒鍾touch不放    touchCount+50次左右

2.Input.touches 觸摸列表

// Prints number of fingers touching the screen

  • //輸出觸摸在屏幕上的手指數量
  • function Update () {
  •         var fingerCount = 0;
  •         for (var touch : Touch in Input.touches) {
  •                 if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
  •                         fingerCount++;
  •         }
  •         if (fingerCount > 0)
  •                 print ("User has " + fingerCount + " finger(s) touching the screen");
  • }

3.讓cube 隨着touch 移動代碼:

static var count:int; //定義touchCount數

  • var particle_:GameObject;//定義存放cube對象
  • var touchposition:Vector3; //存儲移動三維坐標值
  • function Update () {
  •         if(Input.touchCount>0)
  •         {
  •                         count+=Input.touchCount;
  •         }
  •         if((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //如果點擊手指touch了  並且手指touch的狀態為移動的
  •         {
  •                 touchposition=Input.GetTouch(0).deltaPosition;  //獲取手指touch最后一幀移動的xy軸距離
  •                 particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//移動這個距離
  •         }
  • }
  • function OnGUI()
  • {
  •         GUI.Label(Rect(10,10,100,30),"cishu:"+count.ToString());
  •         GUI.Label(Rect(10,50,100,30),touchposition.ToString());
  • }

真機或模擬器測試即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM