【unity3D】單點和多點觸控,unity3d單點觸控

來源:互聯網
上載者:User

【unity3D】單點和多點觸控,unity3d單點觸控

【狗刨學習網】


總結:

    Input.touchCount擷取當前的觸摸點數目,若為1則是單點觸控,大於1則是多點觸控

    點擊事件用:Input.GetTouch(num).phase == TouchPhase.Began這樣的格式


代碼:


    using UnityEngine;

    using System.Collections;

    public class click2 : MonoBehaviour {

        //設定點擊時顯示的圖片

        public Texture2D img;

            void Start () {

        }

            void Update () {

        }

            void OnGUI () {

        //記錄當前觸控點數目

        int count = Input.touchCount;

        //單點觸控,首個觸控點的標誌是0

        if (count == 1) {

          //if(Input.GetTouch(0).phase == TouchPhase.Began){

             }

             float x = Input.GetTouch(0).position.x;

             float y = Input.GetTouch(0).position.y;

             GUI.DrawTexture(new Rect(x,y,100,100),img);

        }

        //多點觸控,遍曆每個觸摸點

        for (int i = 0 ; i < count ; i++){

            //if(Input.GetTouch(i).phase == TouchPhase.Began){}

               float x = Input.GetTouch(i).position.x;

               float y = Input.GetTouch(i).position.y;

               GUI.DrawTexture(new Rect(x,y,100,100),img);

        }

    }

}


注意:


記得把指令檔拖到Camera裡面

然後設定指令碼的圖片



更多精彩盡在狗刨學習網

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.