InteractionObjCube.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using GxrSdk;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. public class InteractionObjCube : MonoBehaviour,
  11. IPointerEnterHandler, IPointerExitHandler,
  12. IPointerDownHandler, IPointerUpHandler, IPointerClickHandler,
  13. IBeginDragHandler, IEndDragHandler,
  14. IDragHandler, IDropHandler, IInitializePotentialDragHandler
  15. {
  16. [SerializeField]
  17. private InteractionObj _interactionObj = null;
  18. [SerializeField]
  19. private TextMesh _textMesh = null;
  20. private Material _cubeMaterial = null;
  21. private Color _randomColor = new Color();
  22. private Vector3 _lastPos = Vector3.zero;
  23. private Color _lastColor;
  24. private bool _isLockColor = false;
  25. private bool _isRotating = false;
  26. void Start()
  27. {
  28. _cubeMaterial = GetComponent<MeshRenderer>().material;
  29. ChangeColor();
  30. }
  31. public void OnPointerEnter(PointerEventData eventData)
  32. {
  33. if (eventData is GxrPointerEventData pointerEventData)
  34. {
  35. _lastColor = _cubeMaterial.color;
  36. ChangeColor();
  37. }
  38. }
  39. public void OnPointerExit(PointerEventData eventData)
  40. {
  41. if (eventData is GxrPointerEventData pointerEventData)
  42. {
  43. ChangeColor(_lastColor);
  44. }
  45. }
  46. public void OnPointerDown(PointerEventData eventData)
  47. {
  48. if (eventData is GxrPointerEventData pointerEventData)
  49. {
  50. _interactionObj.transform.localScale += Vector3.one * 0.06f;
  51. }
  52. }
  53. public void OnPointerUp(PointerEventData eventData)
  54. {
  55. if (eventData is GxrPointerEventData pointerEventData)
  56. {
  57. _interactionObj.transform.localScale -= Vector3.one * 0.06f;
  58. }
  59. }
  60. public void OnPointerClick(PointerEventData eventData)
  61. {
  62. if (eventData is GxrPointerEventData pointerEventData)
  63. {
  64. ChangeColor();
  65. }
  66. }
  67. public void OnBeginDrag(PointerEventData eventData)
  68. {
  69. if (eventData is GxrPointerEventData pointerEventData)
  70. {
  71. }
  72. }
  73. public void OnEndDrag(PointerEventData eventData)
  74. {
  75. if (eventData is GxrPointerEventData pointerEventData)
  76. {
  77. }
  78. }
  79. public void OnDrag(PointerEventData eventData)
  80. {
  81. if (eventData is GxrPointerEventData pointerEventData)
  82. {
  83. Vector3 currentPos = pointerEventData.Position3D + (pointerEventData.Rotation3D * Vector3.forward) * pointerEventData.PressDistance;
  84. if (_lastPos == Vector3.zero)
  85. {
  86. _lastPos = currentPos;
  87. }
  88. Vector3 deltaPos = currentPos - _lastPos;
  89. deltaPos.z = 0;
  90. _interactionObj.transform.position += deltaPos;
  91. _lastPos = currentPos;
  92. }
  93. }
  94. public void OnDrop(PointerEventData eventData)
  95. {
  96. if (eventData is GxrPointerEventData pointerEventData)
  97. {
  98. _lastPos = Vector3.zero;
  99. InteractionObjManager.Instance.DropObj(_interactionObj);
  100. }
  101. }
  102. public void OnInitializePotentialDrag(PointerEventData eventData)
  103. {
  104. if (eventData is GxrPointerEventData pointerEventData)
  105. {
  106. _interactionObj.Add();
  107. }
  108. }
  109. private Color GetRandomColor()
  110. {
  111. _randomColor.r = Random.Range(0, 1.0f);
  112. _randomColor.g = Random.Range(0, 1.0f);
  113. _randomColor.b = Random.Range(0, 1.0f);
  114. return _randomColor;
  115. }
  116. public void ChangeColor(Color color = default)
  117. {
  118. if (_isLockColor)
  119. {
  120. return;
  121. }
  122. if (color == default)
  123. {
  124. color = GetRandomColor();
  125. }
  126. _cubeMaterial.color = color;
  127. Color textColor = Color.white - color;
  128. textColor.a = 1;
  129. _textMesh.color = textColor;
  130. }
  131. public void LockColor(bool isLock)
  132. {
  133. if (isLock)
  134. {
  135. ChangeColor(Color.green);
  136. }
  137. else
  138. {
  139. ChangeColor();
  140. }
  141. _isLockColor = isLock;
  142. }
  143. public void Rotate()
  144. {
  145. if (_interactionObj == null)
  146. {
  147. return;
  148. }
  149. int row = _interactionObj.Row;
  150. int column = _interactionObj.Column;
  151. if (!_isRotating)
  152. {
  153. LeanTween.rotateAroundLocal(gameObject, Vector3.up, 90, 0.5f + column * 0.15f)
  154. .setEase(LeanTweenType.easeOutQuad)
  155. .setOnStart(() =>
  156. {
  157. _isRotating = true;
  158. }).setOnComplete(() =>
  159. {
  160. _isRotating = false;
  161. });
  162. }
  163. }
  164. }