MouseDrag.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using UnityEngine;
  2. public class MouseDrag : MonoBehaviour
  3. {
  4. public Transform ForearmL;
  5. public Transform HandL;
  6. public Vector3 HandLangles;
  7. public int ido;
  8. public bool initFlg;
  9. public bool initFlg2;
  10. public bool isIdo;
  11. public bool isMouseDown;
  12. public bool isMouseDrag;
  13. public bool isMouseUp;
  14. public bool isPlay;
  15. public bool isSelect;
  16. public bool isStop;
  17. public Maid maid;
  18. public GameObject obj;
  19. public Vector3 off;
  20. public Vector3 off2;
  21. public bool onFlg;
  22. public bool onFlg2;
  23. public bool reset;
  24. public float shoki = -1000f;
  25. public Transform UpperArmL;
  26. public Vector3 UpperArmLangles;
  27. private readonly GameObject ForearmL2 = new GameObject();
  28. private readonly GameObject HandL2 = new GameObject();
  29. private readonly TBody.IKCMO IK = new TBody.IKCMO();
  30. private Vector3 mouseIti;
  31. private Vector3 mouseIti2;
  32. private Vector3 rotate;
  33. private Vector3 rotate2;
  34. private readonly GameObject UpperArmL2 = new GameObject();
  35. private Vector3 worldPoint;
  36. public void OnMouseDown()
  37. {
  38. if (maid == null)
  39. return;
  40. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手", false);
  41. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  42. off = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
  43. off2 = new Vector3(obj.transform.position.x - HandL.position.x,
  44. obj.transform.position.y - HandL.position.y,
  45. obj.transform.position.z - HandL.position.z);
  46. if (!initFlg)
  47. {
  48. IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
  49. initFlg = true;
  50. if (!initFlg2)
  51. {
  52. initFlg2 = true;
  53. HandL2.transform.position = HandL.position;
  54. UpperArmL2.transform.position = UpperArmL.position;
  55. ForearmL2.transform.position = ForearmL.position;
  56. HandL2.transform.localRotation = HandL.localRotation;
  57. UpperArmL2.transform.localRotation = UpperArmL.localRotation;
  58. ForearmL2.transform.localRotation = ForearmL.localRotation;
  59. }
  60. }
  61. if (onFlg)
  62. {
  63. onFlg2 = true;
  64. IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
  65. }
  66. mouseIti = Input.mousePosition;
  67. isPlay = maid.body0.m_Bones.GetComponent<Animation>().isPlaying;
  68. isSelect = true;
  69. rotate = HandL.localEulerAngles;
  70. rotate2 = UpperArmL.localEulerAngles;
  71. isMouseUp = false;
  72. isMouseDown = true;
  73. if (ido == 10)
  74. {
  75. Vector3 tgt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off
  76. - off2;
  77. IK.Init(UpperArmL2.transform, ForearmL2.transform, HandL2.transform, maid.body0);
  78. for (int index = 0; index < 10; ++index)
  79. {
  80. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  81. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  82. }
  83. }
  84. if (shoki != -1000.0)
  85. return;
  86. shoki = UpperArmL.localEulerAngles.x;
  87. if (shoki <= 300.0)
  88. return;
  89. shoki -= 360f;
  90. }
  91. public void OnMouseUp()
  92. {
  93. isMouseUp = true;
  94. isMouseDown = false;
  95. }
  96. public void OnMouseDrag()
  97. {
  98. if (maid == null)
  99. return;
  100. if (isPlay && mouseIti != Input.mousePosition)
  101. {
  102. maid.body0.m_Bones.GetComponent<Animation>().Stop();
  103. isStop = true;
  104. isPlay = false;
  105. }
  106. if (reset)
  107. {
  108. reset = false;
  109. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  110. off = transform.position
  111. - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
  112. off2 = new Vector3(obj.transform.position.x - HandL.position.x,
  113. obj.transform.position.y - HandL.position.y,
  114. obj.transform.position.z - HandL.position.z);
  115. rotate = HandL.localEulerAngles;
  116. rotate2 = UpperArmL.localEulerAngles;
  117. mouseIti = Input.mousePosition;
  118. if (onFlg)
  119. IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
  120. }
  121. if (mouseIti != Input.mousePosition)
  122. {
  123. Vector3 tgt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off
  124. - off2;
  125. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手", false);
  126. isMouseDrag = true;
  127. if (!isPlay)
  128. isStop = true;
  129. isIdo = false;
  130. if (ido == 0)
  131. {
  132. isIdo = true;
  133. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  134. if (!onFlg)
  135. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  136. HandLangles = new Vector3(HandL.localEulerAngles.x, HandL.localEulerAngles.y, HandL.localEulerAngles.z);
  137. UpperArmLangles = new Vector3(UpperArmL.localEulerAngles.x, UpperArmL.localEulerAngles.y, UpperArmL.localEulerAngles.z);
  138. }
  139. else if (ido == 11)
  140. {
  141. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  142. if (!onFlg)
  143. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  144. float x = UpperArmL.localEulerAngles.x;
  145. if (x > 250.0 + shoki && x < 352.0 + shoki)
  146. x = 352f + shoki;
  147. if (x < 100.0 + shoki && x > 8.0 + shoki)
  148. x = 8f + shoki;
  149. float z = UpperArmL.localEulerAngles.z;
  150. if (z > 140.0 && z < 240.0)
  151. z = 240f;
  152. if (z <= 140.0 && z > 7.0)
  153. z = 7f;
  154. HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
  155. UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
  156. }
  157. else if (ido == 12)
  158. {
  159. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  160. if (!onFlg)
  161. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  162. float x = UpperArmL.localEulerAngles.x;
  163. if (x > 250.0 + shoki && x < 357.0 + shoki)
  164. x = 357f + shoki;
  165. if (x < 100.0 + shoki && x > 3.0 + shoki)
  166. x = 3f + shoki;
  167. float z = UpperArmL.localEulerAngles.z;
  168. if (z > 140.0 && z < 270.0)
  169. z = 270f;
  170. if (z <= 140.0 && z > 15.0)
  171. z = 15f;
  172. HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
  173. UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
  174. }
  175. else if (ido == 17)
  176. {
  177. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  178. if (!onFlg)
  179. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  180. float x = UpperArmL.localEulerAngles.x;
  181. if (x > 250.0 + shoki && x < 357.0 + shoki)
  182. x = 357f + shoki;
  183. if (x < 100.0 + shoki && x > 3.0 + shoki)
  184. x = 3f + shoki;
  185. float z = UpperArmL.localEulerAngles.z;
  186. if (z > 220.0 && z < 345.0)
  187. z = 345f;
  188. if (z <= 220.0 && z > 90.0)
  189. z = 90f;
  190. HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
  191. UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
  192. }
  193. else if (ido == 13)
  194. {
  195. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  196. if (!onFlg)
  197. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  198. float num1 = UpperArmL.localEulerAngles.x;
  199. if (num1 > 250.0 && num1 < 345.0)
  200. num1 = 345f;
  201. float z = UpperArmL.localEulerAngles.z;
  202. if (z > 160.0 && z < 275.0)
  203. z = 275f;
  204. if (z <= 160.0 && z > 60.0)
  205. z = 60f;
  206. float num2 = UpperArmL.localEulerAngles.y;
  207. if (num2 > 250.0 && num2 < 345.0)
  208. num2 = 345f;
  209. HandL.localEulerAngles = new Vector3(rotate.x, HandL.localEulerAngles.y, HandL.localEulerAngles.z);
  210. UpperArmL.localEulerAngles = new Vector3(UpperArmL.localEulerAngles.x, UpperArmL.localEulerAngles.y, z);
  211. }
  212. else if (ido == 14)
  213. {
  214. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  215. if (!onFlg)
  216. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  217. float x = UpperArmL.localEulerAngles.x;
  218. if (x > 250.0 + shoki && x < 345.0 + shoki)
  219. x = 345f + shoki;
  220. if (x < 100.0 + shoki && x > 15.0 + shoki)
  221. x = 15f + shoki;
  222. float z = UpperArmL.localEulerAngles.z;
  223. if (z > 150.0 && z < 240.0)
  224. z = 240f;
  225. if (z <= 150.0 && z > 30.0)
  226. z = 30f;
  227. HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
  228. UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
  229. }
  230. else if (ido == 15)
  231. {
  232. IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
  233. if (!onFlg)
  234. IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
  235. float x = UpperArmL.localEulerAngles.x;
  236. if (x > 250.0 + shoki && x < 357.0 + shoki)
  237. x = 357f + shoki;
  238. if (x < 100.0 + shoki && x > 3.0 + shoki)
  239. x = 3f + shoki;
  240. float z = UpperArmL.localEulerAngles.z;
  241. if (z > 150.0 && z < 270.0)
  242. z = 270f;
  243. if (z <= 150.0 && z > 30.0)
  244. z = 30f;
  245. HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
  246. UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
  247. }
  248. else if (ido == 16)
  249. {
  250. Vector3 vector3 = Input.mousePosition - mouseIti;
  251. Transform transform = GameMain.Instance.MainCamera.gameObject.transform;
  252. transform.TransformDirection(Vector3.right);
  253. transform.TransformDirection(Vector3.forward);
  254. if (mouseIti2 != Input.mousePosition)
  255. {
  256. UpperArmL.localEulerAngles = rotate2;
  257. UpperArmL.localRotation = Quaternion.Euler(UpperArmL.localEulerAngles)
  258. * Quaternion.AngleAxis(vector3.x / 2.2f, Vector3.right);
  259. }
  260. }
  261. else
  262. {
  263. Vector3 vector3_1 = Input.mousePosition - mouseIti;
  264. Transform transform = GameMain.Instance.MainCamera.gameObject.transform;
  265. Vector3 vector3_2 = transform.TransformDirection(Vector3.right);
  266. Vector3 vector3_3 = transform.TransformDirection(Vector3.forward);
  267. if (mouseIti2 != Input.mousePosition)
  268. {
  269. if (ido <= 4)
  270. HandL.localEulerAngles = rotate;
  271. else
  272. UpperArmL.localEulerAngles = rotate2;
  273. if (ido == 1)
  274. {
  275. HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
  276. * Quaternion.AngleAxis(vector3_1.x / 1.5f, Vector3.up);
  277. HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
  278. * Quaternion.AngleAxis(vector3_1.y / 1.5f, Vector3.forward);
  279. }
  280. if (ido == 2)
  281. HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
  282. * Quaternion.AngleAxis(vector3_1.x / 1.5f, Vector3.right);
  283. if (ido == 3)
  284. {
  285. HandL.RotateAround(HandL.position, new Vector3(vector3_2.x, 0.0f, vector3_2.z), vector3_1.y / 1f);
  286. HandL.RotateAround(HandL.position, new Vector3(vector3_3.x, 0.0f, vector3_3.z), vector3_1.x / 1.5f);
  287. }
  288. if (ido == 4)
  289. HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
  290. * Quaternion.AngleAxis((float) (-(double) vector3_1.x / 1.5), Vector3.right);
  291. if (ido == 5)
  292. UpperArmL.localRotation = Quaternion.Euler(UpperArmL.localEulerAngles)
  293. * Quaternion.AngleAxis((float) (-(double) vector3_1.x / 1.5), Vector3.right);
  294. }
  295. }
  296. }
  297. mouseIti2 = Input.mousePosition;
  298. }
  299. }