HandGestureInEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using Rokid.UXR.Interaction;
  3. namespace Rokid.UXR.Utility
  4. {
  5. /// <summary>
  6. /// 编辑的模拟手势
  7. /// </summary>
  8. public class HandGestureInEditor : AutoInjectBehaviour
  9. {
  10. [SerializeField, Autowrited]
  11. private HandType hand;
  12. [SerializeField, Autowrited]
  13. private GameObject grip;
  14. [SerializeField, Autowrited]
  15. private GameObject poke;
  16. [SerializeField, Autowrited]
  17. private GameObject palm;
  18. [SerializeField, Autowrited]
  19. private Transform content;
  20. public float fallbackMaxDistanceNoItem = 10.0f;
  21. public float fallbackMaxDistanceWithItem = 0.5f;
  22. private float fallbackInteractorDistance = -1.0f;
  23. protected override void Awake()
  24. {
  25. base.Awake();
  26. InteractorStateChange.OnPokeInteractorHover += OnPokeInteractorHover;
  27. InteractorStateChange.OnPokeInteractorUnHover += OnPokeInteractorUnHover;
  28. }
  29. private void Start()
  30. {
  31. #if !UNITY_EDITOR
  32. DestroyImmediate(this.gameObject);
  33. #endif
  34. poke.gameObject.SetActive(true);
  35. grip.gameObject.SetActive(false);
  36. palm.gameObject.SetActive(false);
  37. }
  38. private void OnPokeInteractorHover(HandType hand)
  39. {
  40. if (hand == this.hand)
  41. this.gameObject.SetActive(true);
  42. }
  43. private void OnPokeInteractorUnHover(HandType hand)
  44. {
  45. if (hand == this.hand)
  46. this.gameObject.SetActive(false);
  47. }
  48. private void OnDestroy()
  49. {
  50. InteractorStateChange.OnPokeInteractorHover -= OnPokeInteractorHover;
  51. InteractorStateChange.OnPokeInteractorUnHover -= OnPokeInteractorUnHover;
  52. }
  53. private void Update()
  54. {
  55. UpdateInEditor();
  56. if (Input.GetKey(KeyCode.X))
  57. {
  58. poke.gameObject.SetActive(false);
  59. if (Input.GetMouseButton(0))
  60. {
  61. grip.gameObject.SetActive(true);
  62. palm.gameObject.SetActive(false);
  63. }
  64. else
  65. {
  66. grip.gameObject.SetActive(false);
  67. palm.gameObject.SetActive(true);
  68. }
  69. }
  70. if (Input.GetKeyUp(KeyCode.X))
  71. {
  72. poke.gameObject.SetActive(true);
  73. grip.gameObject.SetActive(false);
  74. palm.gameObject.SetActive(false);
  75. }
  76. }
  77. protected virtual void UpdateInEditor()
  78. {
  79. Ray ray = MainCameraCache.mainCamera.ScreenPointToRay(Input.mousePosition);
  80. Vector3 oldPosition = transform.position;
  81. transform.position = MainCameraCache.mainCamera.transform.forward * (-1000.0f);
  82. RaycastHit raycastHit;
  83. if (Physics.Raycast(ray, out raycastHit, fallbackMaxDistanceNoItem))
  84. {
  85. if (Input.GetMouseButton(0))
  86. {
  87. transform.position = raycastHit.point;
  88. }
  89. else
  90. {
  91. transform.position = raycastHit.point + raycastHit.normal * 0.02f;
  92. }
  93. fallbackInteractorDistance = Mathf.Min(fallbackMaxDistanceNoItem, raycastHit.distance);
  94. }
  95. else if (fallbackInteractorDistance > 0.0f)
  96. {
  97. transform.position = ray.origin + Mathf.Min(fallbackMaxDistanceNoItem, fallbackInteractorDistance) * ray.direction;
  98. }
  99. else
  100. {
  101. transform.position = oldPosition;
  102. }
  103. }
  104. }
  105. }