2024-04-08 NO.6 Quest3 自定义交互事件

文章目录

  • [1 交互事件------更改 Cube 颜色](#1 交互事件——更改 Cube 颜色)
  • [2 交互事件------创建 Cube](#2 交互事件——创建 Cube)
    • [2.1 非代码方式](#2.1 非代码方式)
    • [2.2 代码方式](#2.2 代码方式)

在开始操作前,我们导入上次操作的场景,相关介绍在 《2024-04-08 NO.5 Quest3 手势追踪进行 UI 交互-CSDN博客》 文章中。

1 交互事件------更改 Cube 颜色

(1)在场景中创建一个方块 Cube,修改其缩放大小。

(2)在 Button 上添加脚本 "Interactable Unity Event Wrapper",并关联如下引用。

  • Interactable View <-- "Poke Interactable" 脚本。

(3)在 When Hover() 中添加事件,并关联上 Cube 物体。选择 MeshRenderer > Material material。并将材质球 Red 进行关联。

(4)同样的方式对 When Select() 进行操作,关联 Blue 材质。对 When Unselect() 关联 Default-Material。

​ 此时运行程序,即可点击按钮实现更改 Cube 颜色的功能。

2 交互事件------创建 Cube

2.1 非代码方式

​ 首先复制一份按钮。

(1)创建如下脚本 ItemSpawner.cs。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemSpawner : MonoBehaviour
{
    public GameObject spawnObj;
    public Transform  spawnPoint;

    // Start is called before the first frame update
    void Start() { }

    // Update is called once per frame
    void Update() { }

    public void SpawnItem() {
        Object.Instantiate(spawnObj, spawnPoint.position, spawnPoint.rotation);
    }
}

(2)将 ItemSpawner.cs 挂载到 Button (2) 上。同时创建一个空物体,用于决定新 Cube 的生成位置,并赋值相关引用。

(3)在 "Interactable Unity Event Wrapper" 脚本中,新建 When Select() 事件,关联 "ItemSpawner" 脚本,选择 ItemSpawner > SpawnItem。

​ 此时运行程序,即可点击按钮实现创建新 Cube 的功能。

2.2 代码方式

​ 将 ItemSpawner.cs 脚本内容改为如下即可。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using Oculus.Interaction;
using UnityEngine;

public class ItemSpawner : MonoBehaviour
{
    public GameObject spawnObj;
    public Transform  spawnPoint;

    private InteractableUnityEventWrapper _eventWrapper;

    // Start is called before the first frame update
    void Start() {
        _eventWrapper = GetComponent<InteractableUnityEventWrapper>();
        _eventWrapper.WhenSelect.AddListener(SpawnItem);
    }

    // Update is called once per frame
    void Update() { }

    public void SpawnItem() {
        Object.Instantiate(spawnObj, spawnPoint.position, spawnPoint.rotation);
    }
}
相关推荐
bao_lanlan6 小时前
兰亭妙微:用系统化思维重构智能座舱 UI 体验
ui·设计模式·信息可视化·人机交互·交互·ux·外观模式
Coding的叶子11 小时前
React Flow 节点事件处理实战:鼠标 / 键盘事件全解析(含节点交互代码示例)
react.js·交互·鼠标事件·fgai·react agent
MARS_AI_16 小时前
智能呼叫系统中的NLP意图理解:核心技术解析与实战
人工智能·自然语言处理·nlp·交互·信息与通信
Blossom.11819 小时前
Web3.0:互联网的去中心化未来
人工智能·驱动开发·深度学习·web3·去中心化·区块链·交互
六bring个六1 天前
文件系统交互实现
开发语言·c++·qt·交互
zhz52141 天前
AI数字人融合VR全景:开启未来营销与交互新篇章
人工智能·ai·交互·vr·ai编程·智能体
若愚67921 天前
前端取经路——量子UI:响应式交互新范式
前端·ui·交互
-SGlow-2 天前
Linux相关概念和易错知识点(40)(HTML资源交互、网页管理、搜索引擎)
linux·运维·服务器·网络·html·交互
Anchenry2 天前
【QGIS二次开发】地图显示与交互-03
microsoft·交互