/// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The anchor Id, or empty string.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="worldPosition">World position.</param>
    /// <param name="worldRotation">World rotation.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, Vector3 worldPosition, Quaternion worldRotation)
    {
        string sAnchorId = string.Empty;

        if (!isInitialized || !arManager)
        {
            return(sAnchorId);
        }

        GameObject anchorObj = new GameObject();

        //GameObject anchorObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        sAnchorId      = System.Guid.NewGuid().ToString();
        anchorObj.name = sAnchorId;

        anchorObj.transform.position = worldPosition;
        anchorObj.transform.rotation = worldRotation;
        //anchorObj.transform.localScale = new Vector3(0.1f, 0.2f, 0.1f);  // for debug only

        WorldAnchor anchor = anchorObj.AddComponent <WorldAnchor>();

        anchor.OnTrackingChanged += Anchor_OnTrackingChanged;

        // don't destroy it accross scenes
        DontDestroyOnLoad(anchorObj);

        if (gameObj)
        {
            gameObj.transform.SetParent(anchorObj.transform, true);
            gameObj.transform.localPosition = Vector3.zero;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        arData.allAnchorsDict[sAnchorId] = new List <GameObject>();

        if (gameObj)
        {
            arData.allAnchorsDict[sAnchorId].Add(gameObj);
        }

        return(sAnchorId);
    }
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The anchor Id, or empty string.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="worldPosition">World position.</param>
    /// <param name="worldRotation">World rotation.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, Vector3 worldPosition, Quaternion worldRotation)
    {
        string sAnchorId = string.Empty;

        if (!isInitialized || !arManager)
        {
            return(sAnchorId);
        }

        GameObject anchorObj = new GameObject();

        //GameObject anchorObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        anchorObj.transform.position = worldPosition;
        anchorObj.transform.rotation = worldRotation;
        //anchorObj.transform.localScale = new Vector3(0.1f, 0.2f, 0.1f);  // for debug only

        UnityARUserAnchorData anchorData = UnityARSessionNativeInterface.GetARSessionNativeInterface().AddUserAnchorFromGameObject(anchorObj);

        sAnchorId = anchorData.identifierStr;

        anchorObj.name = sAnchorId;
        DontDestroyOnLoad(anchorObj);          // don't destroy it accross scenes

        if (gameObj)
        {
            gameObj.transform.SetParent(anchorObj.transform, true);
            gameObj.transform.localPosition = Vector3.zero;
            gameObj.transform.localRotation = Quaternion.identity;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        arData.allAnchorsDict[sAnchorId] = new List <GameObject>();

        if (gameObj)
        {
            arData.allAnchorsDict[sAnchorId].Add(gameObj);
        }

        return(sAnchorId);
    }
Пример #3
0
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The game object to world.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="hit">Trackable hit.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, MultiARInterop.TrackableHit hit)
    {
        string anchorId = string.Empty;

        if (hit.psObject != null && hit.psObject is TrackableHit)
        {
            // valid anchor - attach the tracked plane
            TrackableHit intHit = (TrackableHit)hit.psObject;
            Anchor       anchor = intHit.Trackable.CreateAnchor(intHit.Pose);
            if (anchor == null)
            {
                return(string.Empty);
            }

            anchorId = anchor.m_NativeHandle.ToString();
            DontDestroyOnLoad(anchor.gameObject);              // don't destroy it accross scenes

            if (gameObj)
            {
                gameObj.transform.SetParent(anchor.transform, true);
                gameObj.transform.localPosition = Vector3.zero;
                gameObj.transform.localRotation = Quaternion.identity;
            }

            MultiARInterop.MultiARData arData = arManager.GetARData();
            arData.allAnchorsDict[anchorId] = new List <GameObject>();

            if (gameObj)
            {
                arData.allAnchorsDict[anchorId].Add(gameObj);
            }
        }
        else
        {
            anchorId = AnchorGameObjectToWorld(gameObj, hit.point, hit.rotation);
        }

        return(anchorId);
    }