XRSlider.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using XRTool.Util;
  8. namespace XRTool.WorldUI
  9. {
  10. public class XRSlider : Slider
  11. {
  12. private SliderPointEvent onSliderDown;
  13. private SliderPointEvent onSliderUp;
  14. private RectTransform body;
  15. private RectTransform rect3D;
  16. public RectTransform Rect3D
  17. {
  18. get
  19. {
  20. if (!rect3D)
  21. {
  22. var obj = fillRect.GetComponentInChildren<XRIcon3D>();
  23. if (obj)
  24. {
  25. rect3D = obj.GetComponent<RectTransform>();
  26. }
  27. }
  28. return rect3D;
  29. }
  30. set => rect3D = value;
  31. }
  32. public SliderPointEvent OnSliderDown
  33. {
  34. get
  35. {
  36. if (onSliderDown == null)
  37. {
  38. onSliderDown = new SliderPointEvent();
  39. }
  40. return onSliderDown;
  41. }
  42. }
  43. public SliderPointEvent OnSliderUp
  44. {
  45. get
  46. {
  47. if (onSliderUp == null)
  48. {
  49. onSliderUp = new SliderPointEvent();
  50. }
  51. return onSliderUp;
  52. }
  53. }
  54. protected override void Awake()
  55. {
  56. base.Awake();
  57. body = GetComponent<RectTransform>();
  58. onValueChanged.AddListener((float f)=>{
  59. UpdateSlider();
  60. });
  61. }
  62. public override void OnPointerDown(PointerEventData eventData)
  63. {
  64. base.OnPointerDown(eventData);
  65. onSliderDown?.Invoke(eventData);
  66. }
  67. public override void OnPointerUp(PointerEventData eventData)
  68. {
  69. base.OnPointerUp(eventData);
  70. OnSliderUp?.Invoke(eventData);
  71. }
  72. public void UpdateSlider()
  73. {
  74. Vector2 tmp = Rect3D.rect.size;
  75. tmp.x = value * body.rect.size.x;
  76. Rect3D.sizeDelta = tmp;
  77. }
  78. }
  79. public class SliderPointEvent : UnityEvent<PointerEventData>
  80. {
  81. }
  82. }