【问题标题】:how to add a button in AR application如何在 AR 应用程序中添加按钮
【发布时间】:2020-04-10 12:12:46
【问题描述】:

有一个名为 spawnArobject 的函数,它可以生成我的 Ar 模型,但我想制作 2 个按钮来生成不同的 3D 模型,我该怎么做,并且在单击按钮后将其删除,这样就可以了' t 干扰模型

        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 这是整个项目的链接,我想在这个项目中进行更改。 提前谢谢你

【问题讨论】:

    标签: c# unity3d augmented-reality arcore


    【解决方案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>
    

    然后您可以像访问代码中的普通按钮一样访问 then:

     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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      相关资源
      最近更新 更多