PICO+Unity MR空间锚点

embedded/2024/11/15 6:03:43/

官方链接:空间锚点 | PICO 开发者平台

注意:该功能只能打包成APK在PICO 4 Ultra上真机运行,无法通过串流或PICO developer center在PC上运行。使用之前要开启视频透视。

Inspector 窗口中的 PXR_Manager (Script) 面板上,勾选 Spatial Anchor 选框,为应用开启空间锚点能力。然后,你可以调用空间锚点相关接口,在应用内实现空间锚点功能。

新建一个空物体名为SpatialAnchor,添加SpatialAnchor组件(指定地方放置物体)、SeethroughManager代码(开启透视)

编写代码SpatialAnchor

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.XR.PXR;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;public class SpatialAnchor : MonoBehaviour
{public GameObject prerefAnchor;public GameObject anchorPreview;public GameObject firePoint;public Text textPrompt;public Button btnLoadAllAnchors;public Button btnClearAllAnchors;[SerializeField]private InputActionReference rightGrip;public Dictionary<ulong, AnchorInfo> anchorList = new Dictionary<ulong, AnchorInfo>();// Start is called before the first frame updatevoid Start(){btnLoadAllAnchors.onClick.AddListener(OnBtnPressedLoadAllAnchors);btnClearAllAnchors.onClick.AddListener(OnBtnPressedClearAllAnchors);StartSpatialAnchorProvider();}private void OnEnable(){rightGrip.action.started += OnRightGripPressed;rightGrip.action.canceled += OnRightGripReleased;}private void OnDisable(){rightGrip.action.started -= OnRightGripPressed;rightGrip.action.canceled -= OnRightGripReleased;}//called on action.startedprivate void OnRightGripPressed(InputAction.CallbackContext callback){ShowAnchorPreview();}//called on action.releaseprivate void OnRightGripReleased(InputAction.CallbackContext callback){CreateAnchor();}private void ShowAnchorPreview(){//Show anchoranchorPreview.SetActive(true);}private async void StartSpatialAnchorProvider(){var result0 = await PXR_MixedReality.StartSenseDataProvider(PxrSenseDataProviderType.SpatialAnchor);Debug.unityLogger.Log($"StartSenseDataProvider: {result0}");}    private async void CreateAnchor(){anchorPreview.SetActive(false);//Use Spatial Anchor Api to create anchor//This will  trigger AnchorEntityCreatedEventvar result1 = await PXR_MixedReality.CreateSpatialAnchorAsync(firePoint.transform.position, firePoint.transform.rotation);if (result1.result == PxrResult.SUCCESS){GameObject anchorObject = Instantiate(prerefAnchor);anchorObject.SetActive(true);anchorObject.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);anchorObject.transform.rotation = firePoint.transform.rotation;anchorObject.transform.position = firePoint.transform.position;AnchorInfo info = anchorObject.GetComponent<AnchorInfo>();var result2 = await PXR_MixedReality.PersistSpatialAnchorAsync(result1.anchorHandle);if (result2 == PxrResult.SUCCESS){info.ShowSaveIcon(true);}else{info.ShowSaveIcon(false);}anchorList.Add(result1.anchorHandle, info); // 添加到锚点列表}}// 异步加载所有锚点private async void OnBtnPressedLoadAllAnchors(){anchorList.Clear();var result = await PXR_MixedReality.QuerySpatialAnchorAsync(); // 查询所有空间锚点//SetLogInfo("LoadSpatialAnchorAsync:" + result.result.ToString() + result.anchorHandleList.Count); // 记录日志if (result.result == PxrResult.SUCCESS) // 成功查询{int i = 0;foreach (var key in result.anchorHandleList) // 遍历锚点句柄{if (!anchorList.ContainsKey(key)) // 如果锚点列表中不存在该锚点{i++;PXR_MixedReality.LocateAnchor(key, out var position, out var orientation);GameObject anchorObject = Instantiate(prerefAnchor);anchorObject.SetActive(true);anchorObject.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);anchorObject.transform.rotation = orientation;anchorObject.transform.position = position;AnchorInfo anchor = anchorObject.GetComponent<AnchorInfo>(); // 获取锚点组件anchor.SetAnchorHandle(key); // 设置锚点句柄// 定位锚点anchorList.Add(key, anchor); // 添加到锚点列表anchorList[key].ShowSaveIcon(true); // 显示保存图标}else{textPrompt.text = "无法加载:" + i.ToString();}}}else{textPrompt.text = "查询失败...";}}// 异步删除所有锚点private async void OnBtnPressedClearAllAnchors(){List<ulong> keys = anchorList.Keys.ToList();for(int i = 0; i < keys.Count; i++){ulong key = keys[i];await PXR_MixedReality.UnPersistSpatialAnchorAsync(anchorList[key].anchorHandle);textPrompt.text = "正在删除..."+i.ToString();DestroyImmediate(anchorList[key].gameObject);}anchorList.Clear();textPrompt.text = "删除完成";}
}

锚点信息类 

using System.Collections;
using System.Collections.Generic;
using Unity.XR.PXR;
using UnityEngine;
using UnityEngine.UI;public class AnchorInfo : MonoBehaviour
{public Text text;public GameObject savedIcon;[HideInInspector]public ulong anchorHandle;// 设置锚点句柄并更新 UI 显示public void SetAnchorHandle(ulong handle){anchorHandle = handle;text.text = "ID: " + anchorHandle;}// 显示保存图标public void ShowSaveIcon(bool show){savedIcon.SetActive(show);}private void LateUpdate(){// 尝试定位空间锚点var result = PXR_MixedReality.LocateAnchor(anchorHandle, out var position, out var rotation);if (result == PxrResult.SUCCESS){// 如果成功,更新当前对象的位置和旋转transform.position = position;transform.rotation = rotation;}}
}

rightGrip输入赋值 


http://www.ppmy.cn/embedded/136406.html

相关文章

贪心算法习题其三【力扣】【算法学习day.20】

前言 ###我做这类文档一个重要的目的还是给正在学习的大家提供方向&#xff08;例如想要掌握基础用法&#xff0c;该刷哪些题&#xff1f;&#xff09;我的解析也不会做的非常详细&#xff0c;只会提供思路和一些关键点&#xff0c;力扣上的大佬们的题解质量是非常非常高滴&am…

【前端】Svelte:生命周期函数

Svelte 提供了一些强大的生命周期函数&#xff0c;用于管理组件的生命周期。借助这些函数&#xff0c;我们可以在组件的不同状态执行代码&#xff0c;方便控制组件的创建、更新和销毁行为。主要的生命周期函数包括 onMount、beforeUpdate、afterUpdate 和 onDestroy。 本文将通…

Linux设置jar包开机自启

创建启动脚本 在/etc/rc.d/init.d目录下创建**.sh脚本文件&#xff0c;这里起名为jarStartUp.sh 文件中复制以下内容 # 配置jdk的路径&#xff08;jdk的环境变量&#xff0c;可以使用more /etc/profile命令获取copy路径&#xff09; export JAVA_HOME/usr/local/java/jdk1.…

【鉴权】深入探讨 Session:服务器端存储用户状态的机制

目录 引言一、Session 的定义与概念1.1 什么是 Session&#xff1f;1.2 Session 工作流程1.3 Session 工作原理图示 二、Session 的基本结构2.1 Session ID2.2 Session Data 三、Session 的优缺点四、Session 的生命周期与安全性4.1 Session 的生命周期4.1.1 会话创建4.1.2 会话…

JAVA题目笔记(十三) 爬虫

一、网络爬取 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.regex.Matcher; import java.util.regex.Pattern;public class Main {public static v…

Linux(CentOS)yum update -y 事故

CentOS版本&#xff1a;CentOS 7 事情经过&#xff1a; 1、安装好CentOS 7&#xff0c;系统自带JDK8&#xff0c;版本为&#xff1a;1.8.0_181 2、安装好JDK17&#xff0c;版本为&#xff1a;17.0.13 3、为了安装MySQL执行了 yum update -y&#xff08;这个时候不知道该命令的…

数据治理与数据安全的融合路径

数据治理与数据安全的融合路径 在数字化时代&#xff0c;数据已成为企业最宝贵的资产之一。然而&#xff0c;数据的价值不仅在于其数量和质量&#xff0c;更在于如何有效管理和保护这些数据。数据治理与数据安全作为数据管理领域的两大核心支柱&#xff0c;其融合路径对于确保…

革命性AI搜索引擎!ChatGPT最新功能发布,无广告更智能!

文章目录 零、前言一、ChatGPT最新AI搜索引擎功能操作指导实战1:搜索新闻实战2:搜索天气实战3:搜索体育消息 二、感受 零、前言 大人&#xff0c;时代变了。 最强 AI 助力下的无广告搜索引擎终于问世。我们期待已久的这一刻终于到来了&#xff0c;从今天起&#xff0c;ChatGPT…