2017-04-01 86 views
1

阻塞觸摸我們正在建立類似的一個例子「小貓 - 配售的AR虛擬對象」,如下所示:探戈/團結 - UI在屏幕上不

https://developers.google.com/tango/apis/unity/unity-howto-placing-objects

基本上當你觸摸屏幕時,一隻小貓出現在現實世界的飛機(地板)上。

在我們的應用程序中,我們有一個側面菜單,有幾個按鈕,每個都顯示不同的遊戲對象。除了有UI的地方,我們希望檢測屏幕上的任何位置。我們希望UI在Tango中阻止觸摸,並且只允許在沒有UI元素的情況下在屏幕區域實例化相關遊戲對象。

觸摸特定代碼是在這裏:

void Update() { 
    if (Input.touchCount == 1) { 
     // Trigger placepictureframe function when single touch ended. 
     Touch t = Input.GetTouch(0); 
     if (t.phase == TouchPhase.Ended) { 
      PlacePictureFrame(t.position); 
     } 
    } 
} 

(該PlacePictureFrame()地方在觸摸位置的圖像幀的對象。)

我不能找到具有觸摸和UI結合的任何探戈例子。我已經嘗試過一種名爲LeanTouch的資產來阻止UI元素背後的觸動,但它似乎並不適用於Tango。請幫忙!

我已經採用的方法5試圖從該:

How to detect events on UI and GameObjects with the new EventSystem API

,雖然它確實增加一個PhysicsRaycasterTangoARCamera(其被標記爲MainCamera),則OnPointerDown方法沒有產生調試日誌無論在哪裏你觸摸屏幕。探戈是一個特例,所以這不是一個重複的問題。請看下圖:

using System; 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.EventSystems; 

public class PictureFrameUIController : MonoBehaviour, IPointerClickHandler { 

    public GameObject m_pictureFrame; 
    private TangoPointCloud m_pointCloud; 

    void Start() { 
     m_pointCloud = FindObjectOfType<TangoPointCloud>(); 
     addPhysicsRaycaster(); 
    } 

    void addPhysicsRaycaster() { 
     PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>(); 
     if (physicsRaycaster == null) { 
      Camera.main.gameObject.AddComponent<PhysicsRaycaster>(); 
     } 
    } 

    public void OnPointerClick(PointerEventData eventData) { 
     Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name); 
     PlacePictureFrame(eventData.pointerCurrentRaycast.screenPosition); 
    } 


    //void Update() { 
    // if (Input.touchCount == 1) { 
    //  // Trigger placepictureframe function when single touch ended. 
    //  Touch t = Input.GetTouch(0); 
    //  if (t.phase == TouchPhase.Ended) { 
    //   PlacePictureFrame(t.position); 
    //  } 
    // } 
    //} 

    void PlacePictureFrame(Vector2 touchPosition) { 
     // Find the plane. 
     Camera cam = Camera.main; 
     Vector3 planeCenter; 
     Plane plane; 
     if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { 
      Debug.Log("cannot find plane."); 
      return; 
     } 

     // Place picture frame on the surface, and make it always face the camera. 
     if (Vector3.Angle(plane.normal, Vector3.up) > 60.0f && Vector3.Angle(plane.normal, Vector3.up) < 140.0f) { 
      Vector3 forward = plane.normal; 
      // Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; 
      // Vector3 forward = Vector3.Cross(right, plane.normal).normalized; 
      Instantiate(m_pictureFrame, planeCenter, Quaternion.LookRotation(forward, Vector3.up)); 
     } else { 
      Debug.Log("surface is not steep enough for picture frame to be placed on."); 
     } 
    } 

    public void DeleteAllFrames() { 
     GameObject[] frames = GameObject.FindGameObjectsWithTag("Frame"); 
     if (frames == null) { 
      return; 
     } 
     foreach (GameObject frame in frames) { 
      Destroy(frame); 
     } 
    } 
} 
+0

這是重複的。如果該解決方案確實不適用於您,那麼您應該在該解決方案中嘗試瞭解您的問題。請使用該解決方案嘗試更新您的問題。也包括課程。 – Programmer

+0

我做到了。沒有答案,因爲你已經標記爲重複。您的解決方案與Tango無關。 – fiixed

+0

同樣,你應該用那個「沒用」的代碼更新你的問題。您從*方法5 *嘗試的代碼。該代碼應附加到您檢測點擊的GameObject上。你必須展示該代碼,以便人們可以幫助你。 – Programmer

回答

2

如果你想在任何地方檢測到點擊,除了那裏是一個UI控件/組件在屏幕上,你必須檢查指針在與EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)的UI。

如果在桌面上,請使用EventSystem.current.IsPointerOverGameObject()。您正在使用Tango,因此EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)。應該使用。

void Update() 
{ 
    if (Input.touchCount == 1) 
    { 
     //Trigger placepictureframe function when single touch ended. 
     Touch t = Input.GetTouch(0); 
     if (t.phase == TouchPhase.Ended) 
     { 
      //Make sure that pointer is not over UI before calling PlacePictureFrame 
      if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) 
      { 
       PlacePictureFrame(t.position); 
      } 
     } 
    } 
} 

編輯:

好像這個作品只TouchPhase.Began

更改t.phase == TouchPhase.Endedto t.phase == TouchPhase.Began這應該按預期工作。確保使用移動設備/探戈而不是鼠標進行測試。

+1

嗯,不幸的是,沒有工作。觸摸時仍然會將相框放在UI對象後面。 – fiixed

+0

嗨,我看着它,將't.phase == TouchPhase.Ended'改爲't.phase == TouchPhase.Began'。這是問題所在。我不明白爲什麼它不適用於't.phase == TouchPhase.Ended'。 – Programmer

+0

你我的朋友是f **的國王槍!有效!謝謝! – fiixed