2017-06-12 7 views
1

でAndroidアプリケーションを使用しています。Unity3D unity3dで実行時にGameObjectsをインスタンス化して破棄しようとしています。私は統一されたばかりで、現在は飛行機にオブジェクトを配置し、タッチでそのGameObjectの回転操作や移動操作を実行しようとしているGoogleのプロジェクトタンゴに取り組んでいます。Unity3D - EventSystemを使用してAndroidで現在選択されているゲームオブジェクトを取得できません。

3DオブジェクトにFBXファイルを使用し、イベントアップデートを利用するためにイベントシステムを使用しています。飛行機を見つけて飛行機に置くのは、彼らがdocsで利用可能な明確なチュートリアルを持っているからです。しかし、今私はタッチで選択されたGameObjectを取得したい。

インポートしたfbxオブジェクトのプレハブを作成し、Box Colliderを追加しました。その後、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の底から第4行を見てください。これは、初期化されたゲームオブジェクトをクリックするときです。その行の後には何も印刷しません。それはTouch.Endedの状態で書かれたコードを直接印刷します。

SCENE私の知る限り screenshot of the scene

+1

あなたはほとんど接近していました。 'Physics Raycater'は' IPointerDownHandler、IPointerClickHandler、 IPointerUpHandler、IPointerExitHandler、IPointerEnterHandler、 IBeginDragHandler、IDragHandler、IEndDragHandler'のような 'EventSystems'と連動します。詳細については、複製された質問を参照してください。 – Programmer

+0

ありがとうございます。私は試してみて、結果を知ってもらいます:) –

答えて

0

スクリーンショットを追加し、 EventSystem.current.currentSelectedGameObjectは、それが明示的にin the documentationが指定されていないにもかかわらず、UIオブジェクトを参照することができます。あなたがインスタンス化しているのは、UIオブジェクトではなく、シーンの通常のゲームオブジェクトです。インスペクタービュー全体が表示されませんが、インスタンス化中にGameObjectの親としてCanvasのTransformを設定していないことがわかります。

SceneのGameObjectへの参照を取得するには、インスタンス化中にGameObjectへの参照を格納するか、取得するためにタッチの位置に基づいてRaycastを実行する必要があります。この方法は、達成したいことに依存します。

+0

あなたの答えから、私はインスタンス化しているgameobjectを取得できないはずです。しかし、私がインスタンス化した私のgameobjectに触れると、私は真実になります。上のlogcatを参照してください。私は 'isPointingOverGameObject'から真実になります。だから私はそのゲームオブジェクトを検出していると確信しています。しかし、ここで問題となるのは、特定のアクションを実行するためにオブジェクトを取得できないということだけです。 私はあなたの答えから得ることができるもう一つの可能​​性もあります。あなたは、キャンバスなどの中にゲームオブジェクトをインスタンス化して、そのオブジェクトのインスタンスを取得できるようにする必要があると言っていますか? –

+1

はい、私はそれがUIオブジェクトでない限り、 'EventSystem.current.currentSelectedGameObject'を通してアクセスすることはできないと思います。私は、 "InventoryItemDisplay(Clone)"はUIオブジェクトだと仮定しますか?それはなぜログで見ることができるのかを説明しますが、後でインスタンス化するものではありません。また、あなたの編集に関して、 'EventSystem.current.IsPointerOverGameObject(fingerId)'はインスタンス化しているGameObjectの背後にUIオブジェクトがある場合にtrueを返すことができます。実行時に実際に何が起こるかのイメージがないので、私は確信していません。そのため、シーン内に他のオブジェクトの種類が何か分かりません。 – Kasperi

+0

私はビューのスクリーンショットを追加して、何がうまくいかないかを確認できました。画面には、キティが床に座っていることを除いて他のゲームオブジェクトはありません。私は子猫に触れると真の価値を得ていますが、私はあなたがログで見ることができるようにキティgameobjectを得ることができません。それは、 'EventSystem.current.currentSelectedGameObject'を使って現在選択されているゲームオブジェクトを取得しようとすると、コード内でそれ以上進行しません。 –

関連する問題