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);
    }
}
相关推荐
一个处女座的程序猿O(∩_∩)O2 小时前
实现 AI 流式响应:从等待到实时交互的技术解析
网络·人工智能·交互
这张生成的图像能检测吗2 天前
(论文速读)InteractVLM: 基于2D基础模型的3D交互推理
人工智能·计算机视觉·交互·生成模型·图像生成·视觉语言模型·3d重建
周杰伦_Jay3 天前
【常用设计模式全解析】创建型模式(聚焦对象创建机制)、结构型模式(优化类与对象的组合关系)、行为型模式(规范对象间的交互行为)
设计模式·架构·开源·交互·1024程序员节
MoonBit月兔3 天前
MoonBit Pearls Vol.12:初探 MoonBit 中的 JavaScript 交互
开发语言·javascript·数据库·交互·moonbit
MetaverseMan4 天前
Web3j 中使用 Transaction 类进行以太坊交互的核心方法
web3·交互
I烟雨云渊T5 天前
iOS原生与Flutter的交互编程
flutter·ios·交互
Larry_Yanan5 天前
QML学习笔记(四十六)QML与C++交互:Q_PROPERTY宏映射
c++·笔记·qt·学习·ui·交互
迎風吹頭髮5 天前
Linux内核架构浅谈60-Linux块设备驱动:请求队列与BIO结构的交互流程
linux·运维·交互
Larry_Yanan6 天前
QML学习笔记(四十四)QML与C++交互:对QML对象设置objectName
开发语言·c++·笔记·qt·学习·ui·交互
Larry_Yanan6 天前
QML学习笔记(四十五)QML与C++交互:信号槽的双向实现
c++·笔记·qt·学习·ui·交互