Unity AR开发 | Sliders滑块交互界面设计

Sliders滑块交互界面设计

1. 简介

点击参考教程链接 可产看原文教程

原理就是应用Oculus Integration包里面的OVRInputModule.cs脚本。

功能示意图:
在这里插入图片描述

为实现上述功能,共创建三个脚本:

- ControllerInfo
- ControllerPointer
- SetUITransformRay

2. 介绍

ControllerInfo 脚本

Controller info is just a class that makes sure that scripts always have the correct information.

可以理解为获得手柄信息,建立trackingSpace的连接

using UnityEngine;
using static OVRInput;

public class ControllerInfo : MonoBehaviour {
    [SerializeField]
    private Transform trackingSpace;
    public static Transform TRACKING_SPACE;
    public static Controller CONTROLLER;
    public static GameObject CONTROLLER_DATA_FOR_RAYS;

    private void Start () {
        TRACKING_SPACE = trackingSpace;
    }

    private void Update()
    {
        CONTROLLER = ((GetConnectedControllers() & (Controller.LTrackedRemote | Controller.RTrackedRemote) & Controller.LTrackedRemote) != Controller.None) ? Controller.LTrackedRemote : Controller.RTrackedRemote;
    }
}

ControllerPointer 脚本

ControllerPointer draws a line from the controller. This represents the way the controller is pointing. Add this to a LineRenderer.

这个脚本的作用是使手柄射出一条射线,这样可以表示手柄正在指哪里。
注意,这个脚本是加在LineRenderer下。

using UnityEngine;
using UnityEngine.EventSystems;
using static OVRInput;

[RequireComponent(typeof(LineRenderer))]
public class ControllerPointer : MonoBehaviour
{
    [SerializeField]
    private SetUITransformRay uiRays;
    private LineRenderer pointerLine;
    private GameObject tempPointerVals;

    private void Start()
    {
        tempPointerVals = new GameObject();
        tempPointerVals.transform.parent = transform;
        tempPointerVals.name = "tempPointerVals";
        pointerLine = gameObject.GetComponent<LineRenderer>();
        pointerLine.useWorldSpace = true;

        ControllerInfo.CONTROLLER_DATA_FOR_RAYS = tempPointerVals;
        uiRays.SetUIRays();
    }

    private void LateUpdate()
    {
        Quaternion rotation = GetLocalControllerRotation(ControllerInfo.CONTROLLER);
        Vector3 position = GetLocalControllerPosition(ControllerInfo.CONTROLLER);
        Vector3 pointerOrigin = ControllerInfo.TRACKING_SPACE.position + position;
        Vector3 pointerProjectedOrientation = ControllerInfo.TRACKING_SPACE.position + (rotation * Vector3.forward);
        PointerEventData pointerData = new PointerEventData(EventSystem.current);
        Vector3 pointerDrawStart = pointerOrigin - pointerProjectedOrientation * 0.05f;
        Vector3 pointerDrawEnd = pointerOrigin + pointerProjectedOrientation * 500.0f;
        pointerLine.SetPosition(0, pointerDrawStart);
        pointerLine.SetPosition(1, pointerDrawEnd);

        tempPointerVals.transform.position = pointerDrawStart;
        tempPointerVals.transform.rotation = rotation;
    }
}

SetUITransformRay 脚本

SetUITransformRay will automatically set the controller for the OVRInputModule ray. It is required as normally you have two controllers in your scene; one for left and one for right. See the full method below for more info on how to set this up. Add this component to your EventSystem that is generated when you add a canvas.

这个脚本的作用是利用OVRInputModule ray设置手柄,这个脚本需要加在EventSystem下。

using UnityEngine;
using UnityEngine.EventSystems;

public class SetUITransformRay : MonoBehaviour
{
    [SerializeField]
    private OVRInputModule inputModule;
    [SerializeField]
    private OVRGazePointer gazePointer;

    public void SetUIRays()
    {
        inputModule.rayTransform = ControllerInfo.CONTROLLER_DATA_FOR_RAYS.transform;
        gazePointer.rayTransform = ControllerInfo.CONTROLLER_DATA_FOR_RAYS.transform;
    }
}

3. 操作步骤

Step 1) Scene Setup

Import the Oculus Integration pack. Drag the OVRCameraRig onto your scene. Drag a OVRTrackedRemote into the left and right hand anchors. Set each remote to left or right depending on the anchor.

  • 导入Oculus Integration包
  • 把OVRCameraRig拖入场景中
  • 将OVRTrackedRemote 拖入到 lefthand anchor 和 right hand anchor
  • 设定OVRTrackedRemote 控制对应left or right手柄
    在这里插入图片描述

Step 2) Set up your canvas

设置Canvas
在这里插入图片描述

Step 3) Set up Eventsystem.

设置Eventsystem

在这里插入图片描述

Step 4) Set up correct objects

Create 3 objects;

  • Controller Manager,
  • Controller Pointer,
  • OVRGazePointer.

For the OVRGazePointer, I just quickly went in the example scene for UI, Oculus\VR\Scenes, and prefabed the OVRGazePointer there.

On Controller Pointer, there is a LineRenderer. This has two points, doesn’t matter where. It uses world space. It has a width of 0.005.

Only having two points, and using world space is very important. This is because ControllerPointer the script relies on those two being set like that.
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity交互界面设计与创建是指在Unity游戏开发引擎中,通过设计和创建用户界面来实现游戏和应用程序的用户交互体验。这包括设计并创建各种用户界面元素,例如按钮、块、文本框、面板等,以便用户能够与游戏进行互动。 首先,设计师需要使用Unity的UI编辑器来创建用户界面元素。UI编辑器提供了直观和易用的界面设计用户界面,并提供了丰富的控件库供设计师选择。设计师可以根据开发需求和美感要求,选择和布置适当的控件,并设置它们的外观、位置和交互行为。 其次,设计师可以使用Unity的自定义脚本语言(如C#)来实现用户界面交互功能。例如,当用户点击按钮时,可以编写相应的脚本来触发游戏中的某个事件或执行特定的操作。这样,用户界面就能够响应用户的操作,实现游戏与用户之间的交互。 此外,Unity还提供了丰富的资源和工具,如动画编辑器和粒子系统,来增强用户界面的动态效果和视觉效果。设计师可以使用这些资源和工具来创建各种炫酷的动画效果,使用户界面更加生动有趣。 最后,设计师需要对用户界面进行测试和优化。他们可以通过模拟用户的实际操作来检查界面的流畅性和易用性,并根据反馈和测试结果对界面进行调整和改进。这有助于确保用户界面的质量和性能,提供良好的用户体验。 总的来说,Unity交互界面设计与创建是一个综合的过程,涉及到设计、布局、编程和测试等多个环节。通过合理的设计和灵活的编程,可以创建出富有交互性和吸引力的用户界面,提升游戏和应用程序的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值