2017-06-12 22 views
1

我正在研究Android應用程序Unity3D其中我試圖在unity3d中運行時實例化和銷燬GameObjects。我是統一新人,目前正在研究Google的項目探戈,我只是試圖將一個對象放置在飛機上,然後通過觸摸對該對象執行旋轉或移動操作。Unity3D - 無法使用EventSystem在Android中獲取當前選定的遊戲對象

我爲我的3D對象使用FBX文件並利用事件系統來獲取觸摸更新。查找飛機並將物體放置在飛機上,因爲他們在其docs上有明確的教程。但是現在我想通過觸摸來獲得所選的GameObject。

我已經爲導入的fbx對象創建了預製,併爲其添加了一個框對撞機。然後,我添加了一個Physics Raycater到我的Tango相機,並創建了一個EventSystem GameObject以在運行時獲取觸摸事件。見附圖。

GameObject

EventSystem

我的劇本是這樣的(見IsPointerOverGameObject方法): -

using UnityEngine; 
using System.Collections; 
using UnityEngine.EventSystems; 
public class KittyUIController : MonoBehaviour 
{ 
public GameObject m_object; 
private TangoPointCloud m_pointCloud; 
private bool isGameObjectSelected = false; 
void Start() 
{ 
    m_pointCloud = FindObjectOfType<TangoPointCloud>(); 
    InventoryItemDisplay.onClick += InventoryItemDisplay_onClick; 
} 
void OnDestroy() 
{ 
    Debug.Log ("MY LOGS - Unsigned-up for onClick"); 
    InventoryItemDisplay.onClick -= InventoryItemDisplay_onClick; 
} 
void Update() 
{ 
    if (Input.touchCount == 1) 
    { 
     Touch t = Input.GetTouch(0); 
     // Trigger place kitten function when single touch ended. 
     if (t.phase == TouchPhase.Ended) { 
      if (m_object != null && !isGameObjectSelected) { 
       Debug.Log ("MY LOGS - Placing object to the plane"); 
       PlaceObject (t.position); 
      } else { 
       isGameObjectSelected = false; 
      } 
     } else if (t.phase == TouchPhase.Began) { 
      IsPointerOverGameObject (t.fingerId); // Here i'm checking weather a GameObject is Touched or not 
     } 
    } 
} 
/* 
* This method will check whether a game object is touched or not using Event System  
*/ 
void IsPointerOverGameObject(int fingerId){ 

    if (EventSystem.current.IsPointerOverGameObject (fingerId)) { 
     isGameObjectSelected = true; 
     Debug.Log ("MY LOGS - Is pointing over game object"); 
     //-------HERE IS THE REAL ISSUE-------// 
     Debug.Log ("MY LOGS - GETTING SELECTED GAMEOBJECT : " + EventSystem.current.currentSelectedGameObject); 
     GameObject go = EventSystem.current.currentSelectedGameObject; 
     Debug.Log ("MY LOGS - Got the game object!!\nTag for game object is : "+go.tag); 
     // Destroy if Object is of type 3D object 
     if (go.tag == "3DObject") { 
      Debug.Log ("MY LOGS - yoo a 3d object is selected"); 
      Destroy (go); 
     } else { 
      Debug.Log ("MY LOGS - Oops the selected object don't have a tag of 3d object"); 
     } 
    } else { 
     Debug.Log ("MY LOGS - Is not pointing over game object"); 
    } 
} 
//-------Click listener to listen to the clicks in Side Panel 
void InventoryItemDisplay_onClick (InventoryItem item, GameObject itemObject) 
{ 
    Debug.Log ("MY LOGS - Vola..You have selected " + item.displayName); 
    m_object = itemObject; 
} 
/* 
* This method will place selected gameobject to the plane  
*/ 
void PlaceObject(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("MY LOGS - cannot find plane."); 
     return; 
    } 
    // Place object on the surface, and make it always face the camera. 
    if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) 
    { 
     Vector3 up = plane.normal; 
     Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; 
     Vector3 forward = Vector3.Cross(right, plane.normal).normalized; 
     Instantiate(m_object, planeCenter, Quaternion.LookRotation(forward, up)); 
    } 
    else 
    { 
     Debug.Log("MY LOGS - surface is too steep for kitten to stand on."); 
    } 
    } 
} 

我有一個列表項側面板預製這也越來越在運行時創建。因此,當我觸摸側面板上的列表項來選擇一個對象時,所有內容都在IsPointerOverGameObject方法運行良好,除了打印選定對象的標記。但是,當我碰到放置在屏幕的表面上我的對象,然後它不工作後Debug.Log ("MY LOGS - Is pointing over game object");IsPointerOverGameObject方法

它會看到我的logcat後得到明確。

logcat的

06-10 15:33:31.094: I/Unity(15009): MY LOGS - Is not pointing over game object 
06-10 15:33:38.619: I/Unity(15009): MY LOGS - Is pointing over game object 
06-10 15:33:38.621: I/Unity(15009): MY LOGS - GETTING SELECTED GAMEOBJECT : InventoryItemDisplay(Clone) (UnityEngine.GameObject) 
06-10 15:33:38.621: I/Unity(15009): MY LOGS - Got the game object!! 
06-10 15:33:38.622: I/Unity(15009): MY LOGS - Oops the selected object don't have a tag of 3d object 
06-10 15:33:38.665: I/Unity(15009): MY LOGS - Vola..You have selected Rose 
06-10 15:33:45.175: I/Unity(15009): MY LOGS - Is not pointing over game object 
06-10 15:33:45.286: I/Unity(15009): MY LOGS - Placing object to the plane 
06-10 15:33:50.379: I/Unity(15009): MY LOGS - Is pointing over game object 
06-10 15:33:50.379: I/Unity(15009): MY LOGS - GETTING SELECTED GAMEOBJECT : 
06-10 15:33:54.558: I/Unity(15009): MY LOGS - Is not pointing over game object 
06-10 15:33:54.686: I/Unity(15009): MY LOGS - Placing object to the plane 
06-10 15:33:54.688: I/Unity(15009): MY LOGS - surface is too steep for kitten to stand on. 

現在,如果你看到第三行logcat的,你會看到,它打印所選的對象:InventoryItemDisplay(克隆),但如果你的下一行在代碼中檢查Debug.Log(),然後你會看到我不打印日誌後\n....這是當我點擊側面板上的項目。現在看到第四個線從logcat的底部。這是當我點擊初始化的遊戲對象時。它甚至不會在該行之後打印任何內容。它直接打印出寫在Touch.Ended條件下的代碼。

ADDING截圖供現場 screenshot of the scene

+1

你幾乎接近。 '物理Raycater'使用'EventSystems',例如'IPointerDownHandler,IPointerClickHandler,IPointerUpHandler,IPointerExitHandler,IPointerEnterHandler, IBeginDragHandler,IDragHandler,IEndDragHandler'。有關更多信息,請參閱重複的問題。 – Programmer

+0

非常感謝..我會給它一個嘗試,讓你知道結果:) –

回答

0

據我所知, EventSystem.current.currentSelectedGameObject只能參照一個UI對象,即使它沒有明確規定in the documentation。你正在實例化的不是一個UI對象,而只是場景中的一個常規GameObject。我看不到完整的Inspector視圖,但是我可以看到,在實例化過程中,您沒有將Canvas的變換設置爲GameObject的父元素,這就足夠了。

如果您想要在場景中引用GameObject,則需要在實例化過程中存儲對它的引用,或者您需要根據觸摸的位置進行Raycast來獲取它。該方法取決於你想要達到的目標。

+0

好吧...從你的答案我得到的是我應該無法獲得我正在實例化的gameobject。但是當我觸及到我已經實例化的gameobject時,我變得真實。見上面的logcat。我從'isPointingOverGameObject'中獲得了真實。所以我確定它檢測到遊戲對象。但這裏唯一的問題是,我無法獲得對象以便執行某些操作。 還有第二種可能性,我能夠從你的答案得到。你是說我需要在canvas中實例化gameobject或者其他東西,以便我可以獲取該對象的實例? –

+1

是的,我不認爲它可以通過'EventSystem.current.currentSelectedGameObject'訪問,除非它是一個UI對象。我假設「InventoryItemDisplay(Clone)」是一個UI對象?這可以解釋爲什麼你可以在日誌中看到它,但不是你稍後實例化的那個。關於你的編輯,如果你正在實例化的GameObject背後有一個UI對象,'EventSystem.current.IsPointerOverGameObject(fingerId)'也可以返回true。我不確定,因爲在運行時沒有實際發生的圖像,所以我不知道場景中還有其他類型的對象。 – Kasperi

+0

我添加了視圖的截圖,以便您可以看到發生了什麼問題。除了坐在地板上的小貓以外,屏幕上沒有其他的遊戲物體。當我接觸到小貓時,我獲得了真正的價值,但是我不能像日誌中看到的那樣獲得小貓gameobject。當我嘗試使用'EventSystem.current.currentSelectedGameObject'獲取當前選定的遊戲對象時,它不會在代碼中繼續進行。 –

相關問題