Unity 工具类 之 AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存

2024-02-23 04:50

本文主要是介绍Unity 工具类 之 AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Unity 工具类 之 AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存

目录

Unity 工具类 之 AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存

一、引出问题

二、解决方法

三、注意事项

四、效果预览

五、实现步骤

六、附加

鼠标右键交互功能

取消掉UGUI系统默认的鼠标点击交互功能


 

一、引出问题

Unity在使用中,AR和VR开发中,会遇到以下问题

  • 1、首先需要分屏,一般左右两屏幕;

  • 2、交互上一般的点击,由于屏幕的原因(不像手机一样的触击屏幕交互),就不适合点击交互了;

  • 3、Gaze 凝视 交互就应运而生;

  • 4、但是在电脑上开发中,Gaze 交互操作又不是很方便,所以用需要用到 点击交互;

故综上所述,Gaze 凝视交互和点击交互需要并存。

二、解决方法

1、左右两个camera,分别渲染,实现分屏;

2、Canvas 设置为 worldSpace,然后交互camera 设置为 RightCamera(当然根据需要切换也可以);

3、GazeEyeRaycaster 实现 Gaze 交互,且不干扰点击 UI 事件;

三、注意事项

1、由于分屏,所以凝视点点不能像之前单屏一样为屏幕的中心,而是左右屏中某个屏的中心( ScreenWidth * 3/4,ScreenHeight * 1/2(右屏幕中心) 或者 ScreenWidth * 1/4,ScreenHeight * 1/2 (左屏幕中心))

2、这里点击交互以右屏camera交互为主(也可以根据需要动态修改);

四、效果预览

五、实现步骤

1、打开Unity,新建场景,布局如下

2、主要脚本

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections.Generic;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using XRSettings = UnityEngine.VR.VRSettings;
#endif[System.Serializable]
public class FloatUnityEvent : UnityEvent<float> { }public class GazeEyeRaycaster : MonoBehaviour
{[SerializeField, Tooltip("In seconds")]float m_loadingTime;[SerializeField]FloatUnityEvent m_onLoad;float m_elapsedTime = 0;// Prevents loop over the same selectableSelectable m_excluded; Selectable m_currentSelectable;RaycastResult m_currentRaycastResult;IPointerClickHandler m_clickHandler;IDragHandler m_dragHandler;EventSystem m_eventSystem;PointerEventData m_pointerEvent;private void Start(){m_eventSystem = EventSystem.current;m_pointerEvent = new PointerEventData(m_eventSystem);m_pointerEvent.button = PointerEventData.InputButton.Left;}void Update(){// 因为要分屏所以让 m_pointerEvent.position 在右边屏幕中心(即为 width * 3/4,height * 1/2)// Set pointer positionm_pointerEvent.position =
#if UNITY_EDITORnew Vector2(Screen.width * 3 / 4, Screen.height / 2);
#elsenew Vector2(XRSettings.eyeTextureWidth * 3 / 4, XRSettings.eyeTextureHeight / 2);
#endifList<RaycastResult> raycastResults = new List<RaycastResult>();m_eventSystem.RaycastAll(m_pointerEvent, raycastResults);// Detect selectableif (raycastResults.Count > 0){foreach(var result in raycastResults){var newSelectable = result.gameObject.GetComponentInParent<Selectable>();if (newSelectable){if(newSelectable != m_excluded && newSelectable != m_currentSelectable){Select(newSelectable);m_currentRaycastResult = result;}break;}}}else{if(m_currentSelectable || m_excluded){Select(null, null);}}// Target is being activatingif (m_currentSelectable){m_elapsedTime += Time.deltaTime;m_onLoad.Invoke(m_elapsedTime / m_loadingTime);if (m_elapsedTime > m_loadingTime){if (m_clickHandler != null){m_clickHandler.OnPointerClick(m_pointerEvent);Select(null, m_currentSelectable);}else if (m_dragHandler != null){m_pointerEvent.pointerPressRaycast = m_currentRaycastResult;m_dragHandler.OnDrag(m_pointerEvent);}}}}void Select(Selectable s, Selectable exclude = null){m_excluded = exclude;if (m_currentSelectable)m_currentSelectable.OnPointerExit(m_pointerEvent);m_currentSelectable = s;if (m_currentSelectable){m_currentSelectable.OnPointerEnter(m_pointerEvent);m_clickHandler = m_currentSelectable.GetComponent<IPointerClickHandler>();m_dragHandler = m_currentSelectable.GetComponent<IDragHandler>();}m_elapsedTime = 0;m_onLoad.Invoke(m_elapsedTime / m_loadingTime);}
}

3、运行场景效果

4、工程场景下载

UnityGazeAndClickUI AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存 点击下载

六、附加

鼠标右键交互功能

1、该脚本保留之前的凝视计时确认操作,同事添加凝视点击交互,具体见脚本

public class GazeEyeRaycasterAndInteraactiveType : MonoBehaviour{public enum InterActiveType { None,   //   无交互TimeCounter,    // 计时交互LeftClick,      // 右键点击}[SerializeField, Tooltip("In seconds")]float m_loadingTime;[SerializeField]FloatUnityEvent m_onLoad;[SerializeField]InterActiveType mCurInterActiveType = InterActiveType.LeftClick;float m_elapsedTime = 0;// Prevents loop over the same selectableSelectable m_excluded;Selectable m_currentSelectable;RaycastResult m_currentRaycastResult;IPointerClickHandler m_clickHandler;IDragHandler m_dragHandler;EventSystem m_eventSystem;PointerEventData m_pointerEvent;private void Start(){m_eventSystem = EventSystem.current;m_pointerEvent = new PointerEventData(m_eventSystem);m_pointerEvent.button = PointerEventData.InputButton.Left;}void Update(){// 因为要分屏所以让 m_pointerEvent.position 在右边屏幕中心(即为 width * 3/4,height * 1/2)// Set pointer positionm_pointerEvent.position =
#if UNITY_EDITORnew Vector2(Screen.width * 3 / 4, Screen.height / 2);
#elsenew Vector2(XRSettings.eyeTextureWidth * 3 / 4, XRSettings.eyeTextureHeight / 2);
#endifList<RaycastResult> raycastResults = new List<RaycastResult>();m_eventSystem.RaycastAll(m_pointerEvent, raycastResults);// Detect selectableif (raycastResults.Count > 0){foreach (var result in raycastResults){var newSelectable = result.gameObject.GetComponentInParent<Selectable>();if (newSelectable){if (newSelectable != m_excluded && newSelectable != m_currentSelectable){Select(newSelectable);m_currentRaycastResult = result;}break;}}}else{if (m_currentSelectable || m_excluded){Select(null, null);}}// Target is being activatingif (m_currentSelectable){InteractiveHandle(mCurInterActiveType);}}void InteractiveHandle(InterActiveType interActiveType) {if (interActiveType == InterActiveType.TimeCounter){m_elapsedTime += Time.deltaTime;m_onLoad.Invoke(m_elapsedTime / m_loadingTime);if (m_elapsedTime > m_loadingTime){ClickAndDragUIHandle();}}else if (interActiveType == InterActiveType.LeftClick){if (Input.GetMouseButton(0)){ClickAndDragUIHandle();}}}void ClickAndDragUIHandle() {if (m_clickHandler != null){m_clickHandler.OnPointerClick(m_pointerEvent);Select(null, m_currentSelectable);}else if (m_dragHandler != null){m_pointerEvent.pointerPressRaycast = m_currentRaycastResult;m_dragHandler.OnDrag(m_pointerEvent);}}void Select(Selectable s, Selectable exclude = null){m_excluded = exclude;if (m_currentSelectable)m_currentSelectable.OnPointerExit(m_pointerEvent);m_currentSelectable = s;if (m_currentSelectable){m_currentSelectable.OnPointerEnter(m_pointerEvent);m_clickHandler = m_currentSelectable.GetComponent<IPointerClickHandler>();m_dragHandler = m_currentSelectable.GetComponent<IDragHandler>();}m_elapsedTime = 0;m_onLoad.Invoke(m_elapsedTime / m_loadingTime);}}

取消掉UGUI系统默认的鼠标点击交互功能

 

这篇关于Unity 工具类 之 AR/VR 分屏 Gaze 凝视 和 Click 点击 UI 交互并存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737568

相关文章

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

基于Python打造一个全能文本处理工具

《基于Python打造一个全能文本处理工具》:本文主要介绍一个基于Python+Tkinter开发的全功能本地化文本处理工具,它不仅具备基础的格式转换功能,更集成了中文特色处理等实用功能,有需要的... 目录1. 概述:当文本处理遇上python图形界面2. 功能全景图:六大核心模块解析3.运行效果4. 相

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优