ModelManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using XRTool.Util;
  8. public class ModelManager : MonoBehaviour
  9. {
  10. private void OnDestroy()
  11. {
  12. DispatcherBase.KeyUpDelegateUnRegister(OnKeyUpAction);
  13. }
  14. private void OnEnable()
  15. {
  16. DispatcherBase.KeyUpDelegateRegister(OnKeyUpAction);
  17. DispatcherBase.KeyDownDelegateRegister(OnKeyDownAction);
  18. }
  19. InputDevicePartBase part0;
  20. Vector3 modelPos;
  21. private void OnKeyDownAction(InputKeyCode keyCode, InputDevicePartBase part)
  22. {
  23. part0 = part;
  24. startPos = part.detectorBase.pointerBase.cursorBase.transform.position;
  25. this.transform.position = startPos;
  26. this.transform.eulerAngles = new Vector3(0,0,0) ;
  27. Debug.Log("物体开始生成" + startPos);
  28. this.GetComponent<MeshRenderer>().enabled = true;
  29. this.GetComponent<BoxCollider>().enabled = false;
  30. this.transform.parent = null;
  31. modelPos = this.transform.localPosition;
  32. isCreate = true;
  33. DispatcherBase.KeyDownDelegateUnRegister(OnKeyDownAction);
  34. }
  35. private void OnKeyUpAction(InputKeyCode keyCode, InputDevicePartBase part)
  36. {
  37. var obj = part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject;//SDK里判断点击返回的物体
  38. if (isUp)
  39. {
  40. if (Mathf.Abs(this.transform.localScale.y) <= 0.01f)
  41. {
  42. Destroy(this.gameObject);
  43. return;
  44. }
  45. Debug.Log("物体平面生成完毕并消除");
  46. this.GetComponent<BoxCollider>().enabled = true;
  47. this.GetComponent<XBoundingBox>().enabled = true;
  48. this.GetComponent<XDragComponent>().enabled = true;
  49. this.enabled = false;
  50. return;
  51. }
  52. if (isCreate)
  53. {
  54. startPos =new Vector3( part.detectorBase.pointerBase.cursorBase.transform.position.x, part.detectorBase.pointerBase.cursorBase.transform.position.y-0.01f, part.detectorBase.pointerBase.cursorBase.transform.position.z);
  55. Debug.Log("物体平面生成完毕");
  56. if(Mathf.Abs(this.transform.localScale.x)<=0.01f)
  57. {
  58. Destroy(this.gameObject);
  59. return;
  60. }
  61. isUp = true;
  62. isCreate = false;
  63. modelPos = this.transform.localPosition;
  64. }
  65. }
  66. bool isUp;
  67. bool isCreate;
  68. Vector3 startPos;
  69. Vector3 movePos;
  70. public void Update()
  71. {
  72. if(isUp)
  73. {
  74. Vector3 start = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(startPos);
  75. Vector3 end = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(part0.detectorBase.pointerBase.cursorBase.transform.position);
  76. float dicy = Vector3.Distance(new Vector3(0, start.y, 0), new Vector3(0, end.y, 0))/2;
  77. if (start.y > part0.detectorBase.pointerBase.cursorBase.transform.position.y)
  78. {
  79. dicy = -dicy;
  80. }
  81. this.transform.localScale = new Vector3(this.transform.localScale.x, dicy, this.transform.localScale.z);
  82. this.transform.localPosition = new Vector3(this.transform.localPosition.x, modelPos.y+ dicy/2, this.transform.localPosition.z);
  83. Debug.Log("物体开始生成立体");
  84. }
  85. if(isCreate)
  86. {
  87. Vector3 start = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(startPos);
  88. Vector3 end = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(part0.detectorBase.pointerBase.cursorBase.transform.position);
  89. Debug.Log("物体平面生成中" + end);
  90. // this.transform.
  91. float dicx = Vector3.Distance(new Vector3(start.x,0,0), new Vector3(end.x, 0, 0));
  92. if(start.x> part0.detectorBase.pointerBase.cursorBase.transform.position.x)
  93. {
  94. dicx = -dicx;
  95. }
  96. float dicz = Vector3.Distance(new Vector3(0, start.y, start.z), new Vector3(0, end.y, end.z));
  97. if (start.z < end.z)
  98. {
  99. dicz = -dicz;
  100. }
  101. this.transform.localScale = new Vector3(dicx, 0.01f, dicz);
  102. this.transform.localPosition = new Vector3(modelPos.x+dicx/2, this.transform.localPosition.y, modelPos.z+ dicz/2);
  103. }
  104. }
  105. }