2015-04-22 54 views
1

我下面this video here多點觸控的Unity遊戲不工作

的教程,一切工作正常,除了多點觸摸的部分。我有兩個按鈕,實際上是畫布上的兩個不同的圖像,但是當我使用其中一個時,我無法使用其他觸摸按鈕。我的每個按鈕都有這個腳本。

下面是代碼:

using UnityEngine.UI; 
    using UnityEngine.EventSystems; 
    using System.Collections; 

    public class SimpleTouchShoot : MonoBehaviour, IPointerDownHandler, IPointerUpHandler 
    { 
     private bool touched; 
     private int pointerID; 
     private bool canFire; 

    void Awake() 
    { 
     touched = false; 
     canFire = false; 
    } 

    public void OnPointerDown (PointerEventData data) 
    { 
     if(!touched) 
     { 
      touched = true; 
      pointerID = data.pointerId; 
      canFire = true; 
     } 

    } 

    public void OnPointerUp (PointerEventData data) 
    { 
     if(data.pointerId == pointerID) 
     { 
      touched = false; 
      canFire = false; 
     } 

    } 

    public bool CanFire() 
    { 
     return canFire; 
    } 
} 

統一的版本是4.6。

回答

3

您正在使用接口作爲事件處理程序。它沒有實現自己的多點觸摸功能,所以你必須製作自己的觸摸邏輯。在大多數情況下,這是一個很好的做法。製作一個數組或觸摸輸入列表併爲其設置案例。

Unity3D Forum - Event System

點擊鏈接,並檢查應答號5回答是這樣做的哈克的方式。因爲事件接口將EventHandler添加到腳本的任何GameObject中。但它總是需要告訴遊戲引擎您接受多個輸入< - 以簡化操作。捕捉被忽略的第二個觸摸輸入。