でAndroidアプリケーションを使用しています。Unity3D unity3dで実行時にGameObjectsをインスタンス化して破棄しようとしています。私は統一されたばかりで、現在は飛行機にオブジェクトを配置し、タッチでそのGameObjectの回転操作や移動操作を実行しようとしているGoogleのプロジェクトタンゴに取り組んでいます。Unity3D - EventSystemを使用してAndroidで現在選択されているゲームオブジェクトを取得できません。
3DオブジェクトにFBXファイルを使用し、イベントアップデートを利用するためにイベントシステムを使用しています。飛行機を見つけて飛行機に置くのは、彼らがdocsで利用可能な明確なチュートリアルを持っているからです。しかし、今私はタッチで選択されたGameObjectを取得したい。
インポートしたfbxオブジェクトのプレハブを作成し、Box Colliderを追加しました。その後、Physics RaycaterをTangoカメラに追加し、EventSystem GameObjectを作成し、実行時にタッチイベントを取得しました。添付の画像を参照してください。
私のスクリプトは、この(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
の状態で書かれたコードを直接印刷します。
あなたはほとんど接近していました。 'Physics Raycater'は' IPointerDownHandler、IPointerClickHandler、 IPointerUpHandler、IPointerExitHandler、IPointerEnterHandler、 IBeginDragHandler、IDragHandler、IEndDragHandler'のような 'EventSystems'と連動します。詳細については、複製された質問を参照してください。 – Programmer
ありがとうございます。私は試してみて、結果を知ってもらいます:) –