NRButton.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using UnityEngine.EventSystems;
  15. /// <summary> A nr button. </summary>
  16. public class NRButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  17. {
  18. /// <summary> The image normal. </summary>
  19. public Sprite ImageNormal;
  20. /// <summary> The image hover. </summary>
  21. public Sprite ImageHover;
  22. /// <summary> The trigger event. </summary>
  23. public Action<string, GameObject, RaycastResult> TriggerEvent;
  24. /// <summary> The enter. </summary>
  25. public const string Enter = "Enter";
  26. /// <summary> The hover. </summary>
  27. public const string Hover = "Hover";
  28. /// <summary> The exit. </summary>
  29. public const string Exit = "Exit";
  30. /// <summary> The button image. </summary>
  31. private Image m_ButtonImage;
  32. /// <summary> Starts this object. </summary>
  33. void Start()
  34. {
  35. m_ButtonImage = gameObject.GetComponent<Image>();
  36. }
  37. /// <summary> <para></para> </summary>
  38. /// <param name="eventData"> Current event data.</param>
  39. public void OnPointerDown(PointerEventData eventData)
  40. {
  41. if (TriggerEvent != null)
  42. {
  43. TriggerEvent(Enter, gameObject, eventData.pointerCurrentRaycast);
  44. }
  45. if (ImageHover != null && m_ButtonImage != null)
  46. {
  47. m_ButtonImage.sprite = ImageHover;
  48. }
  49. }
  50. /// <summary> <para></para> </summary>
  51. /// <param name="eventData"> Current event data.</param>
  52. public void OnPointerUp(PointerEventData eventData)
  53. {
  54. if (TriggerEvent != null)
  55. {
  56. TriggerEvent(Exit, gameObject, eventData.pointerCurrentRaycast);
  57. }
  58. if (ImageNormal != null && m_ButtonImage != null)
  59. {
  60. m_ButtonImage.sprite = ImageNormal;
  61. }
  62. }
  63. /// <summary> Get onhover by NRMultScrPointerRaycaster. </summary>
  64. /// <param name="racastResult"> The racast result.</param>
  65. public void OnHover(RaycastResult racastResult)
  66. {
  67. if (TriggerEvent != null && m_ButtonImage != null)
  68. {
  69. TriggerEvent(Hover, gameObject, racastResult);
  70. }
  71. }
  72. }
  73. }