как добавить кнопку в приложении AR - PullRequest
0 голосов
/ 10 апреля 2020

есть функция spawnArobject, которая запускает мою Ar-модель, но я хочу сделать 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 это ссылка для всего проекта, в который я хочу внести изменения в этом проекте. СПАСИБО ЗА ПРЕДЕЛА

1 Ответ

0 голосов
/ 20 апреля 2020

Вы можете добавить кнопку к макету так же, как и к обычному макету:

<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

...