MyTween.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. public static class MyTween
  6. {
  7. private delegate float FloatEasingFunction(float start,float end,float value);
  8. private delegate Vector3 Vector3EasingFunction(Vector3 start,Vector3 end,float value);
  9. public enum CoordinateSystem : byte
  10. {
  11. Local,
  12. Global
  13. }
  14. public enum Ease : byte
  15. {
  16. easeInQuad,
  17. easeOutQuad,
  18. easeInOutQuad,
  19. easeInCubic,
  20. easeOutCubic,
  21. easeInOutCubic,
  22. easeInQuart,
  23. easeOutQuart,
  24. easeInOutQuart,
  25. easeInQuint,
  26. easeOutQuint,
  27. easeInOutQuint,
  28. easeInSine,
  29. easeOutSine,
  30. easeInOutSine,
  31. easeInExpo,
  32. easeOutExpo,
  33. easeInOutExpo,
  34. easeInCirc,
  35. easeOutCirc,
  36. easeInOutCirc,
  37. linear,
  38. spring,
  39. easeInBounce,
  40. easeOutBounce,
  41. easeInOutBounce,
  42. easeInBack,
  43. easeOutBack,
  44. easeInOutBack,
  45. easeInElastic,
  46. easeOutElastic,
  47. easeInOutElastic,
  48. }
  49. public enum Loop : byte
  50. {
  51. none,
  52. loop,
  53. pingPong
  54. }
  55. enum VectorComponent : byte
  56. {
  57. x,
  58. y,
  59. z
  60. }
  61. //static Dictionary<int, List<PlayingAnimationInfo>
  62. static List<int> _stopAnimationList = new List<int>();
  63. static int _nextAnimationId;
  64. static float GetDeltaTime(bool useRealTime, ref float lastRealTime)
  65. {
  66. float delta;
  67. if (useRealTime)
  68. {
  69. delta = Time.realtimeSinceStartup - lastRealTime;
  70. lastRealTime = Time.realtimeSinceStartup;
  71. }
  72. else
  73. {
  74. delta = Time.deltaTime;
  75. }
  76. return delta;
  77. }
  78. public static void StopAnimation(this MonoBehaviour mb, int animationId)
  79. {
  80. //can't start coroutines if inactive
  81. if (!mb.gameObject.activeInHierarchy)
  82. {
  83. return;
  84. }
  85. _stopAnimationList.Add(animationId);
  86. mb.StartCoroutine(ClearJustInCaseAnimDidNotExist(animationId));
  87. }
  88. static IEnumerator ClearJustInCaseAnimDidNotExist(int animationId)
  89. {
  90. yield return null; //wait a frame, so if the animation existed then it would have been handled
  91. ShouldStop(animationId);
  92. }
  93. public static bool ShouldStop(int id)
  94. {
  95. for (int i = 0; i < _stopAnimationList.Count; i++)
  96. {
  97. if (_stopAnimationList[i] == id)
  98. {
  99. _stopAnimationList.RemoveAt(i);
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. #region Movement
  106. public static int MoveFrom(this MonoBehaviour mb, Vector3 moveFrom, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  107. {
  108. Transform t = mb.transform;
  109. Vector3 moveTo;
  110. if (coordinateSystem == CoordinateSystem.Global)
  111. {
  112. moveTo = t.position;
  113. t.position = moveFrom;
  114. }
  115. else
  116. {
  117. moveTo = t.localPosition;
  118. t.localPosition = moveFrom;
  119. }
  120. mb.StartCoroutine(MoveToCoroutine(++_nextAnimationId, t, moveTo, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  121. return _nextAnimationId;
  122. }
  123. public static int MoveTo(this MonoBehaviour mb, Vector3 pos, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  124. {
  125. mb.StartCoroutine(MoveToCoroutine(++_nextAnimationId, mb.transform, pos, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  126. return _nextAnimationId;
  127. }
  128. public static int MoveXTo(this MonoBehaviour mb, float x, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  129. {
  130. mb.StartCoroutine(MoveComponentToCoroutine(++_nextAnimationId, mb.transform, x, VectorComponent.x, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  131. return _nextAnimationId;
  132. }
  133. public static int MoveYTo(this MonoBehaviour mb, float y, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  134. {
  135. mb.StartCoroutine(MoveComponentToCoroutine(++_nextAnimationId, mb.transform, y, VectorComponent.y, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  136. return _nextAnimationId;
  137. }
  138. public static int MoveZTo(this MonoBehaviour mb, float z, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  139. {
  140. mb.StartCoroutine(MoveComponentToCoroutine(++_nextAnimationId, mb.transform, z, VectorComponent.z, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  141. return _nextAnimationId;
  142. }
  143. public static int MoveBy(this MonoBehaviour mb, Vector3 pos, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  144. {
  145. Transform t = mb.transform;
  146. if (coordinateSystem == CoordinateSystem.Local)
  147. {
  148. pos += t.localPosition;
  149. }
  150. else
  151. {
  152. pos += t.position;
  153. }
  154. mb.StartCoroutine(MoveToCoroutine(++_nextAnimationId, t, pos, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  155. return _nextAnimationId;
  156. }
  157. static IEnumerator MoveToCoroutine(int id, Transform transform, Vector3 endPos, float time, float delay, CoordinateSystem coordinateSystem, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  158. {
  159. if (delay > 0)
  160. {
  161. yield return new WaitForSeconds(delay);
  162. }
  163. float timeSoFar = 0;
  164. float percentage = 0;
  165. Vector3 startPos = coordinateSystem == CoordinateSystem.Local ? transform.localPosition : transform.position;
  166. bool reverse = false;
  167. bool playing = true;
  168. float lastRealTime = Time.realtimeSinceStartup;
  169. while (playing)
  170. {
  171. if (reverse)
  172. {
  173. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  174. percentage = timeSoFar / time;
  175. if (percentage < 0)
  176. {
  177. timeSoFar = 0;
  178. percentage = 0;
  179. reverse = false;
  180. --numOfLoops;
  181. if (numOfLoops == 0)
  182. {
  183. playing = false;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  190. percentage = timeSoFar / time;
  191. if (percentage > 1)
  192. {
  193. if (loop == Loop.loop)
  194. {
  195. --numOfLoops;
  196. if (numOfLoops > 0)
  197. {
  198. timeSoFar = 0;
  199. percentage = 0;
  200. }
  201. else
  202. {
  203. percentage = 1;
  204. playing = false;
  205. }
  206. }
  207. else
  208. if (loop == Loop.pingPong)
  209. {
  210. timeSoFar = time;
  211. percentage = 1;
  212. reverse = true;
  213. }
  214. else
  215. {
  216. percentage = 1;
  217. playing = false;
  218. }
  219. }
  220. }
  221. Vector3EasingFunction easeFunc = GetVector3EasingFunction(ease);
  222. Vector3 newPos = easeFunc(startPos, endPos, percentage);
  223. if (coordinateSystem == CoordinateSystem.Local)
  224. {
  225. transform.localPosition = newPos;
  226. }
  227. else
  228. {
  229. transform.position = newPos;
  230. }
  231. yield return null;
  232. if (ShouldStop(id))
  233. {
  234. yield break;
  235. }
  236. }
  237. if (OnComplete != null)
  238. {
  239. OnComplete();
  240. }
  241. }
  242. static IEnumerator MoveComponentToCoroutine(int id, Transform transform, float endPos, VectorComponent component, float time, float delay, CoordinateSystem coordinateSystem, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  243. {
  244. if (delay > 0)
  245. {
  246. yield return new WaitForSeconds(delay);
  247. }
  248. float timeSoFar = 0;
  249. float percentage = 0;
  250. Vector3 start = coordinateSystem == CoordinateSystem.Local ? transform.localPosition : transform.position;
  251. float startPos = 0;
  252. switch (component)
  253. {
  254. case VectorComponent.x:
  255. startPos = start.x;
  256. break;
  257. case VectorComponent.y:
  258. startPos = start.y;
  259. break;
  260. case VectorComponent.z:
  261. startPos = start.z;
  262. break;
  263. }
  264. bool reverse = false;
  265. bool playing = true;
  266. float lastRealTime = Time.realtimeSinceStartup;
  267. while (playing)
  268. {
  269. if (reverse)
  270. {
  271. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  272. percentage = timeSoFar / time;
  273. if (percentage < 0)
  274. {
  275. timeSoFar = 0;
  276. percentage = 0;
  277. reverse = false;
  278. --numOfLoops;
  279. if (numOfLoops == 0)
  280. {
  281. playing = false;
  282. }
  283. }
  284. }
  285. else
  286. {
  287. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  288. percentage = timeSoFar / time;
  289. if (percentage > 1)
  290. {
  291. if (loop == Loop.loop)
  292. {
  293. --numOfLoops;
  294. if (numOfLoops > 0)
  295. {
  296. timeSoFar = 0;
  297. percentage = 0;
  298. }
  299. else
  300. {
  301. percentage = 1;
  302. playing = false;
  303. }
  304. }
  305. else
  306. if (loop == Loop.pingPong)
  307. {
  308. timeSoFar = time;
  309. percentage = 1;
  310. reverse = true;
  311. }
  312. else
  313. {
  314. percentage = 1;
  315. playing = false;
  316. }
  317. }
  318. }
  319. FloatEasingFunction easeFunc = GetFloatEasingFunction(ease);
  320. float newComponent = easeFunc(startPos, endPos, percentage);
  321. Vector3 newPos = coordinateSystem == CoordinateSystem.Local ? transform.localPosition : transform.position;
  322. switch (component)
  323. {
  324. case VectorComponent.x:
  325. newPos.x = newComponent;
  326. break;
  327. case VectorComponent.y:
  328. newPos.y = newComponent;
  329. break;
  330. case VectorComponent.z:
  331. newPos.z = newComponent;
  332. break;
  333. }
  334. if (coordinateSystem == CoordinateSystem.Local)
  335. {
  336. transform.localPosition = newPos;
  337. }
  338. else
  339. {
  340. transform.position = newPos;
  341. }
  342. yield return null;
  343. if (ShouldStop(id))
  344. {
  345. yield break;
  346. }
  347. }
  348. if (OnComplete != null)
  349. {
  350. OnComplete();
  351. }
  352. }
  353. #endregion
  354. #region Scale
  355. public static int ScaleFrom(this MonoBehaviour mb, Vector3 scaleFrom, float time, float delay = 0, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  356. {
  357. Transform t = mb.transform;
  358. Vector3 scaleTo = t.localScale;
  359. t.localScale = scaleFrom;
  360. mb.StartCoroutine(ScaleToCoroutine(++_nextAnimationId, t, scaleTo, time, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  361. return _nextAnimationId;
  362. }
  363. public static int ScaleTo(this MonoBehaviour mb, Vector3 scale, float time, float delay = 0, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  364. {
  365. mb.StartCoroutine(ScaleToCoroutine(++_nextAnimationId, mb.transform, scale, time, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  366. return _nextAnimationId;
  367. }
  368. public static int ScaleXTo(this MonoBehaviour mb, float x, float time, float delay = 0, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  369. {
  370. mb.StartCoroutine(ScaleComponentToCoroutine(++_nextAnimationId, mb.transform, x, VectorComponent.x, time, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  371. return _nextAnimationId;
  372. }
  373. public static int ScaleYTo(this MonoBehaviour mb, float y, float time, float delay = 0, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  374. {
  375. mb.StartCoroutine(ScaleComponentToCoroutine(++_nextAnimationId, mb.transform, y, VectorComponent.y, time, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  376. return _nextAnimationId;
  377. }
  378. public static int ScaleZTo(this MonoBehaviour mb, float z, float time, float delay = 0, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  379. {
  380. mb.StartCoroutine(ScaleComponentToCoroutine(++_nextAnimationId, mb.transform, z, VectorComponent.z, time, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  381. return _nextAnimationId;
  382. }
  383. static IEnumerator ScaleToCoroutine(int id, Transform transform, Vector3 endScale, float time, float delay, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  384. {
  385. if (delay > 0)
  386. {
  387. yield return new WaitForSeconds(delay);
  388. }
  389. float timeSoFar = 0;
  390. float percentage = 0;
  391. Vector3 startScale = transform.localScale;
  392. bool reverse = false;
  393. bool playing = true;
  394. float lastRealTime = Time.realtimeSinceStartup;
  395. while (playing)
  396. {
  397. if (reverse)
  398. {
  399. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  400. percentage = timeSoFar / time;
  401. if (percentage < 0)
  402. {
  403. timeSoFar = 0;
  404. percentage = 0;
  405. reverse = false;
  406. --numOfLoops;
  407. if (numOfLoops == 0)
  408. {
  409. playing = false;
  410. }
  411. }
  412. }
  413. else
  414. {
  415. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  416. percentage = timeSoFar / time;
  417. if (percentage > 1)
  418. {
  419. if (loop == Loop.loop)
  420. {
  421. --numOfLoops;
  422. if (numOfLoops > 0)
  423. {
  424. timeSoFar = 0;
  425. percentage = 0;
  426. }
  427. else
  428. {
  429. percentage = 1;
  430. playing = false;
  431. }
  432. }
  433. else
  434. if (loop == Loop.pingPong)
  435. {
  436. timeSoFar = time;
  437. percentage = 1;
  438. reverse = true;
  439. }
  440. else
  441. {
  442. percentage = 1;
  443. playing = false;
  444. }
  445. }
  446. }
  447. Vector3EasingFunction easeFunc = GetVector3EasingFunction(ease);
  448. Vector3 newScale = easeFunc(startScale, endScale, percentage);
  449. transform.localScale = newScale;
  450. yield return null;
  451. if (ShouldStop(id))
  452. {
  453. yield break;
  454. }
  455. }
  456. if (OnComplete != null)
  457. {
  458. OnComplete();
  459. }
  460. }
  461. static IEnumerator ScaleComponentToCoroutine(int id, Transform transform, float endScale, VectorComponent component, float time, float delay, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  462. {
  463. if (delay > 0)
  464. {
  465. yield return new WaitForSeconds(delay);
  466. }
  467. float timeSoFar = 0;
  468. float percentage = 0;
  469. Vector3 start = transform.localScale;
  470. float startScale = 0;
  471. switch (component)
  472. {
  473. case VectorComponent.x:
  474. startScale = start.x;
  475. break;
  476. case VectorComponent.y:
  477. startScale = start.y;
  478. break;
  479. case VectorComponent.z:
  480. startScale = start.z;
  481. break;
  482. }
  483. bool reverse = false;
  484. bool playing = true;
  485. float lastRealTime = Time.realtimeSinceStartup;
  486. while (playing)
  487. {
  488. if (reverse)
  489. {
  490. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  491. percentage = timeSoFar / time;
  492. if (percentage < 0)
  493. {
  494. timeSoFar = 0;
  495. percentage = 0;
  496. reverse = false;
  497. --numOfLoops;
  498. if (numOfLoops == 0)
  499. {
  500. playing = false;
  501. }
  502. }
  503. }
  504. else
  505. {
  506. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  507. percentage = timeSoFar / time;
  508. if (percentage > 1)
  509. {
  510. if (loop == Loop.loop)
  511. {
  512. --numOfLoops;
  513. if (numOfLoops > 0)
  514. {
  515. timeSoFar = 0;
  516. percentage = 0;
  517. }
  518. else
  519. {
  520. percentage = 1;
  521. playing = false;
  522. }
  523. }
  524. else
  525. if (loop == Loop.pingPong)
  526. {
  527. timeSoFar = time;
  528. percentage = 1;
  529. reverse = true;
  530. }
  531. else
  532. {
  533. percentage = 1;
  534. playing = false;
  535. }
  536. }
  537. }
  538. FloatEasingFunction easeFunc = GetFloatEasingFunction(ease);
  539. float newComponent = easeFunc(startScale, endScale, percentage);
  540. Vector3 newScale = transform.localScale;
  541. switch (component)
  542. {
  543. case VectorComponent.x:
  544. newScale.x = newComponent;
  545. break;
  546. case VectorComponent.y:
  547. newScale.y = newComponent;
  548. break;
  549. case VectorComponent.z:
  550. newScale.z = newComponent;
  551. break;
  552. }
  553. transform.localScale = newScale;
  554. yield return null;
  555. if (ShouldStop(id))
  556. {
  557. yield break;
  558. }
  559. }
  560. if (OnComplete != null)
  561. {
  562. OnComplete();
  563. }
  564. }
  565. #endregion
  566. #region Rotation
  567. public static int RotateTo(this MonoBehaviour mb, Quaternion rot, float time, float delay = 0, CoordinateSystem coordinateSystem = CoordinateSystem.Global, Ease ease = Ease.easeOutCubic, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = int.MaxValue, bool useRealTime = false)
  568. {
  569. mb.StartCoroutine(RotateToCoroutine(++_nextAnimationId, mb.transform, rot, time, delay, coordinateSystem, ease, OnComplete, loop, numOfLoops, useRealTime));
  570. return _nextAnimationId;
  571. }
  572. static IEnumerator RotateToCoroutine(int id, Transform transform, Quaternion endRot, float time, float delay, CoordinateSystem coordinateSystem, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  573. {
  574. if (delay > 0)
  575. {
  576. yield return new WaitForSeconds(delay);
  577. }
  578. float timeSoFar = 0;
  579. float percentage = 0;
  580. Quaternion startRot = coordinateSystem == CoordinateSystem.Local ? transform.localRotation : transform.rotation;
  581. bool reverse = false;
  582. bool playing = true;
  583. float lastRealTime = Time.realtimeSinceStartup;
  584. while (playing)
  585. {
  586. if (reverse)
  587. {
  588. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  589. percentage = timeSoFar / time;
  590. if (percentage < 0)
  591. {
  592. timeSoFar = 0;
  593. percentage = 0;
  594. reverse = false;
  595. --numOfLoops;
  596. if (numOfLoops == 0)
  597. {
  598. playing = false;
  599. }
  600. }
  601. }
  602. else
  603. {
  604. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  605. percentage = timeSoFar / time;
  606. if (percentage > 1)
  607. {
  608. if (loop == Loop.loop)
  609. {
  610. --numOfLoops;
  611. if (numOfLoops > 0)
  612. {
  613. timeSoFar = 0;
  614. percentage = 0;
  615. }
  616. else
  617. {
  618. percentage = 1;
  619. playing = false;
  620. }
  621. }
  622. else
  623. if (loop == Loop.pingPong)
  624. {
  625. timeSoFar = time;
  626. percentage = 1;
  627. reverse = true;
  628. }
  629. else
  630. {
  631. percentage = 1;
  632. playing = false;
  633. }
  634. }
  635. }
  636. FloatEasingFunction easeFunc = GetFloatEasingFunction(ease);
  637. float t = easeFunc(0, 1, percentage);
  638. Quaternion newRot = Quaternion.Slerp(startRot, endRot, t);
  639. if (coordinateSystem == CoordinateSystem.Local)
  640. {
  641. transform.localRotation = newRot;
  642. }
  643. else
  644. {
  645. transform.rotation = newRot;
  646. }
  647. yield return null;
  648. if (ShouldStop(id))
  649. {
  650. yield break;
  651. }
  652. }
  653. if (OnComplete != null)
  654. {
  655. OnComplete();
  656. }
  657. }
  658. #endregion
  659. #region Value
  660. public static int ValueTo(this MonoBehaviour mb, float from, float to, float time, Action<float> OnUpdate, float delay = 0, Ease ease = Ease.linear, Action OnComplete = null, Loop loop = Loop.none, int numOfLoops = 0, bool useRealTime = false)
  661. {
  662. mb.StartCoroutine(ValueToCoroutine(++_nextAnimationId, from, to, time, OnUpdate, delay, ease, OnComplete, loop, numOfLoops, useRealTime));
  663. return _nextAnimationId;
  664. }
  665. static IEnumerator ValueToCoroutine(int id, float from, float to, float time, Action<float> OnUpdate, float delay, Ease ease, Action OnComplete, Loop loop, int numOfLoops, bool useRealTime)
  666. {
  667. if (delay > 0)
  668. {
  669. yield return new WaitForSeconds(delay);
  670. }
  671. float timeSoFar = 0;
  672. float percentage = 0;
  673. bool reverse = false;
  674. bool playing = true;
  675. float lastRealTime = Time.realtimeSinceStartup;
  676. while (playing)
  677. {
  678. if (reverse)
  679. {
  680. timeSoFar -= GetDeltaTime(useRealTime, ref lastRealTime);
  681. percentage = timeSoFar / time;
  682. if (percentage < 0)
  683. {
  684. timeSoFar = 0;
  685. percentage = 0;
  686. reverse = false;
  687. --numOfLoops;
  688. if (numOfLoops == 0)
  689. {
  690. playing = false;
  691. }
  692. }
  693. }
  694. else
  695. {
  696. timeSoFar += GetDeltaTime(useRealTime, ref lastRealTime);
  697. percentage = timeSoFar / time;
  698. if (percentage > 1)
  699. {
  700. if (loop == Loop.loop)
  701. {
  702. --numOfLoops;
  703. if (numOfLoops > 0)
  704. {
  705. percentage = 0;
  706. }
  707. else
  708. {
  709. percentage = 1;
  710. playing = false;
  711. }
  712. }
  713. else
  714. if (loop == Loop.pingPong)
  715. {
  716. timeSoFar = time;
  717. percentage = 1;
  718. reverse = true;
  719. }
  720. else
  721. {
  722. percentage = 1;
  723. playing = false;
  724. }
  725. }
  726. }
  727. FloatEasingFunction easeFunc = GetFloatEasingFunction(ease);
  728. float newValue = easeFunc(from, to, percentage);
  729. OnUpdate(newValue);
  730. yield return null;
  731. if (ShouldStop(id))
  732. {
  733. yield break;
  734. }
  735. }
  736. if (OnComplete != null)
  737. {
  738. OnComplete();
  739. }
  740. }
  741. #endregion
  742. #region Ease Mapping Functions
  743. private static FloatEasingFunction GetFloatEasingFunction(Ease ease)
  744. {
  745. switch (ease)
  746. {
  747. case Ease.easeInQuad:
  748. return FloatEaseInQuad;
  749. case Ease.easeOutQuad:
  750. return FloatEaseOutQuad;
  751. case Ease.easeInOutQuad:
  752. return FloatEaseInOutQuad;
  753. case Ease.easeInCubic:
  754. return FloatEaseInCubic;
  755. case Ease.easeOutCubic:
  756. return FloatEaseOutCubic;
  757. case Ease.easeInOutCubic:
  758. return FloatEaseInOutCubic;
  759. case Ease.easeInQuart:
  760. return FloatEaseInQuart;
  761. case Ease.easeOutQuart:
  762. return FloatEaseOutQuart;
  763. case Ease.easeInOutQuart:
  764. return FloatEaseInOutQuart;
  765. case Ease.easeInQuint:
  766. return FloatEaseInQuint;
  767. case Ease.easeOutQuint:
  768. return FloatEaseOutQuint;
  769. case Ease.easeInOutQuint:
  770. return FloatEaseInOutQuint;
  771. case Ease.easeInSine:
  772. return FloatEaseInSine;
  773. case Ease.easeOutSine:
  774. return FloatEaseOutSine;
  775. case Ease.easeInOutSine:
  776. return FloatEaseInOutSine;
  777. case Ease.easeInExpo:
  778. return FloatEaseInExpo;
  779. case Ease.easeOutExpo:
  780. return FloatEaseOutExpo;
  781. case Ease.easeInOutExpo:
  782. return FloatEaseInOutExpo;
  783. case Ease.easeInCirc:
  784. return FloatEaseInCirc;
  785. case Ease.easeOutCirc:
  786. return FloatEaseOutCirc;
  787. case Ease.easeInOutCirc:
  788. return FloatEaseInOutCirc;
  789. case Ease.linear:
  790. return FloatLinear;
  791. case Ease.spring:
  792. return FloatSpring;
  793. case Ease.easeInBounce:
  794. return FloatEaseInBounce;
  795. case Ease.easeOutBounce:
  796. return FloatEaseOutBounce;
  797. case Ease.easeInOutBounce:
  798. return FloatEaseInOutBounce;
  799. case Ease.easeInBack:
  800. return FloatEaseInBack;
  801. case Ease.easeOutBack:
  802. return FloatEaseOutBack;
  803. case Ease.easeInOutBack:
  804. return FloatEaseInOutBack;
  805. case Ease.easeInElastic:
  806. return FloatEaseInElastic;
  807. case Ease.easeOutElastic:
  808. return FloatEaseOutElastic;
  809. case Ease.easeInOutElastic:
  810. return FloatEaseInOutElastic;
  811. default:
  812. Debug.LogError("Float Easing Function " + ease + " not found");
  813. return null;
  814. }
  815. }
  816. private static Vector3EasingFunction GetVector3EasingFunction(Ease ease)
  817. {
  818. switch (ease)
  819. {
  820. case Ease.easeInQuad:
  821. return Vector3EaseInQuad;
  822. case Ease.easeOutQuad:
  823. return Vector3EaseOutQuad;
  824. case Ease.easeInOutQuad:
  825. return Vector3EaseInOutQuad;
  826. case Ease.easeInCubic:
  827. return Vector3EaseInCubic;
  828. case Ease.easeOutCubic:
  829. return Vector3EaseOutCubic;
  830. case Ease.easeInOutCubic:
  831. return Vector3EaseInOutCubic;
  832. case Ease.easeInQuart:
  833. return Vector3EaseInQuart;
  834. case Ease.easeOutQuart:
  835. return Vector3EaseOutQuart;
  836. case Ease.easeInOutQuart:
  837. return Vector3EaseInOutQuart;
  838. case Ease.easeInQuint:
  839. return Vector3EaseInQuint;
  840. case Ease.easeOutQuint:
  841. return Vector3EaseOutQuint;
  842. case Ease.easeInOutQuint:
  843. return Vector3EaseInOutQuint;
  844. case Ease.easeInSine:
  845. return Vector3EaseInSine;
  846. case Ease.easeOutSine:
  847. return Vector3EaseOutSine;
  848. case Ease.easeInOutSine:
  849. return Vector3EaseInOutSine;
  850. case Ease.easeInExpo:
  851. return Vector3EaseInExpo;
  852. case Ease.easeOutExpo:
  853. return Vector3EaseOutExpo;
  854. case Ease.easeInOutExpo:
  855. return Vector3EaseInOutExpo;
  856. case Ease.easeInCirc:
  857. return Vector3EaseInCirc;
  858. case Ease.easeOutCirc:
  859. return Vector3EaseOutCirc;
  860. case Ease.easeInOutCirc:
  861. return Vector3EaseInOutCirc;
  862. case Ease.linear:
  863. return Vector3Linear;
  864. case Ease.spring:
  865. return Vector3Spring;
  866. case Ease.easeInBounce:
  867. return Vector3EaseInBounce;
  868. case Ease.easeOutBounce:
  869. return Vector3EaseOutBounce;
  870. case Ease.easeInOutBounce:
  871. return Vector3EaseInOutBounce;
  872. case Ease.easeInBack:
  873. return Vector3EaseInBack;
  874. case Ease.easeOutBack:
  875. return Vector3EaseOutBack;
  876. case Ease.easeInOutBack:
  877. return Vector3EaseInOutBack;
  878. case Ease.easeInElastic:
  879. return Vector3EaseInElastic;
  880. case Ease.easeOutElastic:
  881. return Vector3EaseOutElastic;
  882. case Ease.easeInOutElastic:
  883. return Vector3EaseInOutElastic;
  884. default:
  885. Debug.LogError("Vector3 Easing Function " + ease + " not found");
  886. return null;
  887. }
  888. }
  889. #endregion
  890. #region Float easing functions
  891. private static float FloatLinear(float start, float end, float value)
  892. {
  893. return Mathf.Lerp(start, end, value);
  894. }
  895. private static float FloatSpring(float start, float end, float value)
  896. {
  897. value = Mathf.Clamp01(value);
  898. value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
  899. return start + (end - start) * value;
  900. }
  901. private static float FloatEaseInQuad(float start, float end, float value)
  902. {
  903. end -= start;
  904. return end * value * value + start;
  905. }
  906. private static float FloatEaseOutQuad(float start, float end, float value)
  907. {
  908. end -= start;
  909. return -end * value * (value - 2) + start;
  910. }
  911. private static float FloatEaseInOutQuad(float start, float end, float value)
  912. {
  913. value /= .5f;
  914. end -= start;
  915. if (value < 1)
  916. {
  917. return end / 2 * value * value + start;
  918. }
  919. value--;
  920. return -end / 2 * (value * (value - 2) - 1) + start;
  921. }
  922. private static float FloatEaseInCubic(float start, float end, float value)
  923. {
  924. end -= start;
  925. return end * value * value * value + start;
  926. }
  927. private static float FloatEaseOutCubic(float start, float end, float value)
  928. {
  929. value--;
  930. end -= start;
  931. return end * (value * value * value + 1) + start;
  932. }
  933. private static float FloatEaseInOutCubic(float start, float end, float value)
  934. {
  935. value /= .5f;
  936. end -= start;
  937. if (value < 1)
  938. {
  939. return end / 2 * value * value * value + start;
  940. }
  941. value -= 2;
  942. return end / 2 * (value * value * value + 2) + start;
  943. }
  944. private static float FloatEaseInQuart(float start, float end, float value)
  945. {
  946. end -= start;
  947. return end * value * value * value * value + start;
  948. }
  949. private static float FloatEaseOutQuart(float start, float end, float value)
  950. {
  951. value--;
  952. end -= start;
  953. return -end * (value * value * value * value - 1) + start;
  954. }
  955. private static float FloatEaseInOutQuart(float start, float end, float value)
  956. {
  957. value /= .5f;
  958. end -= start;
  959. if (value < 1)
  960. {
  961. return end / 2 * value * value * value * value + start;
  962. }
  963. value -= 2;
  964. return -end / 2 * (value * value * value * value - 2) + start;
  965. }
  966. private static float FloatEaseInQuint(float start, float end, float value)
  967. {
  968. end -= start;
  969. return end * value * value * value * value * value + start;
  970. }
  971. private static float FloatEaseOutQuint(float start, float end, float value)
  972. {
  973. value--;
  974. end -= start;
  975. return end * (value * value * value * value * value + 1) + start;
  976. }
  977. private static float FloatEaseInOutQuint(float start, float end, float value)
  978. {
  979. value /= .5f;
  980. end -= start;
  981. if (value < 1)
  982. {
  983. return end / 2 * value * value * value * value * value + start;
  984. }
  985. value -= 2;
  986. return end / 2 * (value * value * value * value * value + 2) + start;
  987. }
  988. private static float FloatEaseInSine(float start, float end, float value)
  989. {
  990. end -= start;
  991. return -end * Mathf.Cos(value / 1 * (Mathf.PI / 2)) + end + start;
  992. }
  993. private static float FloatEaseOutSine(float start, float end, float value)
  994. {
  995. end -= start;
  996. return end * Mathf.Sin(value / 1 * (Mathf.PI / 2)) + start;
  997. }
  998. private static float FloatEaseInOutSine(float start, float end, float value)
  999. {
  1000. end -= start;
  1001. return -end / 2 * (Mathf.Cos(Mathf.PI * value / 1) - 1) + start;
  1002. }
  1003. private static float FloatEaseInExpo(float start, float end, float value)
  1004. {
  1005. end -= start;
  1006. return end * Mathf.Pow(2, 10 * (value / 1 - 1)) + start;
  1007. }
  1008. private static float FloatEaseOutExpo(float start, float end, float value)
  1009. {
  1010. end -= start;
  1011. return end * (-Mathf.Pow(2, -10 * value / 1) + 1) + start;
  1012. }
  1013. private static float FloatEaseInOutExpo(float start, float end, float value)
  1014. {
  1015. value /= .5f;
  1016. end -= start;
  1017. if (value < 1)
  1018. {
  1019. return end / 2 * Mathf.Pow(2, 10 * (value - 1)) + start;
  1020. }
  1021. value--;
  1022. return end / 2 * (-Mathf.Pow(2, -10 * value) + 2) + start;
  1023. }
  1024. private static float FloatEaseInCirc(float start, float end, float value)
  1025. {
  1026. end -= start;
  1027. return -end * (Mathf.Sqrt(1 - value * value) - 1) + start;
  1028. }
  1029. private static float FloatEaseOutCirc(float start, float end, float value)
  1030. {
  1031. value--;
  1032. end -= start;
  1033. return end * Mathf.Sqrt(1 - value * value) + start;
  1034. }
  1035. private static float FloatEaseInOutCirc(float start, float end, float value)
  1036. {
  1037. value /= .5f;
  1038. end -= start;
  1039. if (value < 1)
  1040. {
  1041. return -end / 2 * (Mathf.Sqrt(1 - value * value) - 1) + start;
  1042. }
  1043. value -= 2;
  1044. return end / 2 * (Mathf.Sqrt(1 - value * value) + 1) + start;
  1045. }
  1046. private static float FloatEaseInBounce(float start, float end, float value)
  1047. {
  1048. end -= start;
  1049. float d = 1f;
  1050. return end - FloatEaseOutBounce(0, end, d - value) + start;
  1051. }
  1052. private static float FloatEaseOutBounce(float start, float end, float value)
  1053. {
  1054. value /= 1f;
  1055. end -= start;
  1056. if (value < (1 / 2.75f))
  1057. {
  1058. return end * (7.5625f * value * value) + start;
  1059. }
  1060. else
  1061. if (value < (2 / 2.75f))
  1062. {
  1063. value -= (1.5f / 2.75f);
  1064. return end * (7.5625f * (value) * value + .75f) + start;
  1065. }
  1066. else
  1067. if (value < (2.5 / 2.75))
  1068. {
  1069. value -= (2.25f / 2.75f);
  1070. return end * (7.5625f * (value) * value + .9375f) + start;
  1071. }
  1072. else
  1073. {
  1074. value -= (2.625f / 2.75f);
  1075. return end * (7.5625f * (value) * value + .984375f) + start;
  1076. }
  1077. }
  1078. private static float FloatEaseInOutBounce(float start, float end, float value)
  1079. {
  1080. end -= start;
  1081. float d = 1f;
  1082. if (value < d / 2)
  1083. {
  1084. return FloatEaseInBounce(0, end, value * 2) * 0.5f + start;
  1085. }
  1086. else
  1087. {
  1088. return FloatEaseOutBounce(0, end, value * 2 - d) * 0.5f + end * 0.5f + start;
  1089. }
  1090. }
  1091. private static float FloatEaseInBack(float start, float end, float value)
  1092. {
  1093. end -= start;
  1094. value /= 1;
  1095. float s = 1.70158f;
  1096. return end * (value) * value * ((s + 1) * value - s) + start;
  1097. }
  1098. private static float FloatEaseOutBack(float start, float end, float value)
  1099. {
  1100. float s = 1.70158f;
  1101. end -= start;
  1102. value = (value / 1) - 1;
  1103. return end * ((value) * value * ((s + 1) * value + s) + 1) + start;
  1104. }
  1105. private static float FloatEaseInOutBack(float start, float end, float value)
  1106. {
  1107. float s = 1.70158f;
  1108. end -= start;
  1109. value /= .5f;
  1110. if ((value) < 1)
  1111. {
  1112. s *= (1.525f);
  1113. return end / 2 * (value * value * (((s) + 1) * value - s)) + start;
  1114. }
  1115. value -= 2;
  1116. s *= (1.525f);
  1117. return end / 2 * ((value) * value * (((s) + 1) * value + s) + 2) + start;
  1118. }
  1119. private static float FloatEaseInElastic(float start, float end, float value)
  1120. {
  1121. end -= start;
  1122. float d = 1f;
  1123. float p = d * .3f;
  1124. float s = 0;
  1125. float a = 0;
  1126. if (value == 0)
  1127. {
  1128. return start;
  1129. }
  1130. if ((value /= d) == 1)
  1131. {
  1132. return start + end;
  1133. }
  1134. if (a == 0f || a < Mathf.Abs(end))
  1135. {
  1136. a = end;
  1137. s = p / 4;
  1138. }
  1139. else
  1140. {
  1141. s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  1142. }
  1143. return -(a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
  1144. }
  1145. private static float FloatEaseOutElastic(float start, float end, float value)
  1146. {
  1147. end -= start;
  1148. float d = 1f;
  1149. float p = d * .3f;
  1150. float s = 0;
  1151. float a = 0;
  1152. if (value == 0)
  1153. {
  1154. return start;
  1155. }
  1156. if ((value /= d) == 1)
  1157. {
  1158. return start + end;
  1159. }
  1160. if (a == 0f || a < Mathf.Abs(end))
  1161. {
  1162. a = end;
  1163. s = p / 4;
  1164. }
  1165. else
  1166. {
  1167. s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  1168. }
  1169. return (a * Mathf.Pow(2, -10 * value) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) + end + start);
  1170. }
  1171. private static float FloatEaseInOutElastic(float start, float end, float value)
  1172. {
  1173. end -= start;
  1174. float d = 1f;
  1175. float p = d * .3f;
  1176. float s = 0;
  1177. float a = 0;
  1178. if (value == 0)
  1179. {
  1180. return start;
  1181. }
  1182. if ((value /= d / 2) == 2)
  1183. {
  1184. return start + end;
  1185. }
  1186. if (a == 0f || a < Mathf.Abs(end))
  1187. {
  1188. a = end;
  1189. s = p / 4;
  1190. }
  1191. else
  1192. {
  1193. s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  1194. }
  1195. if (value < 1)
  1196. {
  1197. return -0.5f * (a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
  1198. }
  1199. return a * Mathf.Pow(2, -10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) * 0.5f + end + start;
  1200. }
  1201. #endregion
  1202. #region Vector3 easing functions
  1203. private static Vector3 Vector3Linear(Vector3 start, Vector3 end, float value)
  1204. {
  1205. return Vector3.Lerp(start, end, value);
  1206. }
  1207. private static Vector3 Vector3Spring(Vector3 start, Vector3 end, float value){
  1208. value = Mathf.Clamp01(value);
  1209. value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
  1210. return start + (end - start) * value;
  1211. }
  1212. private static Vector3 Vector3EaseInQuad(Vector3 start, Vector3 end, float value)
  1213. {
  1214. end -= start;
  1215. return end * value * value + start;
  1216. }
  1217. private static Vector3 Vector3EaseOutQuad(Vector3 start, Vector3 end, float value)
  1218. {
  1219. end -= start;
  1220. return -end * value * (value - 2) + start;
  1221. }
  1222. private static Vector3 Vector3EaseInOutQuad(Vector3 start, Vector3 end, float value)
  1223. {
  1224. value /= .5f;
  1225. end -= start;
  1226. if (value < 1)
  1227. {
  1228. return end / 2 * value * value + start;
  1229. }
  1230. value--;
  1231. return -end / 2 * (value * (value - 2) - 1) + start;
  1232. }
  1233. private static Vector3 Vector3EaseInCubic(Vector3 start, Vector3 end, float value)
  1234. {
  1235. end -= start;
  1236. return end * value * value * value + start;
  1237. }
  1238. private static Vector3 Vector3EaseOutCubic(Vector3 start, Vector3 end, float value)
  1239. {
  1240. value--;
  1241. end -= start;
  1242. return end * (value * value * value + 1) + start;
  1243. }
  1244. private static Vector3 Vector3EaseInOutCubic(Vector3 start, Vector3 end, float value)
  1245. {
  1246. value /= .5f;
  1247. end -= start;
  1248. if (value < 1)
  1249. {
  1250. return end / 2 * value * value * value + start;
  1251. }
  1252. value -= 2;
  1253. return end / 2 * (value * value * value + 2) + start;
  1254. }
  1255. private static Vector3 Vector3EaseInQuart(Vector3 start, Vector3 end, float value)
  1256. {
  1257. end -= start;
  1258. return end * value * value * value * value + start;
  1259. }
  1260. private static Vector3 Vector3EaseOutQuart(Vector3 start, Vector3 end, float value)
  1261. {
  1262. value--;
  1263. end -= start;
  1264. return -end * (value * value * value * value - 1) + start;
  1265. }
  1266. private static Vector3 Vector3EaseInOutQuart(Vector3 start, Vector3 end, float value)
  1267. {
  1268. value /= .5f;
  1269. end -= start;
  1270. if (value < 1)
  1271. {
  1272. return end / 2 * value * value * value * value + start;
  1273. }
  1274. value -= 2;
  1275. return -end / 2 * (value * value * value * value - 2) + start;
  1276. }
  1277. private static Vector3 Vector3EaseInQuint(Vector3 start, Vector3 end, float value)
  1278. {
  1279. end -= start;
  1280. return end * value * value * value * value * value + start;
  1281. }
  1282. private static Vector3 Vector3EaseOutQuint(Vector3 start, Vector3 end, float value)
  1283. {
  1284. value--;
  1285. end -= start;
  1286. return end * (value * value * value * value * value + 1) + start;
  1287. }
  1288. private static Vector3 Vector3EaseInOutQuint(Vector3 start, Vector3 end, float value)
  1289. {
  1290. value /= .5f;
  1291. end -= start;
  1292. if (value < 1)
  1293. {
  1294. return end / 2 * value * value * value * value * value + start;
  1295. }
  1296. value -= 2;
  1297. return end / 2 * (value * value * value * value * value + 2) + start;
  1298. }
  1299. private static Vector3 Vector3EaseInSine(Vector3 start, Vector3 end, float value)
  1300. {
  1301. end -= start;
  1302. return -end * Mathf.Cos(value / 1 * (Mathf.PI / 2)) + end + start;
  1303. }
  1304. private static Vector3 Vector3EaseOutSine(Vector3 start, Vector3 end, float value)
  1305. {
  1306. end -= start;
  1307. return end * Mathf.Sin(value / 1 * (Mathf.PI / 2)) + start;
  1308. }
  1309. private static Vector3 Vector3EaseInOutSine(Vector3 start, Vector3 end, float value)
  1310. {
  1311. end -= start;
  1312. return -end / 2 * (Mathf.Cos(Mathf.PI * value / 1) - 1) + start;
  1313. }
  1314. private static Vector3 Vector3EaseInExpo(Vector3 start, Vector3 end, float value)
  1315. {
  1316. end -= start;
  1317. return end * Mathf.Pow(2, 10 * (value / 1 - 1)) + start;
  1318. }
  1319. private static Vector3 Vector3EaseOutExpo(Vector3 start, Vector3 end, float value)
  1320. {
  1321. end -= start;
  1322. return end * (-Mathf.Pow(2, -10 * value / 1) + 1) + start;
  1323. }
  1324. private static Vector3 Vector3EaseInOutExpo(Vector3 start, Vector3 end, float value)
  1325. {
  1326. value /= .5f;
  1327. end -= start;
  1328. if (value < 1)
  1329. {
  1330. return end / 2 * Mathf.Pow(2, 10 * (value - 1)) + start;
  1331. }
  1332. value--;
  1333. return end / 2 * (-Mathf.Pow(2, -10 * value) + 2) + start;
  1334. }
  1335. private static Vector3 Vector3EaseInCirc(Vector3 start, Vector3 end, float value)
  1336. {
  1337. end -= start;
  1338. return -end * (Mathf.Sqrt(1 - value * value) - 1) + start;
  1339. }
  1340. private static Vector3 Vector3EaseOutCirc(Vector3 start, Vector3 end, float value)
  1341. {
  1342. value--;
  1343. end -= start;
  1344. return end * Mathf.Sqrt(1 - value * value) + start;
  1345. }
  1346. private static Vector3 Vector3EaseInOutCirc(Vector3 start, Vector3 end, float value)
  1347. {
  1348. value /= .5f;
  1349. end -= start;
  1350. if (value < 1)
  1351. {
  1352. return -end / 2 * (Mathf.Sqrt(1 - value * value) - 1) + start;
  1353. }
  1354. value -= 2;
  1355. return end / 2 * (Mathf.Sqrt(1 - value * value) + 1) + start;
  1356. }
  1357. private static Vector3 Vector3EaseInBounce(Vector3 start, Vector3 end, float value)
  1358. {
  1359. end -= start;
  1360. float d = 1f;
  1361. return end - Vector3EaseOutBounce(Vector3.zero, end, d - value) + start;
  1362. }
  1363. private static Vector3 Vector3EaseOutBounce(Vector3 start, Vector3 end, float value)
  1364. {
  1365. value /= 1f;
  1366. end -= start;
  1367. if (value < (1 / 2.75f))
  1368. {
  1369. return end * (7.5625f * value * value) + start;
  1370. }
  1371. else
  1372. if (value < (2 / 2.75f))
  1373. {
  1374. value -= (1.5f / 2.75f);
  1375. return end * (7.5625f * (value) * value + .75f) + start;
  1376. }
  1377. else
  1378. if (value < (2.5 / 2.75))
  1379. {
  1380. value -= (2.25f / 2.75f);
  1381. return end * (7.5625f * (value) * value + .9375f) + start;
  1382. }
  1383. else
  1384. {
  1385. value -= (2.625f / 2.75f);
  1386. return end * (7.5625f * (value) * value + .984375f) + start;
  1387. }
  1388. }
  1389. private static Vector3 Vector3EaseInOutBounce(Vector3 start, Vector3 end, float value)
  1390. {
  1391. end -= start;
  1392. float d = 1f;
  1393. if (value < d / 2)
  1394. {
  1395. return Vector3EaseInBounce(Vector3.zero, end, value * 2) * 0.5f + start;
  1396. }
  1397. else
  1398. {
  1399. return Vector3EaseOutBounce(Vector3.zero, end, value * 2 - d) * 0.5f + end * 0.5f + start;
  1400. }
  1401. }
  1402. private static Vector3 Vector3EaseInBack(Vector3 start, Vector3 end, float value)
  1403. {
  1404. end -= start;
  1405. value /= 1;
  1406. float s = 1.70158f;
  1407. return end * (value) * value * ((s + 1) * value - s) + start;
  1408. }
  1409. private static Vector3 Vector3EaseOutBack(Vector3 start, Vector3 end, float value)
  1410. {
  1411. float s = 1.70158f;
  1412. end -= start;
  1413. value = (value / 1) - 1;
  1414. return end * ((value) * value * ((s + 1) * value + s) + 1) + start;
  1415. }
  1416. private static Vector3 Vector3EaseInOutBack(Vector3 start, Vector3 end, float value)
  1417. {
  1418. float s = 1.70158f;
  1419. end -= start;
  1420. value /= .5f;
  1421. if ((value) < 1)
  1422. {
  1423. s *= (1.525f);
  1424. return end / 2 * (value * value * (((s) + 1) * value - s)) + start;
  1425. }
  1426. value -= 2;
  1427. s *= (1.525f);
  1428. return end / 2 * ((value) * value * (((s) + 1) * value + s) + 2) + start;
  1429. }
  1430. private static Vector3 Vector3EaseInElastic(Vector3 start, Vector3 end, float value)
  1431. {
  1432. return new Vector3(
  1433. FloatEaseInElastic(start.x, end.x, value),
  1434. FloatEaseInElastic(start.y, end.y, value),
  1435. FloatEaseInElastic(start.z, end.z, value));
  1436. }
  1437. private static Vector3 Vector3EaseOutElastic(Vector3 start, Vector3 end, float value)
  1438. {
  1439. return new Vector3(
  1440. FloatEaseOutElastic(start.x, end.x, value),
  1441. FloatEaseOutElastic(start.y, end.y, value),
  1442. FloatEaseOutElastic(start.z, end.z, value));
  1443. }
  1444. private static Vector3 Vector3EaseInOutElastic(Vector3 start, Vector3 end, float value)
  1445. {
  1446. return new Vector3(
  1447. FloatEaseInOutElastic(start.x, end.x, value),
  1448. FloatEaseInOutElastic(start.y, end.y, value),
  1449. FloatEaseInOutElastic(start.z, end.z, value));
  1450. }
  1451. #endregion
  1452. }