AniRoByTime.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. namespace MetaClient
  6. {
  7. public class AniRoByTime : MonoBehaviour
  8. {
  9. public Dictionary<int, Quaternion> dicRo = new Dictionary<int, Quaternion>();
  10. public Dictionary<int,Vector3> pos = new Dictionary<int,Vector3>();
  11. private Quaternion initRo = new Quaternion();
  12. private Vector3 initPos = new Vector3();
  13. private int startTime = 0;
  14. private Vector3 startPos = new Vector3();
  15. private Vector3 endPos = new Vector3();
  16. private Quaternion startRo = new Quaternion();
  17. private int endTime = 0;
  18. private Quaternion endRo = new Quaternion();
  19. private List<Quaternion> mList = new List<Quaternion>();
  20. private int lasttime = 0;
  21. public AniManager aniManager;
  22. public bool allBody=false;
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. }
  27. public void Init()
  28. {
  29. if (this.gameObject.name == aniManager.nowAniName)
  30. {
  31. allBody = true;
  32. initRo = this.gameObject.transform.localRotation;
  33. initPos = this.gameObject.transform.localPosition;
  34. }
  35. else
  36. {
  37. initRo = this.gameObject.transform.parent.localRotation;
  38. }
  39. }
  40. public void DicOrderBy()
  41. {
  42. Dictionary<int, Quaternion> newdic = dicRo.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  43. dicRo = newdic;
  44. Dictionary<int,Vector3> newpos=pos.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  45. pos = newpos;
  46. }
  47. public void RoAllBodyChangeFun(int _time)
  48. {
  49. startTime = 0;
  50. startRo = initRo;
  51. startPos = initPos;
  52. endPos = initPos;
  53. endTime = 0;
  54. endRo = initRo;
  55. lasttime = -1;
  56. if (dicRo.Count == 0)
  57. {
  58. // return;
  59. this.gameObject.transform.localRotation = initRo;
  60. this.gameObject.transform.localPosition = initPos;
  61. return;
  62. }
  63. else if (dicRo.Count == 1)
  64. {
  65. mList = new List<Quaternion>();
  66. var value = dicRo.Values.GetEnumerator();
  67. value.MoveNext();
  68. var valuePos = pos.Values.GetEnumerator();
  69. valuePos.MoveNext();
  70. this.gameObject.transform.localRotation = value.Current;
  71. this.gameObject.transform.localPosition = valuePos.Current;
  72. }
  73. else
  74. {
  75. mList = new List<Quaternion>();
  76. var value = dicRo.Keys.GetEnumerator();
  77. var valuePos = pos.Values.GetEnumerator();
  78. while (value.MoveNext())
  79. {
  80. valuePos.MoveNext();
  81. int data = value.Current;
  82. if (_time < data)
  83. {
  84. endTime = data;
  85. endRo = dicRo[endTime];
  86. endPos = pos[endTime];
  87. if (lasttime != -1)
  88. {
  89. startTime = lasttime;
  90. startRo = dicRo[lasttime];
  91. startPos = pos[lasttime];
  92. RoAllBodyChange(_time);
  93. }
  94. else
  95. {
  96. if (dicRo.ContainsKey(lasttime))
  97. {
  98. this.gameObject.transform.localRotation = dicRo[lasttime];
  99. this.gameObject.transform.localPosition = pos[lasttime];
  100. }
  101. else
  102. {
  103. this.gameObject.transform.localPosition = endPos;
  104. }
  105. return;
  106. //startTime = endTime;
  107. //startRo = endRo;
  108. }
  109. }
  110. else
  111. {
  112. lasttime = value.Current;
  113. }
  114. }
  115. }
  116. }
  117. public void RoChangeFun(int _time)
  118. {
  119. if (allBody)
  120. {
  121. RoAllBodyChangeFun(_time);
  122. return;
  123. }
  124. startTime = 0;
  125. startRo = initRo;
  126. endTime = 0;
  127. endRo = initRo;
  128. lasttime = -1;
  129. if (dicRo.Count == 0)
  130. {
  131. // return;
  132. this.gameObject.transform.parent.localRotation = initRo;
  133. return;
  134. }
  135. else if (dicRo.Count == 1)
  136. {
  137. //List<Quaternion> mList;
  138. mList = new List<Quaternion>();
  139. var value = dicRo.Values.GetEnumerator();
  140. value.MoveNext();
  141. this.gameObject.transform.parent.localRotation = value.Current;
  142. //while (value.MoveNext())
  143. //{
  144. // Quaternion data = value.Current;
  145. // mList.Add(data);
  146. //}
  147. //this.gameObject.transform.localEulerAngles = dicRo;
  148. }
  149. else
  150. {
  151. mList = new List<Quaternion>();
  152. var value = dicRo.Keys.GetEnumerator();
  153. while (value.MoveNext())
  154. {
  155. int data = value.Current;
  156. if (_time < data)
  157. {
  158. endTime = data;
  159. endRo = dicRo[endTime];
  160. if (lasttime != -1)
  161. {
  162. startTime = lasttime;
  163. startRo = dicRo[lasttime];
  164. RoChange(_time);
  165. }
  166. else
  167. {
  168. if (dicRo.ContainsKey(lasttime))
  169. {
  170. this.gameObject.transform.parent.localRotation = dicRo[lasttime];
  171. }
  172. else
  173. {
  174. this.gameObject.transform.parent.localRotation = endRo;
  175. }
  176. return;
  177. //startTime = endTime;
  178. //startRo = endRo;
  179. }
  180. }
  181. else
  182. {
  183. lasttime = value.Current;
  184. }
  185. }
  186. }
  187. }
  188. public void RoAllBodyChange(int _time)
  189. {
  190. Quaternion _nextRo = new Quaternion();
  191. Vector3 _nextPos = new Vector3();
  192. if (endTime <= _time)
  193. {
  194. this.gameObject.transform.localRotation = endRo;
  195. this.gameObject.transform.localPosition = endPos;
  196. return;
  197. }
  198. else if (endTime == startTime)
  199. {
  200. this.gameObject.transform.localRotation = endRo;
  201. this.gameObject.transform.localPosition = endPos;
  202. return;
  203. }
  204. _nextRo = Quaternion.Lerp(endRo, startRo, (endTime - _time) / (endTime - startTime));
  205. Debug.Log("结束旋转" + endRo + "");
  206. Debug.Log("开始旋转" + startRo + "");
  207. this.gameObject.transform.localRotation = _nextRo;
  208. _nextPos =Vector3.Lerp(endPos, startPos, (endTime - _time) / (endTime - startTime));
  209. Debug.Log("结束距离" + endPos + "");
  210. Debug.Log("开始距离" + startPos + "");
  211. this.gameObject.transform.localPosition = _nextPos;
  212. }
  213. public void RoChange(int _time)
  214. {
  215. Quaternion _nextRo = new Quaternion();
  216. if (endTime <= _time)
  217. {
  218. this.gameObject.transform.parent.localRotation = endRo;
  219. return;
  220. }
  221. else if (endTime == startTime)
  222. {
  223. this.gameObject.transform.parent.localRotation = endRo;
  224. return;
  225. }
  226. _nextRo = Quaternion.Lerp(endRo, startRo, (endTime - _time) / (endTime - startTime));
  227. Debug.Log("结束距离" + endRo + "");
  228. Debug.Log("开始距离" + startRo + "");
  229. this.gameObject.transform.parent.localRotation = _nextRo;
  230. }
  231. public void DicAdd(int _time)
  232. {
  233. if (allBody)
  234. {
  235. if (dicRo.ContainsKey(_time))
  236. {
  237. dicRo[_time] = this.gameObject.transform.localRotation;
  238. pos[_time] = this.gameObject.transform.localPosition;
  239. }
  240. else
  241. {
  242. dicRo.Add(_time, this.gameObject.transform.localRotation);
  243. pos[_time] = this.gameObject.transform.localPosition;
  244. }
  245. }
  246. else
  247. {
  248. if (dicRo.ContainsKey(_time))
  249. {
  250. dicRo[_time] = this.gameObject.transform.parent.localRotation;
  251. }
  252. else
  253. {
  254. dicRo.Add(_time, this.gameObject.transform.parent.localRotation);
  255. }
  256. }
  257. }
  258. // Update is called once per frame
  259. void Update()
  260. {
  261. }
  262. }
  263. }