ARFoundation Shu (b) Basic Configuration

Tip : Select the right side of the directory, you can quickly find what you need

This series of blog Address: Portal

: This program is the basis of the configuration of the project, to facilitate our future development.

 

 

A, SDK environment configuration

Install the necessary SDK.

a、Windows--Packages:

Advanced:Show preview packages

 

b, installation ARFoundation, ARCoreXRPlugin, ARKitXRPlugin.

ARFoundation etc. The default is the current version validated (verified). We unified the above package selection See all versions, select 3.1.0, Install.

 

 

Second, the scene configuration

1, component lifecycle

Level panel to delete the original video cameras, right-XR, add AR the Session Origin , AR the Session .

Both components contain cameras, ARFoundation life cycle and more. We often fall after the functions of the two components of the object.

 

2, to achieve the detection point cloud displayed

A, ARSessionOrigin object added  ARPointCloudManager assembly

This assembly feature is implemented to detect and display the point cloud. But what kind of point cloud? Then we need to step b configured.

B, level right panel XR-AR Default Point Cloud, made the object point cloud preform , delete the object hierarchy panel. The preform assembly assign ARPointCloudManager

 

3, to achieve detection display plane

A, ARSessionOrigin object added  ARPlaneManager  assembly

Similarly, what kind of plane? Step b needs to be configured.

B, level right panel XR-AR Default Plane, the object made flat preform , delete the object hierarchy panel. The preform is assigned to ARPlaneManager  assembly

 DetectionMode: Control test showed a horizontal plane, vertical plane, are detected or not detected.

 

4, resource control

Above we do is to achieve a device to detect a real environment feature points, planar, and flat display point cloud.

Then we also need to control these detected point cloud, plane, so they hide or display.

So we write a script that provides Public disclosed methods, help us control these objects .

This script is free to hang in what will do. Script functions are as follows:

  • Offers: Enabling and disabling the detection plane
  • Provided: display plane detected by the Hide
  • Yield: AR current session is in progress, and tracked (i.e., the device can determine its current position and orientation in the world)
  • Automatic operation: Check the equipment operating environment, can support ARFoundation
  • Automatic operation: contains one power-saving strategy, identify the target when the device is not found, allow the screen to dim after a period of time last active
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;

public class Skode_ARFManager : MonoBehaviour
{
    #region Public Parameters

    public static Skode_ARFManager ins;

    #endregion


    #region Private Parameters

    ARPlaneManager m_ARPlaneManager;

    /// <summary>
    /// 当前识别出的平面
    /// </summary>
    List<ARPlane> detectPlanes = new List<ARPlane>();

    /// <summary>
    /// 当前是否要显示平面
    /// </summary>
    bool isShowPlane = true;

    #endregion


    #region MonoBehaviour CallBacks

    private void Awake()
    {
        ins = this;

        m_ARPlaneManager = FindObjectOfType<ARPlaneManager>();
    }

    void Start()
    {
        CheckDevice();

        m_ARPlaneManager.planesChanged += OnPlaneChanged;
    }

    private void Update()
    {
        SaveElePolicy();
    }

    void OnDisable()
    {
        m_ARPlaneManager.planesChanged -= OnPlaneChanged;
    }

    #endregion


    #region Public Methods

    // 启用与禁用平面检测
    // 程序默认启用,启用时一直不停地检测平面。关闭时则不会再检测新平面了。
    public void Skode_PlaneDetectionContro(bool value)
    {
        m_ARPlaneManager.enabled = value;
        if (m_ARPlaneManager.enabled)
        {
            print("已启用平面检测");
        }
        else
        {
            print("已禁用平面检测");
        }
    }

    // 显示与隐藏检测到的平面
    public void Skode_PlaneContro(bool value)
    {
        isShowPlane = value;

        for (int i = detectPlanes.Count - 1; i >= 0; i--)
        {
            if (detectPlanes[i] == null || detectPlanes[i].gameObject == null)
                detectPlanes.Remove(detectPlanes[i]);
            else
                detectPlanes[i].gameObject.SetActive(value);
        }
    }

    /// <summary>
    /// 得到当前AR会话是否正在运行,并被跟踪(即,该设备能够确定其在世界上的位置和方向)。
    /// </summary>
    public bool Skode_IsTracking()
    {
        bool isTracking = false;

        if (ARSession.state == ARSessionState.SessionTracking)
        {
            isTracking = true;
        }

        return isTracking;
    }

    #endregion


    #region Private Methods

    //在ARFoundation新发现平面时,将平面添加进列表里,便于我们控制这些平面
    void OnPlaneChanged(ARPlanesChangedEventArgs arg)
    {
        for (int i = 0; i < arg.added.Count; i++)
        {
            detectPlanes.Add(arg.added[i]);
            arg.added[i].gameObject.SetActive(isShowPlane);
        }
    }

    //检查设备运行环境
    void CheckDevice()
    {
        if (ARSession.state == ARSessionState.NeedsInstall)
        {
            ShowAndroidToastMessage("AR is supported, but requires an additional install. .");
            Invoke("Quit", 1);
        }
        else if (ARSession.state == ARSessionState.Ready)
        {
            Debug.Log("AR is supported and ready.");
        }
        else if (ARSession.state == ARSessionState.Unsupported)
        {
            ShowAndroidToastMessage("AR is not supported on the current device.");
            Invoke("Quit", 1);
        }
    }

    void ShowAndroidToastMessage(string message)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        if (unityActivity != null)
        {
            AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
            unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity, message, 0);
                toastObject.Call("show");
            }));
        }
    }

    void Quit()
    {
        Application.Quit();
    }

    /// <summary>
    /// 一种省电设置,当设备没找到识别目标,允许屏幕在最后激活一段时间后变暗
    /// </summary>
    void SaveElePolicy()
    {
        if (ARSession.state != ARSessionState.SessionTracking)
        {
            const int lostTrackingSleepTimeout = 15;
            Screen.sleepTimeout = lostTrackingSleepTimeout;
        }
        else
        {
            Screen.sleepTimeout = SleepTimeout.NeverSleep;
        }
    }

    #endregion
}

 

 

End of this section, the next section we talk about placing objects

Enjoy~

Published 329 original articles · won praise 85 · views 190 000 +

Guess you like

Origin blog.csdn.net/weixin_38239050/article/details/105094215