美文网首页
Unity+Hololens2+WorldAnchor实现锚点锁

Unity+Hololens2+WorldAnchor实现锚点锁

作者: 玄策丶 | 来源:发表于2023-03-05 15:40 被阅读0次

本篇使用旧版Unity(2019)的WorldAnchor功能实现将物体锁定于虚拟空间的特定位置,多个物体可分别锁定锚点位置。
官方文档:https://learn.microsoft.com/zh-cn/windows/mixed-reality/develop/unity/spatial-anchors-in-unity?tabs=worldanchor

一、新建空物体挂载脚本WorldAnchorManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.WSA.Persistence;

public class WorldAnchorManager : MonoBehaviour
{
    public static WorldAnchorManager Instance;
    public bool canChangeAnchor;        //控制改变物体锚点的开关
    public WorldAnchorStore anchorStore;//保存锚点的库
    public string[] ids;

    void Awake()
    {
        Instance = this;
        //加载世界锚点
        WorldAnchorStore.GetAsync(StoreLoaded);
    }
    private void StoreLoaded(WorldAnchorStore store)
    {
        //读取所有已保存的空间锚
        anchorStore = store;
        ids = anchorStore.GetAllIds();
    }
    public void ChangeAnchor()
    {
        canChangeAnchor = !canChangeAnchor;
    }
}

二、在物体上添加组件WorldAnchor

image.png

三、在物体上添加WorldAnchorTest

将RemoveAnchor和AddAnchor方法注册到物体MRTK的操作脚本上

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.XR.WSA;

public class WorldAnchorTest : MonoBehaviour
{

    void Start()
    {
        Invoke("LoadAnchor", .1f);
    }

    void LoadAnchor()
    {
        if (WorldAnchorManager.Instance.ids.Contains(transform.name))
        {
            WorldAnchorManager.Instance.anchorStore.Load(transform.name, gameObject);
        }
    }

    /// <summary>
    /// 添加世界锚点
    /// </summary>
    public void AddAnchor()
    {
        if (!WorldAnchorManager.Instance.canChangeAnchor)
        {
            return;
        }

        Debug.Log("添加锚点");

        if (WorldAnchorManager.Instance.anchorStore == null)
        {
            return;
        }

        WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();
        if (anchor.isLocated)
        {
            WorldAnchorManager.Instance.anchorStore.Save(transform.name, anchor);
        }
        else
        {
            anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
        }
    }
    void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
    {
        if (located)
        {
            WorldAnchorManager.Instance.anchorStore.Save(transform.name, self);
            //取消事件监听
            self.OnTrackingChanged -= Anchor_OnTrackingChanged;
        }
    }

    /// <summary>
    /// 删除世界锚点
    /// </summary>
    public void RemoveAnchor()
    {
        if (!WorldAnchorManager.Instance.canChangeAnchor)
        {
            return;
        }

        Debug.Log("删除锚点");
        DestroyImmediate(gameObject.GetComponent<WorldAnchor>());

        if (WorldAnchorManager.Instance.anchorStore.GetAllIds().Contains(transform.name))
        {
            WorldAnchorManager.Instance.anchorStore.Delete(transform.name);
        }
    }
}
image.png
1.png
2.png

四、结语

这样就可以通过操作bool值canChangeAnchor的开关来控制是否可以移动物体添加锚点(因为有WorldAnchor存在时,物体是不可以进行操作的)。
当canChangeAnchor为flase时不可以移动操作物体(将物体锁定在场景中的某个位置)。
当canChangeAnchor为true时可以移动操作物体,并在操作结束后改变锚点的位置。

相关文章

  • iview 锚点爬坑 && vue 锚点

    在vue中实现锚点定位效果 iview的锚点 原生锚点实现 iview爬坑点 且需要把滚动容器的id或者class...

  • 2019-07-02 overflow 与锚点定位

    overflow 与锚点定位 锚点,通俗点的解释就是可以让页面定位到某个位置的点。 基于URL 地址的锚链实现锚点...

  • react实现锚点

    实现锚点滚动,不要使用 标签,会引发路由跳转。我们使用H5的新增API,scrollToAnchor 以前使用 标...

  • 锚点链接实现

    用到onpagescroll方法获取页面的滚动高度和获取元素距离页面顶部的高度。

  • 【a标签的锚点技术】

    锚点技术 锚点:指的是一个网页上的某个“定位位置”。 锚点的实现:需要使用至少2次a标签来配合完成。 其基本形式如...

  • Markdown 语法介绍

    Markdown 语法介绍 目录 [TOC]简书不支持,可用锚点实现 标题 锚点 点击跳转 引用 列表 列表项目标...

  • HTML5初识

    1.锚点 通过超链接可实现页内跳转,在地址栏上会拼接上#targetId。 锚点测试 说明: a: a 元素 (...

  • html css js 基础五(锚点)

    利用锚点可以实现点击某个位置,显示它对应的位置的文字或者图片,实现类似于定位的效果,锚点用的是id属性; 这种效果...

  • html 锚点三种实现方法

    在网页中经常用到锚点,特别是在比较长的页面中锚点的使用会增加用户体验,现在php中文网介绍html 锚点三种实现方...

  • scroll-anchor

    介绍 实现点击一个锚点(导航),跳转到对应的区块;容器滚动时,对应的锚点高亮;项目演示地址http://admin...

网友评论

      本文标题:Unity+Hololens2+WorldAnchor实现锚点锁

      本文链接:https://www.haomeiwen.com/subject/afzuldtx.html