RotManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using SC.InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. public class RotManager : ChangeFcous
  7. {
  8. [HideInInspector]
  9. public DragComponent dc;
  10. // [HideInInspector]
  11. public int index;
  12. [HideInInspector]
  13. public float perValue;
  14. // Start is called before the first frame update
  15. SCButton scbt;
  16. private GameObject AllowPrefab;
  17. GameObject rotgameclone;
  18. bool isOpen = false;
  19. void Start()
  20. {
  21. // scbt = this.gameObject.AddComponent<SCButton>();
  22. // UnityEvent onSCDown = new UnityEvent();
  23. // onSCDown.AddListener(onDown);
  24. //scbt.onDown = onSCDown;
  25. box = this.GetComponent<BoxCollider>();
  26. AllowPrefab = Resources.Load<GameObject>("ShadowEidtBox");
  27. rotgameclone = AllowPrefab.GetComponent<DragManager>().focusRotModel;
  28. //model = dc.dm.focusRotModel;
  29. }
  30. override public void OnSCPointerEnter(InputDevicePartBase part, SCPointEventData eventData)
  31. {
  32. isOpen = true;
  33. }
  34. public override void OnSCPointerExit(InputDevicePartBase part, SCPointEventData eventData)
  35. {
  36. base.OnSCPointerExit(part, eventData);
  37. isOpen = false;
  38. if (objClone != null)
  39. {
  40. Destroy(objClone);
  41. }
  42. dc.changeEnterPart(part.name, false);
  43. if (!dc.isEnterChoose())
  44. dc.HideBoxEdit();
  45. }
  46. override public void OnSCPointerDown(InputDevicePartBase part, SCPointEventData eventData)
  47. {
  48. dragInitLocalPosition = eventData.dragPosition;
  49. Debug.Log("Down=======>" + dragInitLocalPosition.x.ToString() + dragInitLocalPosition.y.ToString() + dragInitLocalPosition.z.ToString());
  50. onDown();
  51. }
  52. override public void OnSCPointerUp(InputDevicePartBase part, SCPointEventData eventData)
  53. {
  54. onUp();
  55. }
  56. override public void OnSCPointerDrag(InputDevicePartBase part, SCPointEventData eventData)
  57. {
  58. if (!dc.isBoth&&!dc.isMove && isDown)
  59. {
  60. float temp = 0;
  61. float temp2 = 0;
  62. float temp3 = 0;
  63. // Vector3 poslocal = new Vector3(0, 0, 0);
  64. Vector3 poslocal = eventData.dragPosition;
  65. Vector3 xlv3 = GetNormal(dragInitLocalPosition, poslocal);
  66. temp = xlv3.x;
  67. temp2 = xlv3.y;
  68. temp3 = xlv3.z;
  69. float xishu = 40;
  70. Vector3 xxjd;
  71. if (index == 4 || index == 8 || index == 9 || index == 11)
  72. {
  73. if (index == 9 || index == 11)
  74. {
  75. temp = -temp;
  76. }
  77. if (index == 4 || index == 11)
  78. {
  79. temp3 = -temp3;
  80. }
  81. dc.transform.localEulerAngles = new Vector3(0, (temp + temp3 + temp2) * xishu, 0);
  82. // dc.transform.Rotate(xxjd - nowxz);
  83. // Debug.Log("temp========>"+ index + nowxz);
  84. }
  85. else if (index == 2 || index == 6 || index == 0 || index == 7)
  86. {
  87. if (index == 7 || index == 6)
  88. {
  89. temp = -temp;
  90. }
  91. if (index == 7 || index == 0)
  92. {
  93. temp3 = -temp3;
  94. }
  95. if (index == 2 || index == 6)
  96. {
  97. temp2 = -temp2;
  98. }
  99. dc.transform.localEulerAngles = new Vector3(0, 0, -(temp + temp3 - temp2) * xishu);
  100. Debug.Log(" 绕Z轴旋转Z" + index);
  101. }
  102. else
  103. {
  104. if (index == 5 || index == 10)
  105. {
  106. temp3 = -temp3;
  107. }
  108. if (index == 5 || index == 3)
  109. {
  110. temp2 = -temp2;
  111. }
  112. dc.transform.localEulerAngles = new Vector3((temp + temp3 + temp2) * xishu, 0, 0);
  113. Debug.Log(" 绕X轴旋转Y" + index + xlv3);
  114. }
  115. dc.changeLine();
  116. }
  117. else
  118. {
  119. Debug.Log("关闭DOWN");
  120. isDown = false;
  121. }
  122. }
  123. private void onUp()
  124. {
  125. // InputDeviceEventBase.anyKeyUpDelegate -= onUp;
  126. isOpen = false;
  127. if (objClone != null)
  128. {
  129. Destroy(objClone);
  130. }
  131. isDown = false;
  132. dc.box.enabled = true;
  133. dc.transform.parent = dc.parentGame.transform.parent;
  134. dc.parentGame.transform.rotation = dc.transform.rotation;
  135. nowxz = new Vector3(0,0,0);
  136. dc.editGame.transform.parent = dc.parentGame.transform;
  137. dc.ShowBoxEdit();
  138. }
  139. Vector3 GetNormal(Vector3 b, Vector3 c)
  140. {
  141. b = dc.childGame.transform.InverseTransformPoint(b);
  142. c = dc.childGame.transform.InverseTransformPoint(c);
  143. Debug.Log("b=======>" + b.x.ToString() + "," + b.y.ToString() + "," + b.z.ToString());
  144. Debug.Log("c=======>" + c.x.ToString()+","+ c.y.ToString() + "," + c.z.ToString());
  145. return c - b;
  146. }
  147. Vector3 dragInitLocalPosition;
  148. Vector3 nowxz =new Vector3(0,0,0);
  149. BoxCollider box;
  150. Vector3 objCenter;
  151. Vector3 objUp;
  152. private void GetObjUpPoint(BoxCollider boxCollider)
  153. {
  154. objCenter = this.transform.TransformPoint(boxCollider.center.x, boxCollider.center.y, boxCollider.center.z); ;
  155. objUp = this.transform.TransformPoint(boxCollider.center.x, boxCollider.center.y + boxCollider.size.y * 0.5f, boxCollider.center.z);
  156. }
  157. GameObject objClone;
  158. public void ShowModel2()
  159. {
  160. GetObjUpPoint(box);
  161. rotgameclone.SetActive(true);
  162. Vector3 a = new Vector3(objUp.x - objCenter.x, objUp.y - objCenter.y, objUp.z - objCenter.z);
  163. Vector3 b = new Vector3(objUp.x - objCenter.x, 0, objUp.z - objCenter.z);
  164. float angle = Vector3.Angle(a, b);
  165. if (a.x == 0 && a.z == 0)
  166. {
  167. rotgameclone.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = true;
  168. rotgameclone.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().enabled = false;
  169. }
  170. else if (a.y == 0)
  171. {
  172. rotgameclone.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = false;
  173. rotgameclone.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().enabled = true;
  174. }
  175. else if (angle < 45)
  176. {
  177. rotgameclone.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = false;
  178. rotgameclone.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().enabled = true;
  179. }
  180. else if (angle >= 45)
  181. {
  182. rotgameclone.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = true;
  183. rotgameclone.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().enabled = false;
  184. }
  185. if (objClone == null)
  186. {
  187. objClone = GameObject.Instantiate(rotgameclone);
  188. objClone.transform.parent = GameObject.Find("Focus").transform;
  189. objClone.transform.localPosition = Vector3.zero;
  190. objClone.transform.localEulerAngles = Vector3.zero;
  191. }
  192. }
  193. override public void Update()
  194. {
  195. /*
  196. this.model.transform.LookAt(SvrManager.Instance.head);
  197. this.model.transform.eulerAngles = new Vector3(this.model.transform.eulerAngles.x, this.model.transform.eulerAngles.y, this.model.transform.eulerAngles.z + this.transform.localEulerAngles.z);
  198. */
  199. if (isOpen)
  200. {
  201. ShowModel2();
  202. }
  203. else
  204. {
  205. if (objClone != null)
  206. {
  207. Destroy(objClone);
  208. }
  209. }
  210. }
  211. private bool isDown;
  212. public void onDown()
  213. {
  214. /*
  215. for (int i = 0; i < dc.veces.Length; i++)
  216. {
  217. dc.scaleGameList[i].GetComponent<BoxCollider>().enabled = false;
  218. }
  219. for (int i = 0; i < 12; i++)
  220. {
  221. dc.eulerGameList[i].GetComponent<BoxCollider>().enabled = false;
  222. }*/
  223. // InputDeviceEventBase.anyKeyUpDelegate += onUp;
  224. dc.parentGame.transform.localEulerAngles = dc.transform.localEulerAngles;
  225. dc.parentGame.transform.position = dc.transform.position;
  226. dc.transform.parent = dc.parentGame.transform;
  227. dc.rotEurl = dc.transform.localEulerAngles;
  228. dc.editGame.transform.parent = dc.transform;
  229. dc.childGame.transform.localEulerAngles = new Vector3(0, 0, 0);
  230. dc.childGame.transform.localScale = dc.transform.localScale;
  231. // dc.box.enabled = false;
  232. dc.changeLine();
  233. Debug.Log("触发DOWN");
  234. isDown = true;
  235. }
  236. }