在Unity Arkit中,如何在UnityARHitTestExample场景中显示文本,直到找到垂直平面?

时间:2018-09-12 12:41:19

标签: c# unity3d arkit

我试图显示一个文本“扫描表面”,直到它扫描一个平面为止。一旦设备拾取了一个平面,该消息就会消失。为此,我应该在哪里编写代码。我找到了两个实例在UnityARGeneratePlane.cs和UnityARUtility.cs中初始化预制飞机的位置。我在UnityARGeneratePlane.cs中添加了一个Gameobject作为Text,并使用setactive设置为true和false来显示和隐藏文本。方法。如何隐藏文本是我的难题。在检测到飞机时在哪里使用代码隐藏文本?是在UnityARGeneratePlane.cs还是UnityARUtility.cs中。

public class UnityARGeneratePlane : MonoBehaviour
{
    public GameObject planePrefab;
    public GameObject scantxt;
    private UnityARAnchorManager unityARAnchorManager;

    // Use this for initialization
    void Start () {
        unityARAnchorManager = new UnityARAnchorManager();
        UnityARUtility.InitializePlanePrefab (planePrefab);
       scantxt.SetActive(true); //Tried to show the text working when app opens
    }

    void OnDestroy()
    {
        unityARAnchorManager.Destroy ();
        scantxt.SetActive(false); //Here is where I tried to hide the text until a Vertical or Horizontal plane is detected
    }

    void OnGUI()
    {
        IEnumerable<ARPlaneAnchorGameObject> arpags = unityARAnchorManager.GetCurrentPlaneAnchors ();
        foreach(var planeAnchor in arpags)
        {
            //ARPlaneAnchor ap = planeAnchor;
            //GUI.Box (new Rect (100, 100, 800, 60), string.Format ("Center: x:{0}, y:{1}, z:{2}", ap.center.x, ap.center.y, ap.center.z));
            //GUI.Box(new Rect(100, 200, 800, 60), string.Format ("Extent: x:{0}, y:{1}, z:{2}", ap.extent.x, ap.extent.y, ap.extent.z));
        }
    }
}

下面的代码是UnityARUtility.cs

public class UnityARUtility
{
    private MeshCollider meshCollider; //declared to avoid code stripping of class
    private MeshFilter meshFilter; //declared to avoid code stripping of class
    public static GameObject planePrefab = null;
   // public  Text text1;


    public static void InitializePlanePrefab(GameObject go)
    {
        planePrefab = go;
    }





    public static GameObject CreatePlaneInScene(ARPlaneAnchor arPlaneAnchor)
    {
        GameObject plane;
        if (planePrefab != null)
        {
            plane = GameObject.Instantiate(planePrefab); //I dont understand why again Plane prefab is initialized.Other than Generate planes.
        //  text1.text = "Select Any Painting from panel";

        } 
        else {
            plane = new GameObject (); //put in a blank gameObject to get at least a transform to manipulate
        }

        //plane.name = arPlaneAnchor.identifier;

        ARKitPlaneMeshRender apmr = plane.GetComponent<ARKitPlaneMeshRender> ();
        if (apmr != null) {
            apmr.InitiliazeMesh (arPlaneAnchor);
        }

        return UpdatePlaneWithAnchorTransform(plane, arPlaneAnchor);

    }

    public static GameObject UpdatePlaneWithAnchorTransform(GameObject plane, ARPlaneAnchor arPlaneAnchor)
    {

        //do coordinate conversion from ARKit to Unity
        plane.transform.position = UnityARMatrixOps.GetPosition (arPlaneAnchor.transform);
        plane.transform.rotation = UnityARMatrixOps.GetRotation (arPlaneAnchor.transform);

        ARKitPlaneMeshRender apmr = plane.GetComponent<ARKitPlaneMeshRender> ();
        if (apmr != null) {
            apmr.UpdateMesh (arPlaneAnchor);
        }


        MeshFilter mf = plane.GetComponentInChildren<MeshFilter> ();

        if (mf != null) {
            if (apmr == null) {
                //since our plane mesh is actually 10mx10m in the world, we scale it here by 0.1f
                mf.gameObject.transform.localScale = new Vector3 (arPlaneAnchor.extent.x * 0.1f, arPlaneAnchor.extent.y * 0.1f, arPlaneAnchor.extent.z * 0.1f);

                //convert our center position to unity coords
                mf.gameObject.transform.localPosition = new Vector3(arPlaneAnchor.center.x,arPlaneAnchor.center.y, -arPlaneAnchor.center.z);
            }

        }

        return plane;
    }



}

}

0 个答案:

没有答案