123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- // /******************************************************************************
- // * File: CubeAppearanceFeedback.cs
- // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
- // *
- // *
- // ******************************************************************************/
- using UnityEngine;
- using UnityEngine.XR.Interaction.Toolkit;
- namespace QCHT.Samples.Proximal
- {
- public class CubeAppearanceFeedback : MonoBehaviour
- {
- [SerializeField] private Texture2D _normalTexture;
- [SerializeField] private Texture2D _hoverTexture;
- [SerializeField] private Texture2D _selectedTexture;
- [SerializeField] private CubeLabel _label;
-
- private XRGrabInteractable _interactable;
-
- public void OnEnable() {
- _interactable = GetComponentInParent<XRGrabInteractable>();
- if (_interactable) {
- _interactable.hoverEntered.AddListener(OnHoverEntered);
- _interactable.hoverExited.AddListener(OnHoverExited);
- _interactable.selectEntered.AddListener(OnSelectEntered);
- _interactable.selectExited.AddListener(OnSelectExited);
- }
- }
- public void OnDisable() {
- if (_interactable) {
- _interactable.hoverEntered.RemoveListener(OnHoverEntered);
- _interactable.hoverExited.RemoveListener(OnHoverExited);
- _interactable.selectEntered.RemoveListener(OnSelectEntered);
- _interactable.selectExited.RemoveListener(OnSelectExited);
- }
- }
- private void OnHoverEntered(HoverEnterEventArgs _) => UpdateAppearance();
- private void OnHoverExited(HoverExitEventArgs _) => UpdateAppearance();
- private void OnSelectEntered(SelectEnterEventArgs _) => UpdateAppearance();
- private void OnSelectExited(SelectExitEventArgs _) => UpdateAppearance();
-
- private void UpdateAppearance() {
- var material = GetComponent<Renderer>().material;
- _label?.ShowLabel(!_interactable.isSelected);
- if (_interactable.isSelected) {
- material.mainTexture = _selectedTexture;
- return;
- }
- if (_interactable.isHovered) {
- material.mainTexture = _hoverTexture;
- return;
- }
- material.mainTexture = _normalTexture;
- }
- }
- }
|