как да добавите бутон в AR приложение

има функция, наречена spawnArobject, която задейства моя Ar modle, но искам да направя 2 бутона, които дават различен 3D модел за зараждане, как мога да го направя и също така да премахна бутона, след като бъде щракнат, така че да не пречи на модела

        public void _SpawnARObject()
    {
        Touch touch;
        touch = Input.GetTouch(0);
        Debug.Log("touch count is " + Input.touchCount);
        TrackableHit hit;      // Raycast against the location the player touched to search for planes.
        TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
        TrackableHitFlags.FeaturePointWithSurfaceNormal;

        if (touch.phase == TouchPhase.Began)
        {
            Debug.Log("Touch Began");
            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
            {
                if (CurrentNumberOfGameObjects < numberOfGameObjectsAllowed)
                {
                    Debug.Log("Screen Touched");
                    Destroy(ARObject);
                    // Use hit pose and camera pose to check if hittest is from the
                    // back of the plane, if it is, no need to create the anchor.
                    if ((hit.Trackable is DetectedPlane) &&
                        Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                            hit.Pose.rotation * Vector3.up) < 0)
                    {
                        Debug.Log("Hit at back of the current DetectedPlane");
                    }
                    else
                    {

                        ARObject = Instantiate(ARAndroidPrefab, hit.Pose.position, hit.Pose.rotation);// Instantiate Andy model at the hit pose.                                                                                 
                        ARObject.transform.Rotate(0, 0, 0, Space.Self);// Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
                        var anchor = hit.Trackable.CreateAnchor(hit.Pose);
                        ARObject.transform.parent = anchor.transform;
                        CurrentNumberOfGameObjects = CurrentNumberOfGameObjects + 1;

                        // Hide Plane once ARObject is Instantiated 
                        foreach (GameObject Temp in DetectedPlaneGenerator.instance.PLANES) //RK
                        {
                            Temp.SetActive(false);
                        }
                    }

                }

            }

        }

https://github.com/reigngt09/ARCore/tree/master/J_VerticalPlaneDetection това е връзката за целия проект. Искам да направя промени в този проект. БЛАГОДАРЯ ВИ ПРЕДВАРИТЕЛНО


person Tanmay Singh    schedule 10.04.2020    source източник


Отговори (1)


Можете да добавите бутон към оформлението, както бихте направили при нормално оформление:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.amodtech.ar.sceneform.amodapps.lineview.LineViewMainActivity">

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/your_Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:visibility="invisible"
        android:src="@mipmap/ic_launcher" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/your_other_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/blank_button_bottom_right"
        android:layout_margin="5dp"
        android:src="@drawable/ic_baseline_arrow_downward_24px" />


    <fragment
        android:id="@+id/ux_fragment"
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  </RelativeLayout>

</FrameLayout>

След това можете да получите достъп, както бихте направили нормален бутон във вашия код:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Your existing OnCReate code here

        //Add a listener for your button 
        FloatingActionButton yourButtom = findViewById(R.id.your_buttom);
        yourButtom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Add the code you want executed here

                }
            }
        });

        //Add a listener for another button button
        FloatingActionButton yourOtherButton = findViewById(R.id.your_other_button);
        yourOtherButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 //Add the code you want executed here

                }
            }
        });

 }

Можете да зададете бутона видим или невидим в кода според нуждите:

                //When you want to hide the button
                yourButtom.hide()

                //When you want to show it again
                yourButton.Show()

Можете да видите работещ пример на: https://github.com/mickod/LineView

person Mick    schedule 20.04.2020