CubeAppearanceFeedback.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // /******************************************************************************
  2. // * File: CubeAppearanceFeedback.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using UnityEngine;
  8. using UnityEngine.XR.Interaction.Toolkit;
  9. namespace QCHT.Samples.Proximal
  10. {
  11. public class CubeAppearanceFeedback : MonoBehaviour
  12. {
  13. [SerializeField] private Texture2D _normalTexture;
  14. [SerializeField] private Texture2D _hoverTexture;
  15. [SerializeField] private Texture2D _selectedTexture;
  16. [SerializeField] private CubeLabel _label;
  17. private XRGrabInteractable _interactable;
  18. public void OnEnable() {
  19. _interactable = GetComponentInParent<XRGrabInteractable>();
  20. if (_interactable) {
  21. _interactable.hoverEntered.AddListener(OnHoverEntered);
  22. _interactable.hoverExited.AddListener(OnHoverExited);
  23. _interactable.selectEntered.AddListener(OnSelectEntered);
  24. _interactable.selectExited.AddListener(OnSelectExited);
  25. }
  26. }
  27. public void OnDisable() {
  28. if (_interactable) {
  29. _interactable.hoverEntered.RemoveListener(OnHoverEntered);
  30. _interactable.hoverExited.RemoveListener(OnHoverExited);
  31. _interactable.selectEntered.RemoveListener(OnSelectEntered);
  32. _interactable.selectExited.RemoveListener(OnSelectExited);
  33. }
  34. }
  35. private void OnHoverEntered(HoverEnterEventArgs _) => UpdateAppearance();
  36. private void OnHoverExited(HoverExitEventArgs _) => UpdateAppearance();
  37. private void OnSelectEntered(SelectEnterEventArgs _) => UpdateAppearance();
  38. private void OnSelectExited(SelectExitEventArgs _) => UpdateAppearance();
  39. private void UpdateAppearance() {
  40. var material = GetComponent<Renderer>().material;
  41. _label?.ShowLabel(!_interactable.isSelected);
  42. if (_interactable.isSelected) {
  43. material.mainTexture = _selectedTexture;
  44. return;
  45. }
  46. if (_interactable.isHovered) {
  47. material.mainTexture = _hoverTexture;
  48. return;
  49. }
  50. material.mainTexture = _normalTexture;
  51. }
  52. }
  53. }