LeanTween.cs 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701
  1. //namespace DentedPixel{
  2. // LeanTween version 2.45 - http://dentedpixel.com/developer-diary/
  3. //
  4. // The MIT License (MIT)
  5. //
  6. // Copyright (c) 2017 Russell Savage - Dented Pixel
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. /*
  26. TERMS OF USE - EASING EQUATIONS#
  27. Open source under the BSD License.
  28. Copyright (c)2001 Robert Penner
  29. All rights reserved.
  30. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  31. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  32. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  33. Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /**
  37. * Pass this to the "ease" parameter, to get a different easing behavior<br><br>
  38. * <strong>Example: </strong><br>LeanTween.rotateX(gameObject, 270.0f, 1.5f).setEase(LeanTweenType.easeInBack);
  39. *
  40. * @class LeanTweenType
  41. */
  42. /**
  43. * @property {integer} linear
  44. */
  45. /**
  46. * @property {integer} easeOutQuad
  47. */
  48. /**
  49. * @property {integer} easeInQuad
  50. */
  51. /**
  52. * @property {integer} easeInOutQuad
  53. */
  54. /**
  55. * @property {integer} easeInCubic
  56. */
  57. /**
  58. * @property {integer} easeOutCubic
  59. */
  60. /**
  61. * @property {integer} easeInOutCubic
  62. */
  63. /**
  64. * @property {integer} easeInQuart
  65. */
  66. /**
  67. * @property {integer} easeOutQuart
  68. */
  69. /**
  70. * @property {integer} easeInOutQuart
  71. */
  72. /**
  73. * @property {integer} easeInQuint
  74. */
  75. /**
  76. * @property {integer} easeOutQuint
  77. */
  78. /**
  79. * @property {integer} easeInOutQuint
  80. */
  81. /**
  82. * @property {integer} easeInSine
  83. */
  84. /**
  85. * @property {integer} easeOutSine
  86. */
  87. /**
  88. * @property {integer} easeInOutSine
  89. */
  90. /**
  91. * @property {integer} easeInExpo
  92. */
  93. /**
  94. * @property {integer} easeOutExpo
  95. */
  96. /**
  97. * @property {integer} easeInOutExpo
  98. */
  99. /**
  100. * @property {integer} easeInCirc
  101. */
  102. /**
  103. * @property {integer} easeOutCirc
  104. */
  105. /**
  106. * @property {integer} easeInOutCirc
  107. */
  108. /**
  109. * @property {integer} easeInBounce
  110. */
  111. /**
  112. * @property {integer} easeOutBounce
  113. */
  114. /**
  115. * @property {integer} easeInOutBounce
  116. */
  117. /**
  118. * @property {integer} easeInBack
  119. */
  120. /**
  121. * @property {integer} easeOutBack
  122. */
  123. /**
  124. * @property {integer} easeInOutBack
  125. */
  126. /**
  127. * @property {integer} easeInElastic
  128. */
  129. /**
  130. * @property {integer} easeOutElastic
  131. */
  132. /**
  133. * @property {integer} easeInOutElastic
  134. */
  135. /**
  136. * @property {integer} punch
  137. */
  138. using UnityEngine;
  139. using System;
  140. using System.Collections.Generic;
  141. public enum TweenAction{
  142. MOVE_X,
  143. MOVE_Y,
  144. MOVE_Z,
  145. MOVE_LOCAL_X,
  146. MOVE_LOCAL_Y,
  147. MOVE_LOCAL_Z,
  148. MOVE_CURVED,
  149. MOVE_CURVED_LOCAL,
  150. MOVE_SPLINE,
  151. MOVE_SPLINE_LOCAL,
  152. SCALE_X,
  153. SCALE_Y,
  154. SCALE_Z,
  155. ROTATE_X,
  156. ROTATE_Y,
  157. ROTATE_Z,
  158. ROTATE_AROUND,
  159. ROTATE_AROUND_LOCAL,
  160. CANVAS_ROTATEAROUND,
  161. CANVAS_ROTATEAROUND_LOCAL,
  162. CANVAS_PLAYSPRITE,
  163. ALPHA,
  164. TEXT_ALPHA,
  165. CANVAS_ALPHA,
  166. CANVASGROUP_ALPHA,
  167. ALPHA_VERTEX,
  168. COLOR,
  169. CALLBACK_COLOR,
  170. TEXT_COLOR,
  171. CANVAS_COLOR,
  172. CANVAS_MOVE_X,
  173. CANVAS_MOVE_Y,
  174. CANVAS_MOVE_Z,
  175. CALLBACK,
  176. MOVE,
  177. MOVE_LOCAL,
  178. MOVE_TO_TRANSFORM,
  179. ROTATE,
  180. ROTATE_LOCAL,
  181. SCALE,
  182. VALUE3,
  183. GUI_MOVE,
  184. GUI_MOVE_MARGIN,
  185. GUI_SCALE,
  186. GUI_ALPHA,
  187. GUI_ROTATE,
  188. DELAYED_SOUND,
  189. CANVAS_MOVE,
  190. CANVAS_SCALE,
  191. CANVAS_SIZEDELTA,
  192. }
  193. public enum LeanTweenType{
  194. notUsed, linear, easeOutQuad, easeInQuad, easeInOutQuad, easeInCubic, easeOutCubic, easeInOutCubic, easeInQuart, easeOutQuart, easeInOutQuart,
  195. easeInQuint, easeOutQuint, easeInOutQuint, easeInSine, easeOutSine, easeInOutSine, easeInExpo, easeOutExpo, easeInOutExpo, easeInCirc, easeOutCirc, easeInOutCirc,
  196. easeInBounce, easeOutBounce, easeInOutBounce, easeInBack, easeOutBack, easeInOutBack, easeInElastic, easeOutElastic, easeInOutElastic, easeSpring, easeShake, punch, once, clamp, pingPong, animationCurve
  197. }
  198. /**
  199. * LeanTween is an efficient tweening engine for Unity3d<br><br>
  200. * <a href="#index">Index of All Methods</a> | <a href="LTDescr.html">Optional Paramaters that can be passed</a><br><br>
  201. * <strong id='optional'>Optional Parameters</strong> are passed at the end of every method<br>
  202. * <br>
  203. * <i>Example:</i><br>
  204. * LeanTween.moveX( gameObject, 1f, 1f).setEase( <a href="LeanTweenType.html">LeanTweenType</a>.easeInQuad ).setDelay(1f);<br>
  205. * <br>
  206. * You can pass the optional parameters in any order, and chain on as many as you wish!<br><br>
  207. * You can also modify this tween later, just save the unique id of the tween.<br>
  208. * <h4>Example:</h4>
  209. * int id = LeanTween.moveX(gameObject, 1f, 1f).id;<br>
  210. * <a href="LTDescr.html">LTDescr</a> d = LeanTween.<a href="#method_LeanTween.descr">descr</a>( id );<br><br>
  211. * if(d!=null){ <span style="color:gray">// if the tween has already finished it will return null</span><br>
  212. * <span style="color:gray">&nbsp;&nbsp; // change some parameters</span><br>
  213. * &nbsp;&nbsp; d.setOnComplete( onCompleteFunc ).setEase( <a href="LeanTweenType.html">LeanTweenType</a>.easeInOutBack );<br>
  214. * }
  215. *
  216. * @class LeanTween
  217. */
  218. public class LeanTween : MonoBehaviour {
  219. public static bool throwErrors = true;
  220. public static float tau = Mathf.PI*2.0f;
  221. public static float PI_DIV2 = Mathf.PI / 2.0f;
  222. private static LTSeq[] sequences;
  223. private static LTDescr[] tweens;
  224. private static int[] tweensFinished;
  225. private static LTDescr tween;
  226. private static int tweenMaxSearch = -1;
  227. private static int maxTweens = 400;
  228. private static int maxSequences = 400;
  229. private static int frameRendered= -1;
  230. private static GameObject _tweenEmpty;
  231. public static float dtEstimated = -1f;
  232. public static float dtManual;
  233. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5
  234. private static float previousRealTime;
  235. #endif
  236. public static float dtActual;
  237. private static uint global_counter = 0;
  238. private static int i;
  239. private static int j;
  240. private static int finishedCnt;
  241. public static AnimationCurve punch = new AnimationCurve( new Keyframe(0.0f, 0.0f ), new Keyframe(0.112586f, 0.9976035f ), new Keyframe(0.3120486f, -0.1720615f ), new Keyframe(0.4316337f, 0.07030682f ), new Keyframe(0.5524869f, -0.03141804f ), new Keyframe(0.6549395f, 0.003909959f ), new Keyframe(0.770987f, -0.009817753f ), new Keyframe(0.8838775f, 0.001939224f ), new Keyframe(1.0f, 0.0f ) );
  242. public static AnimationCurve shake = new AnimationCurve( new Keyframe(0f, 0f), new Keyframe(0.25f, 1f), new Keyframe(0.75f, -1f), new Keyframe(1f, 0f) ) ;
  243. public static void init(){
  244. init(maxTweens);
  245. }
  246. public static int maxSearch{
  247. get{
  248. return tweenMaxSearch;
  249. }
  250. }
  251. public static int maxSimulataneousTweens{
  252. get {
  253. return maxTweens;
  254. }
  255. }
  256. /**
  257. * Find out how many tweens you have animating at a given time
  258. *
  259. * @method LeanTween.tweensRunning
  260. * @example
  261. * Debug.Log("I have "+LeanTween.tweensRunning+" animating!");
  262. */
  263. public static int tweensRunning{
  264. get{
  265. int count = 0;
  266. for (int i = 0; i <= tweenMaxSearch; i++){
  267. if (tweens[i].toggle){
  268. count++;
  269. }
  270. }
  271. return count;
  272. }
  273. }
  274. /**
  275. * This line is optional. Here you can specify the maximum number of tweens you will use (the default is 400). This must be called before any use of LeanTween is made for it to be effective.
  276. *
  277. * @method LeanTween.init
  278. * @param {integer} maxSimultaneousTweens:int The maximum number of tweens you will use, make sure you don't go over this limit, otherwise the code will throw an error
  279. * @example
  280. * LeanTween.init( 800 );
  281. */
  282. public static void init(int maxSimultaneousTweens ){
  283. init(maxSimultaneousTweens, maxSequences);
  284. }
  285. public static void init(int maxSimultaneousTweens, int maxSimultaneousSequences){
  286. if(tweens==null){
  287. maxTweens = maxSimultaneousTweens;
  288. tweens = new LTDescr[maxTweens];
  289. tweensFinished = new int[maxTweens];
  290. _tweenEmpty = new GameObject();
  291. _tweenEmpty.name = "~LeanTween";
  292. _tweenEmpty.AddComponent(typeof(LeanTween));
  293. _tweenEmpty.isStatic = true;
  294. #if !UNITY_EDITOR
  295. _tweenEmpty.hideFlags = HideFlags.HideAndDontSave;
  296. #endif
  297. #if UNITY_EDITOR
  298. if(Application.isPlaying)
  299. DontDestroyOnLoad( _tweenEmpty );
  300. #else
  301. DontDestroyOnLoad( _tweenEmpty );
  302. #endif
  303. for(int i = 0; i < maxTweens; i++){
  304. tweens[i] = new LTDescr();
  305. }
  306. #if UNITY_5_4_OR_NEWER
  307. UnityEngine.SceneManagement.SceneManager.sceneLoaded += onLevelWasLoaded54;
  308. #endif
  309. sequences = new LTSeq[ maxSimultaneousSequences ];
  310. for(int i = 0; i < maxSimultaneousSequences; i++){
  311. sequences[i] = new LTSeq();
  312. }
  313. }
  314. }
  315. public static void reset(){
  316. if(tweens!=null){
  317. for (int i = 0; i <= tweenMaxSearch; i++){
  318. if(tweens[i]!=null)
  319. tweens[i].toggle = false;
  320. }
  321. }
  322. tweens = null;
  323. Destroy(_tweenEmpty);
  324. }
  325. public void Update(){
  326. LeanTween.update();
  327. }
  328. #if UNITY_5_4_OR_NEWER
  329. private static void onLevelWasLoaded54( UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode ){ internalOnLevelWasLoaded( scene.buildIndex ); }
  330. #else
  331. public void OnLevelWasLoaded( int lvl ){ internalOnLevelWasLoaded( lvl ); }
  332. #endif
  333. private static void internalOnLevelWasLoaded( int lvl ){
  334. // Debug.Log("reseting gui");
  335. LTGUI.reset();
  336. }
  337. private static int maxTweenReached;
  338. public static void update() {
  339. if(frameRendered != Time.frameCount){ // make sure update is only called once per frame
  340. init();
  341. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5
  342. dtEstimated = Time.realtimeSinceStartup - previousRealTime;
  343. if(dtEstimated>0.2f) // a catch put in, when at the start sometimes this number can grow unrealistically large
  344. dtEstimated = 0.2f;
  345. previousRealTime = Time.realtimeSinceStartup;
  346. #else
  347. dtEstimated = dtEstimated<0f ? 0f : dtEstimated = Time.unscaledDeltaTime;
  348. // Debug.Log("Time.unscaledDeltaTime:"+Time.unscaledDeltaTime);
  349. #endif
  350. dtActual = Time.deltaTime;
  351. maxTweenReached = 0;
  352. finishedCnt = 0;
  353. // if(tweenMaxSearch>1500)
  354. // Debug.Log("tweenMaxSearch:"+tweenMaxSearch +" maxTweens:"+maxTweens);
  355. for( int i = 0; i <= tweenMaxSearch && i < maxTweens; i++){
  356. tween = tweens[i];
  357. // if(i==0 && tweens[i].toggle)
  358. // Debug.Log("tweens["+i+"]"+tweens[i]);
  359. if(tween.toggle){
  360. maxTweenReached = i;
  361. if (tween.updateInternal()) { // returns true if the tween is finished with it's loop
  362. tweensFinished[finishedCnt] = i;
  363. finishedCnt++;
  364. }
  365. }
  366. }
  367. // Debug.Log("maxTweenReached:"+maxTweenReached);
  368. tweenMaxSearch = maxTweenReached;
  369. frameRendered = Time.frameCount;
  370. for(int i = 0; i < finishedCnt; i++){
  371. j = tweensFinished[i];
  372. tween = tweens[ j ];
  373. // Debug.Log("removing tween:"+tween);
  374. removeTween(j);
  375. if(tween.hasExtraOnCompletes && tween.trans!=null)
  376. tween.callOnCompletes();
  377. }
  378. }
  379. }
  380. public static void removeTween( int i, int uniqueId){ // Only removes the tween if the unique id matches
  381. if(tweens[i].uniqueId==uniqueId){
  382. removeTween( i );
  383. }
  384. }
  385. // This method is only used internally! Do not call this from your scripts. To cancel a tween use LeanTween.cancel
  386. public static void removeTween( int i ){
  387. if(tweens[i].toggle){
  388. tweens[i].toggle = false;
  389. //logError("Removing tween["+i+"]:"+tweens[i]);
  390. if(tweens[i].destroyOnComplete){
  391. //Debug.Log("destroying tween.type:"+tween.type);
  392. if(tweens[i].ltRect!=null){
  393. // Debug.Log("destroy i:"+i+" id:"+tweens[i].ltRect.id);
  394. LTGUI.destroy( tweens[i].ltRect.id );
  395. }else{ // check if equal to tweenEmpty
  396. if(tweens[i].trans!=null && tweens[i].trans.gameObject!=_tweenEmpty){
  397. Destroy(tweens[i].trans.gameObject);
  398. }
  399. }
  400. }
  401. //tweens[i].optional = null;
  402. startSearch = i;
  403. //Debug.Log("start search reset:"+startSearch + " i:"+i+" tweenMaxSearch:"+tweenMaxSearch);
  404. if(i+1>=tweenMaxSearch){
  405. //Debug.Log("reset to zero");
  406. startSearch = 0;
  407. //tweenMaxSearch--;
  408. }
  409. }
  410. }
  411. public static Vector3[] add(Vector3[] a, Vector3 b){
  412. Vector3[] c = new Vector3[ a.Length ];
  413. for(i=0; i<a.Length; i++){
  414. c[i] = a[i] + b;
  415. }
  416. return c;
  417. }
  418. public static float closestRot( float from, float to ){
  419. float minusWhole = 0 - (360 - to);
  420. float plusWhole = 360 + to;
  421. float toDiffAbs = Mathf.Abs( to-from );
  422. float minusDiff = Mathf.Abs(minusWhole-from);
  423. float plusDiff = Mathf.Abs(plusWhole-from);
  424. if( toDiffAbs < minusDiff && toDiffAbs < plusDiff ){
  425. return to;
  426. }else {
  427. if(minusDiff < plusDiff){
  428. return minusWhole;
  429. }else{
  430. return plusWhole;
  431. }
  432. }
  433. }
  434. /**
  435. * Cancels all tweens
  436. *
  437. * @method LeanTween.cancelAll
  438. * @param {bool} callComplete:bool (optional) if true, then the all onCompletes will run before canceling
  439. * @example LeanTween.cancelAll(true); <br>
  440. */
  441. public static void cancelAll(){
  442. cancelAll(false);
  443. }
  444. public static void cancelAll(bool callComplete){
  445. init();
  446. for (int i = 0; i <= tweenMaxSearch; i++)
  447. {
  448. if (tweens[i].trans != null){
  449. if (callComplete && tweens[i].optional.onComplete != null)
  450. tweens[i].optional.onComplete();
  451. removeTween(i);
  452. }
  453. }
  454. }
  455. /**
  456. * Cancel all tweens that are currently targeting the gameObject
  457. *
  458. * @method LeanTween.cancel
  459. * @param {GameObject} gameObject:GameObject gameObject whose tweens you wish to cancel
  460. * @param {bool} callOnComplete:bool (optional) whether to call the onComplete method before canceling
  461. * @example LeanTween.move( gameObject, new Vector3(0f,1f,2f), 1f); <br>
  462. * LeanTween.cancel( gameObject );
  463. */
  464. public static void cancel( GameObject gameObject ){
  465. cancel( gameObject, false);
  466. }
  467. public static void cancel( GameObject gameObject, bool callOnComplete ){
  468. init();
  469. Transform trans = gameObject.transform;
  470. for(int i = 0; i <= tweenMaxSearch; i++){
  471. if(tweens[i].toggle && tweens[i].trans==trans){
  472. if (callOnComplete && tweens[i].optional.onComplete != null)
  473. tweens[i].optional.onComplete();
  474. removeTween(i);
  475. }
  476. }
  477. }
  478. public static void cancel( RectTransform rect ){
  479. cancel( rect.gameObject, false);
  480. }
  481. // public static void cancel( GameObject gameObject, int uniqueId ){
  482. // if(uniqueId>=0){
  483. // init();
  484. // int backId = uniqueId & 0xFFFF;
  485. // int backCounter = uniqueId >> 16;
  486. // // Debug.Log("uniqueId:"+uniqueId+ " id:"+backId +" counter:"+backCounter + " setCounter:"+ tweens[backId].counter + " tweens[id].type:"+tweens[backId].type);
  487. // if(tweens[backId].trans==null || (tweens[backId].trans.gameObject == gameObject && tweens[backId].counter==backCounter))
  488. // removeTween((int)backId);
  489. // }
  490. // }
  491. public static void cancel( GameObject gameObject, int uniqueId, bool callOnComplete = false ){
  492. if(uniqueId>=0){
  493. init();
  494. int backId = uniqueId & 0xFFFF;
  495. int backCounter = uniqueId >> 16;
  496. // Debug.Log("uniqueId:"+uniqueId+ " id:"+backId +" counter:"+backCounter + " setCounter:"+ tw eens[backId].counter + " tweens[id].type:"+tweens[backId].type);
  497. if(tweens[backId].trans==null || (tweens[backId].trans.gameObject == gameObject && tweens[backId].counter==backCounter)) {
  498. if (callOnComplete && tweens[backId].optional.onComplete != null)
  499. tweens[backId].optional.onComplete();
  500. removeTween((int)backId);
  501. }
  502. }
  503. }
  504. public static void cancel( LTRect ltRect, int uniqueId ){
  505. if(uniqueId>=0){
  506. init();
  507. int backId = uniqueId & 0xFFFF;
  508. int backCounter = uniqueId >> 16;
  509. // Debug.Log("uniqueId:"+uniqueId+ " id:"+backId +" action:"+(TweenAction)backType + " tweens[id].type:"+tweens[backId].type);
  510. if(tweens[backId].ltRect == ltRect && tweens[backId].counter==backCounter)
  511. removeTween((int)backId);
  512. }
  513. }
  514. /**
  515. * Cancel a specific tween with the provided id
  516. *
  517. * @method LeanTween.cancel
  518. * @param {int} id:int unique id that represents that tween
  519. * @param {bool} callOnComplete:int (optional) whether to call the onComplete method before canceling
  520. * @example int id = LeanTween.move( gameObject, new Vector3(0f,1f,2f), 1f).id; <br>
  521. * LeanTween.cancel( id );
  522. */
  523. public static void cancel( int uniqueId ){
  524. cancel( uniqueId, false);
  525. }
  526. public static void cancel( int uniqueId, bool callOnComplete ){
  527. if(uniqueId>=0){
  528. init();
  529. int backId = uniqueId & 0xFFFF;
  530. int backCounter = uniqueId >> 16;
  531. if (backId > tweens.Length - 1) { // sequence
  532. int sequenceId = backId - tweens.Length;
  533. LTSeq seq = sequences[sequenceId];
  534. for (int i = 0; i < maxSequences; i++) {
  535. if (seq.current.tween != null) {
  536. int tweenId = seq.current.tween.uniqueId;
  537. int tweenIndex = tweenId & 0xFFFF;
  538. removeTween(tweenIndex);
  539. }
  540. if (seq.previous == null)
  541. break;
  542. seq.current = seq.previous;
  543. }
  544. } else { // tween
  545. // Debug.Log("uniqueId:"+uniqueId+ " id:"+backId +" action:"+(TweenAction)backType + " tweens[id].type:"+tweens[backId].type);
  546. if (tweens[backId].counter == backCounter) {
  547. if (callOnComplete && tweens[backId].optional.onComplete != null)
  548. tweens[backId].optional.onComplete();
  549. removeTween((int)backId);
  550. }
  551. }
  552. }
  553. }
  554. /**
  555. * Retrieve a tweens LTDescr object to modify
  556. *
  557. * @method LeanTween.descr
  558. * @param {int} id:int unique id that represents that tween
  559. * @example int id = LeanTween.move( gameObject, new Vector3(0f,1f,2f), 1f).setOnComplete( oldMethod ).id; <br><br>
  560. * <div style="color:gray">// later I want decide I want to change onComplete method </div>
  561. * LTDescr descr = LeanTween.descr( id );<br>
  562. * if(descr!=null) <span style="color:gray">// if the tween has already finished it will come back null</span><br>
  563. * &nbsp;&nbsp;descr.setOnComplete( newMethod );<br>
  564. */
  565. public static LTDescr descr( int uniqueId ){
  566. int backId = uniqueId & 0xFFFF;
  567. int backCounter = uniqueId >> 16;
  568. if(tweens[backId]!=null && tweens[backId].uniqueId == uniqueId && tweens[backId].counter==backCounter)
  569. return tweens[backId];
  570. for(int i = 0; i <= tweenMaxSearch; i++){
  571. if(tweens[i].uniqueId == uniqueId && tweens[i].counter==backCounter)
  572. return tweens[i];
  573. }
  574. return null;
  575. }
  576. public static LTDescr description( int uniqueId ){
  577. return descr( uniqueId );
  578. }
  579. /**
  580. * Retrieve a tweens LTDescr object(s) to modify
  581. *
  582. * @method LeanTween.descriptions
  583. * @param {GameObject} id:GameObject object whose tween descriptions you want to retrieve
  584. * @example LeanTween.move( gameObject, new Vector3(0f,1f,2f), 1f).setOnComplete( oldMethod ); <br><br>
  585. * <div style="color:gray">// later I want decide I want to change onComplete method </div>
  586. * LTDescr[] descr = LeanTween.descriptions( gameObject );<br>
  587. * if(descr.Length>0) <span style="color:gray">// make sure there is a valid description for this target</span><br>
  588. * &nbsp;&nbsp;descr[0].setOnComplete( newMethod );<span style="color:gray">// in this case we only ever expect there to be one tween on this object</span><br>
  589. */
  590. public static LTDescr[] descriptions(GameObject gameObject = null) {
  591. if (gameObject == null) return null;
  592. List<LTDescr> descrs = new List<LTDescr>();
  593. Transform trans = gameObject.transform;
  594. for (int i = 0; i <= tweenMaxSearch; i++) {
  595. if (tweens[i].toggle && tweens[i].trans == trans)
  596. descrs.Add( tweens[i] );
  597. }
  598. return descrs.ToArray();
  599. }
  600. [System.Obsolete("Use 'pause( id )' instead")]
  601. public static void pause( GameObject gameObject, int uniqueId ){
  602. pause( uniqueId );
  603. }
  604. /**
  605. * Pause all tweens for a GameObject
  606. *
  607. * @method LeanTween.pause
  608. * @param {int} id:int Id of the tween you want to pause
  609. * @example
  610. * int id = LeanTween.moveX(gameObject, 5, 1.0).id<br>
  611. * LeanTween.pause( id );<br>
  612. * // Later....<br>
  613. * LeanTween.resume( id );
  614. */
  615. public static void pause( int uniqueId ){
  616. int backId = uniqueId & 0xFFFF;
  617. int backCounter = uniqueId >> 16;
  618. if(tweens[backId].counter==backCounter){
  619. tweens[backId].pause();
  620. }
  621. }
  622. /**
  623. * Pause all tweens for a GameObject
  624. *
  625. * @method LeanTween.pause
  626. * @param {GameObject} gameObject:GameObject GameObject whose tweens you want to pause
  627. */
  628. public static void pause( GameObject gameObject ){
  629. Transform trans = gameObject.transform;
  630. for(int i = 0; i <= tweenMaxSearch; i++){
  631. if(tweens[i].trans==trans){
  632. tweens[i].pause();
  633. }
  634. }
  635. }
  636. /**
  637. * Pause all active tweens
  638. *
  639. * @method LeanTween.pauseAll
  640. */
  641. public static void pauseAll(){
  642. init();
  643. for (int i = 0; i <= tweenMaxSearch; i++){
  644. tweens[i].pause();
  645. }
  646. }
  647. /**
  648. * Resume all active tweens
  649. *
  650. * @method LeanTween.resumeAll
  651. */
  652. public static void resumeAll(){
  653. init();
  654. for (int i = 0; i <= tweenMaxSearch; i++){
  655. tweens[i].resume();
  656. }
  657. }
  658. [System.Obsolete("Use 'resume( id )' instead")]
  659. public static void resume( GameObject gameObject, int uniqueId ){
  660. resume( uniqueId );
  661. }
  662. /**
  663. * Resume a specific tween
  664. *
  665. * @method LeanTween.resume
  666. * @param {int} id:int Id of the tween you want to resume
  667. * @example
  668. * int id = LeanTween.moveX(gameObject, 5, 1.0).id<br>
  669. * LeanTween.pause( id );<br>
  670. * // Later....<br>
  671. * LeanTween.resume( id );
  672. */
  673. public static void resume( int uniqueId ){
  674. int backId = uniqueId & 0xFFFF;
  675. int backCounter = uniqueId >> 16;
  676. if(tweens[backId].counter==backCounter){
  677. tweens[backId].resume();
  678. }
  679. }
  680. /**
  681. * Resume all the tweens on a GameObject
  682. *
  683. * @method LeanTween.resume
  684. * @param {GameObject} gameObject:GameObject GameObject whose tweens you want to resume
  685. */
  686. public static void resume( GameObject gameObject ){
  687. Transform trans = gameObject.transform;
  688. for(int i = 0; i <= tweenMaxSearch; i++){
  689. if(tweens[i].trans==trans)
  690. tweens[i].resume();
  691. }
  692. }
  693. /**
  694. * Test whether or not a tween is active on a GameObject
  695. *
  696. * @method LeanTween.isTweening
  697. * @param {GameObject} gameObject:GameObject GameObject that you want to test if it is tweening
  698. */
  699. public static bool isTweening( GameObject gameObject = null ){
  700. if(gameObject==null){
  701. for(int i = 0; i <= tweenMaxSearch; i++){
  702. if(tweens[i].toggle)
  703. return true;
  704. }
  705. return false;
  706. }
  707. Transform trans = gameObject.transform;
  708. for(int i = 0; i <= tweenMaxSearch; i++){
  709. if(tweens[i].toggle && tweens[i].trans==trans)
  710. return true;
  711. }
  712. return false;
  713. }
  714. public static bool isTweening( RectTransform rect ){
  715. return isTweening(rect.gameObject);
  716. }
  717. /**
  718. * Test whether or not a tween is active or not
  719. *
  720. * @method LeanTween.isTweening
  721. * @param {GameObject} id:int id of the tween that you want to test if it is tweening
  722. * @example
  723. * int id = LeanTween.moveX(gameObject, 1f, 3f).id;<br>
  724. * if(LeanTween.isTweening( id ))<br>
  725. * &nbsp;&nbsp; &nbsp;&nbsp;Debug.Log("I am tweening!");<br>
  726. */
  727. public static bool isTweening( int uniqueId ){
  728. int backId = uniqueId & 0xFFFF;
  729. int backCounter = uniqueId >> 16;
  730. if (backId < 0 || backId >= maxTweens) return false;
  731. // Debug.Log("tweens[backId].counter:"+tweens[backId].counter+" backCounter:"+backCounter +" toggle:"+tweens[backId].toggle);
  732. if(tweens[backId].counter==backCounter && tweens[backId].toggle){
  733. return true;
  734. }
  735. return false;
  736. }
  737. public static bool isTweening( LTRect ltRect ){
  738. for( int i = 0; i <= tweenMaxSearch; i++){
  739. if(tweens[i].toggle && tweens[i].ltRect==ltRect)
  740. return true;
  741. }
  742. return false;
  743. }
  744. public static void drawBezierPath(Vector3 a, Vector3 b, Vector3 c, Vector3 d, float arrowSize = 0.0f, Transform arrowTransform = null){
  745. Vector3 last = a;
  746. Vector3 p;
  747. Vector3 aa = (-a + 3*(b-c) + d);
  748. Vector3 bb = 3*(a+c) - 6*b;
  749. Vector3 cc = 3*(b-a);
  750. float t;
  751. if(arrowSize>0.0f){
  752. Vector3 beforePos = arrowTransform.position;
  753. Quaternion beforeQ = arrowTransform.rotation;
  754. float distanceTravelled = 0f;
  755. for(float k = 1.0f; k <= 120.0f; k++){
  756. t = k / 120.0f;
  757. p = ((aa* t + (bb))* t + cc)* t + a;
  758. Gizmos.DrawLine(last, p);
  759. distanceTravelled += (p-last).magnitude;
  760. if(distanceTravelled>1f){
  761. distanceTravelled = distanceTravelled - 1f;
  762. /*float deltaY = p.y - last.y;
  763. float deltaX = p.x - last.x;
  764. float ang = Mathf.Atan(deltaY / deltaX);
  765. Vector3 arrow = p + new Vector3( Mathf.Cos(ang+2.5f), Mathf.Sin(ang+2.5f), 0f)*0.5f;
  766. Gizmos.DrawLine(p, arrow);
  767. arrow = p + new Vector3( Mathf.Cos(ang+-2.5f), Mathf.Sin(ang+-2.5f), 0f)*0.5f;
  768. Gizmos.DrawLine(p, arrow);*/
  769. arrowTransform.position = p;
  770. arrowTransform.LookAt( last, Vector3.forward );
  771. Vector3 to = arrowTransform.TransformDirection(Vector3.right);
  772. // Debug.Log("to:"+to+" tweenEmpty.transform.position:"+arrowTransform.position);
  773. Vector3 back = (last-p);
  774. back = back.normalized;
  775. Gizmos.DrawLine(p, p + (to + back)*arrowSize);
  776. to = arrowTransform.TransformDirection(-Vector3.right);
  777. Gizmos.DrawLine(p, p + (to + back)*arrowSize);
  778. }
  779. last = p;
  780. }
  781. arrowTransform.position = beforePos;
  782. arrowTransform.rotation = beforeQ;
  783. }else{
  784. for(float k = 1.0f; k <= 30.0f; k++){
  785. t = k / 30.0f;
  786. p = ((aa* t + (bb))* t + cc)* t + a;
  787. Gizmos.DrawLine(last, p);
  788. last = p;
  789. }
  790. }
  791. }
  792. public static object logError( string error ){
  793. if(throwErrors) Debug.LogError(error); else Debug.Log(error);
  794. return null;
  795. }
  796. // LeanTween 2.0 Methods
  797. public static LTDescr options(LTDescr seed){ Debug.LogError("error this function is no longer used"); return null; }
  798. public static LTDescr options(){
  799. init();
  800. bool found = false;
  801. // Debug.Log("Search start");
  802. for(j=0, i = startSearch; j <= maxTweens; i++){
  803. if(j >= maxTweens)
  804. return logError("LeanTween - You have run out of available spaces for tweening. To avoid this error increase the number of spaces to available for tweening when you initialize the LeanTween class ex: LeanTween.init( "+(maxTweens*2)+" );") as LTDescr;
  805. if(i>=maxTweens)
  806. i = 0;
  807. // Debug.Log("searching i:"+i);
  808. if(tweens[i].toggle==false){
  809. if(i+1>tweenMaxSearch)
  810. tweenMaxSearch = i+1;
  811. startSearch = i + 1;
  812. found = true;
  813. break;
  814. }
  815. j++;
  816. }
  817. if(found==false)
  818. logError("no available tween found!");
  819. // Debug.Log("new tween with i:"+i+" counter:"+tweens[i].counter+" tweenMaxSearch:"+tweenMaxSearch+" tween:"+tweens[i]);
  820. tweens[i].reset();
  821. global_counter++;
  822. if(global_counter>0x8000)
  823. global_counter = 0;
  824. tweens[i].setId( (uint)i, global_counter );
  825. return tweens[i];
  826. }
  827. public static GameObject tweenEmpty{
  828. get{
  829. init(maxTweens);
  830. return _tweenEmpty;
  831. }
  832. }
  833. public static int startSearch = 0;
  834. public static LTDescr d;
  835. private static LTDescr pushNewTween( GameObject gameObject, Vector3 to, float time, LTDescr tween ){
  836. init(maxTweens);
  837. if(gameObject==null || tween==null)
  838. return null;
  839. tween.trans = gameObject.transform;
  840. tween.to = to;
  841. tween.time = time;
  842. //tween.hasPhysics = gameObject.rigidbody!=null;
  843. return tween;
  844. }
  845. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5
  846. /**
  847. * Play a sequence of images on a Unity UI Object
  848. *
  849. * @method LeanTween.play
  850. * @param {RectTransform} rectTransform:RectTransform RectTransform that you want to play the sequence of sprites on
  851. * @param {Sprite[]} sprites:Sprite[] Sequence of sprites to be played
  852. * @return {LTDescr} LTDescr an object that distinguishes the tween
  853. * @example
  854. * LeanTween.play(gameObject.GetComponent<RectTransform>(), sprites).setLoopPingPong();
  855. */
  856. public static LTDescr play(RectTransform rectTransform, UnityEngine.Sprite[] sprites){
  857. float defaultFrameRate = 0.25f;
  858. float time = defaultFrameRate * sprites.Length;
  859. return pushNewTween(rectTransform.gameObject, new Vector3((float)sprites.Length - 1.0f,0,0), time, options().setCanvasPlaySprite().setSprites( sprites ).setRepeat(-1));
  860. }
  861. #endif
  862. /**
  863. * Fade a gameobject's material to a certain alpha value. The material's shader needs to support alpha. <a href="http://owlchemylabs.com/content/">Owl labs has some excellent efficient shaders</a>.
  864. *
  865. * @method LeanTween.alpha
  866. * @param {GameObject} gameObject:GameObject Gameobject that you wish to fade
  867. * @param {float} to:float the final alpha value (0-1)
  868. * @param {float} time:float The time with which to fade the object
  869. * @return {LTDescr} LTDescr an object that distinguishes the tween
  870. * @example
  871. * LeanTween.alpha(gameObject, 1f, 1f) .setDelay(1f);
  872. */
  873. public static LTDescr alpha(GameObject gameObject, float to, float time){
  874. LTDescr lt = pushNewTween( gameObject, new Vector3(to,0,0), time, options().setAlpha() );
  875. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  876. SpriteRenderer ren = gameObject.GetComponent<SpriteRenderer>();
  877. lt.spriteRen = ren;
  878. #endif
  879. return lt;
  880. }
  881. /**
  882. * Retrieve a sequencer object where you can easily chain together tweens and methods one after another
  883. *
  884. * @method LeanTween.sequence
  885. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  886. * @example
  887. * var seq = LeanTween.sequence();<br>
  888. * seq.add(1f); // delay everything one second<br>
  889. * seq.add( () => { // fire an event before start<br>
  890. * &nbsp;Debug.Log("I have started");<br>
  891. * });<br>
  892. * seq.add( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  893. * seq.add( () => { // fire event after tween<br>
  894. * &nbsp;Debug.Log("We are done now");<br>
  895. * });;<br>
  896. */
  897. public static LTSeq sequence( bool initSequence = true){
  898. init(maxTweens);
  899. // Loop through and find available sequence
  900. for (int i = 0; i < sequences.Length; i++) {
  901. // Debug.Log("i:" + i + " sequences[i]:" + sequences[i]);
  902. if (sequences[i].tween==null || sequences[i].tween.toggle == false) {
  903. if (sequences[i].toggle == false) {
  904. LTSeq seq = sequences[i];
  905. if (initSequence) {
  906. seq.init((uint)(i + tweens.Length), global_counter);
  907. global_counter++;
  908. if (global_counter > 0x8000)
  909. global_counter = 0;
  910. } else {
  911. seq.reset();
  912. }
  913. return seq;
  914. }
  915. }
  916. }
  917. return null;
  918. }
  919. /**
  920. * Fade a GUI Object
  921. *
  922. * @method LeanTween.alpha
  923. * @param {LTRect} ltRect:LTRect LTRect that you wish to fade
  924. * @param {float} to:float the final alpha value (0-1)
  925. * @param {float} time:float The time with which to fade the object
  926. * @return {LTDescr} LTDescr an object that distinguishes the tween
  927. * @example
  928. * LeanTween.alpha(ltRect, 1f, 1f) .setEase(LeanTweenType.easeInCirc);
  929. */
  930. public static LTDescr alpha(LTRect ltRect, float to, float time){
  931. ltRect.alphaEnabled = true;
  932. return pushNewTween( tweenEmpty, new Vector3(to,0f,0f), time, options().setGUIAlpha().setRect( ltRect ) );
  933. }
  934. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5
  935. /**
  936. * Fade a Unity UI Object
  937. *
  938. * @method LeanTween.alphaText
  939. * @param {RectTransform} rectTransform:RectTransform RectTransform associated with the Text Component you wish to fade
  940. * @param {float} to:float the final alpha value (0-1)
  941. * @param {float} time:float The time with which to fade the object
  942. * @return {LTDescr} LTDescr an object that distinguishes the tween
  943. * @example
  944. * LeanTween.alphaText(gameObject.GetComponent&lt;RectTransform&gt;(), 1f, 1f) .setEase(LeanTweenType.easeInCirc);
  945. */
  946. public static LTDescr textAlpha(RectTransform rectTransform, float to, float time){
  947. return pushNewTween(rectTransform.gameObject, new Vector3(to,0,0), time, options().setTextAlpha());
  948. }
  949. public static LTDescr alphaText(RectTransform rectTransform, float to, float time){
  950. return pushNewTween(rectTransform.gameObject, new Vector3(to,0,0), time, options().setTextAlpha());
  951. }
  952. /**
  953. * Fade a Unity UI Canvas Group
  954. *
  955. * @method LeanTween.alphaCanvas
  956. * @param {RectTransform} rectTransform:RectTransform RectTransform that the CanvasGroup is attached to
  957. * @param {float} to:float the final alpha value (0-1)
  958. * @param {float} time:float The time with which to fade the object
  959. * @return {LTDescr} LTDescr an object that distinguishes the tween
  960. * @example
  961. * LeanTween.alphaCanvas(gameObject.GetComponent&lt;RectTransform&gt;(), 0f, 1f) .setLoopPingPong();
  962. */
  963. public static LTDescr alphaCanvas(CanvasGroup canvasGroup, float to, float time){
  964. return pushNewTween(canvasGroup.gameObject, new Vector3(to,0,0), time, options().setCanvasGroupAlpha());
  965. }
  966. #endif
  967. /**
  968. * This works by tweening the vertex colors directly.<br>
  969. <br>
  970. Vertex-based coloring is useful because you avoid making a copy of your
  971. object's material for each instance that needs a different color.<br>
  972. <br>
  973. A shader that supports vertex colors is required for it to work
  974. (for example the shaders in Mobile/Particles/)
  975. *
  976. * @method LeanTween.alphaVertex
  977. * @param {GameObject} gameObject:GameObject Gameobject that you wish to alpha
  978. * @param {float} to:float The alpha value you wish to tween to
  979. * @param {float} time:float The time with which to delay before calling the function
  980. * @return {LTDescr} LTDescr an object that distinguishes the tween
  981. */
  982. public static LTDescr alphaVertex(GameObject gameObject, float to, float time){
  983. return pushNewTween( gameObject, new Vector3(to,0f,0f), time, options().setAlphaVertex() );
  984. }
  985. /**
  986. * Change a gameobject's material to a certain color value. The material's shader needs to support color tinting. <a href="http://owlchemylabs.com/content/">Owl labs has some excellent efficient shaders</a>.
  987. *
  988. * @method LeanTween.color
  989. * @param {GameObject} gameObject:GameObject Gameobject that you wish to change the color
  990. * @param {Color} to:Color the final color value ex: Color.Red, new Color(1.0f,1.0f,0.0f,0.8f)
  991. * @param {float} time:float The time with which to fade the object
  992. * @return {LTDescr} LTDescr an object that distinguishes the tween
  993. * @example
  994. * LeanTween.color(gameObject, Color.yellow, 1f) .setDelay(1f);
  995. */
  996. public static LTDescr color(GameObject gameObject, Color to, float time){
  997. LTDescr lt = pushNewTween( gameObject, new Vector3(1.0f, to.a, 0.0f), time, options().setColor().setPoint( new Vector3(to.r, to.g, to.b) ) );
  998. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  999. SpriteRenderer ren = gameObject.GetComponent<SpriteRenderer>();
  1000. lt.spriteRen = ren;
  1001. #endif
  1002. return lt;
  1003. }
  1004. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5
  1005. /**
  1006. * Change the color a Unity UI Object
  1007. *
  1008. * @method LeanTween.colorText
  1009. * @param {RectTransform} rectTransform:RectTransform RectTransform attached to the Text Component whose color you want to change
  1010. * @param {Color} to:Color the final alpha value ex: Color.Red, new Color(1.0f,1.0f,0.0f,0.8f)
  1011. * @param {float} time:float The time with which to fade the object
  1012. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1013. * @example
  1014. * LeanTween.colorText(gameObject.GetComponent&lt;RectTransform&gt;(), Color.yellow, 1f) .setDelay(1f);
  1015. */
  1016. public static LTDescr textColor(RectTransform rectTransform, Color to, float time){
  1017. return pushNewTween(rectTransform.gameObject, new Vector3(1.0f, to.a, 0.0f), time, options().setTextColor().setPoint(new Vector3(to.r, to.g, to.b)));
  1018. }
  1019. public static LTDescr colorText(RectTransform rectTransform, Color to, float time){
  1020. return pushNewTween(rectTransform.gameObject, new Vector3(1.0f, to.a, 0.0f), time, options().setTextColor().setPoint(new Vector3(to.r, to.g, to.b)));
  1021. }
  1022. #endif
  1023. /**
  1024. * Call a method after a specified amount of time
  1025. *
  1026. * @method LeanTween.delayedCall
  1027. * @param {GameObject} gameObject:GameObject Gameobject that you wish to associate with this delayed call
  1028. * @param {float} time:float delay The time you wish to pass before the method is called
  1029. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1030. * @example LeanTween.delayedCall(gameObject, 1f, ()=>{ <br>Debug.Log("I am called one second later!");<br> }));
  1031. */
  1032. public static LTDescr delayedCall( float delayTime, Action callback){
  1033. return pushNewTween( tweenEmpty, Vector3.zero, delayTime, options().setCallback().setOnComplete(callback) );
  1034. }
  1035. public static LTDescr delayedCall( float delayTime, Action<object> callback){
  1036. return pushNewTween( tweenEmpty, Vector3.zero, delayTime, options().setCallback().setOnComplete(callback) );
  1037. }
  1038. public static LTDescr delayedCall( GameObject gameObject, float delayTime, Action callback){
  1039. return pushNewTween( gameObject, Vector3.zero, delayTime, options().setCallback().setOnComplete(callback) );
  1040. }
  1041. public static LTDescr delayedCall( GameObject gameObject, float delayTime, Action<object> callback){
  1042. return pushNewTween( gameObject, Vector3.zero, delayTime, options().setCallback().setOnComplete(callback) );
  1043. }
  1044. public static LTDescr destroyAfter( LTRect rect, float delayTime){
  1045. return pushNewTween( tweenEmpty, Vector3.zero, delayTime, options().setCallback().setRect( rect ).setDestroyOnComplete(true) );
  1046. }
  1047. /*public static LTDescr delayedCall(GameObject gameObject, float delayTime, string callback){
  1048. return pushNewTween( gameObject, Vector3.zero, delayTime, TweenAction.CALLBACK, options().setOnComplete( callback ) );
  1049. }*/
  1050. /**
  1051. * Move a GameObject to a certain location
  1052. *
  1053. * @method LeanTween.move
  1054. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1055. * @param {Vector3} vec:Vector3 to The final positin with which to move to
  1056. * @param {float} time:float time The time to complete the tween in
  1057. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1058. * @example LeanTween.move(gameObject, new Vector3(0f,-3f,5f), 2.0f) .setEase( LeanTweenType.easeOutQuad );
  1059. */
  1060. public static LTDescr move(GameObject gameObject, Vector3 to, float time){
  1061. return pushNewTween( gameObject, to, time, options().setMove() );
  1062. }
  1063. public static LTDescr move(GameObject gameObject, Vector2 to, float time){
  1064. return pushNewTween( gameObject, new Vector3(to.x, to.y, gameObject.transform.position.z), time, options().setMove() );
  1065. }
  1066. /**
  1067. * Move a GameObject along a set of bezier curves
  1068. *
  1069. * @method LeanTween.move
  1070. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1071. * @param {Vector3[]} path:Vector3[] A set of points that define the curve(s) ex: Point1,Handle2,Handle1,Point2,...
  1072. * @param {float} time:float The time to complete the tween in
  1073. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1074. * @example
  1075. * <i>Javascript:</i><br>
  1076. * LeanTween.move(gameObject, [Vector3(0,0,0),Vector3(1,0,0),Vector3(1,0,0),Vector3(1,0,1)], 2.0) .setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br><br>
  1077. * <i>C#:</i><br>
  1078. * LeanTween.move(gameObject, new Vector3[]{new Vector3(0f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,1f)}, 1.5f).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);;<br>
  1079. */
  1080. public static LTDescr move(GameObject gameObject, Vector3[] to, float time){
  1081. d = options().setMoveCurved();
  1082. if(d.optional.path==null)
  1083. d.optional.path = new LTBezierPath( to );
  1084. else
  1085. d.optional.path.setPoints( to );
  1086. return pushNewTween( gameObject, new Vector3(1.0f,0.0f,0.0f), time, d );
  1087. }
  1088. public static LTDescr move(GameObject gameObject, LTBezierPath to, float time) {
  1089. d = options().setMoveCurved();
  1090. d.optional.path = to;
  1091. return pushNewTween(gameObject, new Vector3(1.0f, 0.0f, 0.0f), time, d);
  1092. }
  1093. public static LTDescr move(GameObject gameObject, LTSpline to, float time) {
  1094. d = options().setMoveSpline();
  1095. d.optional.spline = to;
  1096. return pushNewTween(gameObject, new Vector3(1.0f, 0.0f, 0.0f), time, d);
  1097. }
  1098. /**
  1099. * Move a GameObject through a set of points
  1100. *
  1101. * @method LeanTween.moveSpline
  1102. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1103. * @param {Vector3[]} path:Vector3[] A set of points that define the curve(s) ex: ControlStart,Pt1,Pt2,Pt3,.. ..ControlEnd<br>Note: The first and last item just define the angle of the end points, they are not actually used in the spline path itself. If you do not care about the angle you can jus set the first two items and last two items as the same value.
  1104. * @param {float} time:float The time to complete the tween in
  1105. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1106. * @example
  1107. * <i>Javascript:</i><br>
  1108. * LeanTween.moveSpline(gameObject, [Vector3(0,0,0),Vector3(1,0,0),Vector3(1,0,0),Vector3(1,0,1)], 2.0) .setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br><br>
  1109. * <i>C#:</i><br>
  1110. * LeanTween.moveSpline(gameObject, new Vector3[]{new Vector3(0f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,1f)}, 1.5f).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br>
  1111. */
  1112. public static LTDescr moveSpline(GameObject gameObject, Vector3[] to, float time){
  1113. d = options().setMoveSpline();
  1114. d.optional.spline = new LTSpline( to );
  1115. return pushNewTween( gameObject, new Vector3(1.0f,0.0f,0.0f), time, d );
  1116. }
  1117. /**
  1118. * Move a GameObject through a set of points
  1119. *
  1120. * @method LeanTween.moveSpline
  1121. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1122. * @param {LTSpline} spline:LTSpline pass a pre-existing LTSpline for the object to move along
  1123. * @param {float} time:float The time to complete the tween in
  1124. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1125. * @example
  1126. * <i>Javascript:</i><br>
  1127. * LeanTween.moveSpline(gameObject, ltSpline, 2.0) .setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br><br>
  1128. * <i>C#:</i><br>
  1129. * LeanTween.moveSpline(gameObject, ltSpline, 1.5f).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br>
  1130. */
  1131. public static LTDescr moveSpline(GameObject gameObject, LTSpline to, float time){
  1132. d = options().setMoveSpline();
  1133. d.optional.spline = to;
  1134. return pushNewTween( gameObject, new Vector3(1.0f,0.0f,0.0f), time, d );
  1135. }
  1136. /**
  1137. * Move a GameObject through a set of points, in local space
  1138. *
  1139. * @method LeanTween.moveSplineLocal
  1140. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1141. * @param {Vector3[]} path:Vector3[] A set of points that define the curve(s) ex: ControlStart,Pt1,Pt2,Pt3,.. ..ControlEnd
  1142. * @param {float} time:float The time to complete the tween in
  1143. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1144. * @example
  1145. * <i>Javascript:</i><br>
  1146. * LeanTween.moveSpline(gameObject, [Vector3(0,0,0),Vector3(1,0,0),Vector3(1,0,0),Vector3(1,0,1)], 2.0) .setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br><br>
  1147. * <i>C#:</i><br>
  1148. * LeanTween.moveSpline(gameObject, new Vector3[]{new Vector3(0f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,0f),new Vector3(1f,0f,1f)}, 1.5f).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br>
  1149. */
  1150. public static LTDescr moveSplineLocal(GameObject gameObject, Vector3[] to, float time){
  1151. d = options().setMoveSplineLocal();
  1152. d.optional.spline = new LTSpline( to );
  1153. return pushNewTween( gameObject, new Vector3(1.0f,0.0f,0.0f), time, d );
  1154. }
  1155. /**
  1156. * Move a GUI Element to a certain location
  1157. *
  1158. * @method LeanTween.move (GUI)
  1159. * @param {LTRect} ltRect:LTRect ltRect LTRect object that you wish to move
  1160. * @param {Vector2} vec:Vector2 to The final position with which to move to (pixel coordinates)
  1161. * @param {float} time:float time The time to complete the tween in
  1162. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1163. */
  1164. public static LTDescr move(LTRect ltRect, Vector2 to, float time){
  1165. return pushNewTween( tweenEmpty, to, time, options().setGUIMove().setRect( ltRect ) );
  1166. }
  1167. public static LTDescr moveMargin(LTRect ltRect, Vector2 to, float time){
  1168. return pushNewTween( tweenEmpty, to, time, options().setGUIMoveMargin().setRect( ltRect ) );
  1169. }
  1170. /**
  1171. * Move a GameObject along the x-axis
  1172. *
  1173. * @method LeanTween.moveX
  1174. * @param {GameObject} gameObject:GameObject gameObject Gameobject that you wish to move
  1175. * @param {float} to:float to The final position with which to move to
  1176. * @param {float} time:float time The time to complete the move in
  1177. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1178. */
  1179. public static LTDescr moveX(GameObject gameObject, float to, float time){
  1180. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveX() );
  1181. }
  1182. /**
  1183. * Move a GameObject along the y-axis
  1184. *
  1185. * @method LeanTween.moveY
  1186. * @param {GameObject} GameObject gameObject Gameobject that you wish to move
  1187. * @param {float} float to The final position with which to move to
  1188. * @param {float} float time The time to complete the move in
  1189. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1190. */
  1191. public static LTDescr moveY(GameObject gameObject, float to, float time){
  1192. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveY() );
  1193. }
  1194. /**
  1195. * Move a GameObject along the z-axis
  1196. *
  1197. * @method LeanTween.moveZ
  1198. * @param {GameObject} GameObject gameObject Gameobject that you wish to move
  1199. * @param {float} float to The final position with which to move to
  1200. * @param {float} float time The time to complete the move in
  1201. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1202. */
  1203. public static LTDescr moveZ(GameObject gameObject, float to, float time){
  1204. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveZ() );
  1205. }
  1206. /**
  1207. * Move a GameObject to a certain location relative to the parent transform.
  1208. *
  1209. * @method LeanTween.moveLocal
  1210. * @param {GameObject} GameObject gameObject Gameobject that you wish to rotate
  1211. * @param {Vector3} Vector3 to The final positin with which to move to
  1212. * @param {float} float time The time to complete the tween in
  1213. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1214. */
  1215. public static LTDescr moveLocal(GameObject gameObject, Vector3 to, float time){
  1216. return pushNewTween( gameObject, to, time, options().setMoveLocal() );
  1217. }
  1218. /**
  1219. * Move a GameObject along a set of bezier curves, in local space
  1220. *
  1221. * @method LeanTween.moveLocal
  1222. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1223. * @param {Vector3[]} path:Vector3[] A set of points that define the curve(s) ex: Point1,Handle1,Handle2,Point2,...
  1224. * @param {float} time:float The time to complete the tween in
  1225. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1226. * @example
  1227. * <i>Javascript:</i><br>
  1228. * LeanTween.moveLocal(gameObject, [Vector3(0,0,0),Vector3(1,0,0),Vector3(1,0,0),Vector3(1,0,1)], 2.0).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br><br>
  1229. * <i>C#:</i><br>
  1230. * LeanTween.moveLocal(gameObject, new Vector3[]{Vector3(0f,0f,0f),Vector3(1f,0f,0f),Vector3(1f,0f,0f),Vector3(1f,0f,1f)}).setEase(LeanTweenType.easeOutQuad).setOrientToPath(true);<br>
  1231. */
  1232. public static LTDescr moveLocal(GameObject gameObject, Vector3[] to, float time){
  1233. d = options().setMoveCurvedLocal();
  1234. if(d.optional.path==null)
  1235. d.optional.path = new LTBezierPath( to );
  1236. else
  1237. d.optional.path.setPoints( to );
  1238. return pushNewTween( gameObject, new Vector3(1.0f,0.0f,0.0f), time, d );
  1239. }
  1240. public static LTDescr moveLocalX(GameObject gameObject, float to, float time){
  1241. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveLocalX() );
  1242. }
  1243. public static LTDescr moveLocalY(GameObject gameObject, float to, float time){
  1244. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveLocalY() );
  1245. }
  1246. public static LTDescr moveLocalZ(GameObject gameObject, float to, float time){
  1247. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setMoveLocalZ() );
  1248. }
  1249. public static LTDescr moveLocal(GameObject gameObject, LTBezierPath to, float time) {
  1250. d = options().setMoveCurvedLocal();
  1251. d.optional.path = to;
  1252. return pushNewTween(gameObject, new Vector3(1.0f, 0.0f, 0.0f), time, d);
  1253. }
  1254. public static LTDescr moveLocal(GameObject gameObject, LTSpline to, float time) {
  1255. d = options().setMoveSplineLocal();
  1256. d.optional.spline = to;
  1257. return pushNewTween(gameObject, new Vector3(1.0f, 0.0f, 0.0f), time, d);
  1258. }
  1259. /**
  1260. * Move a GameObject to another transform
  1261. *
  1262. * @method LeanTween.move
  1263. * @param {GameObject} gameObject:GameObject Gameobject that you wish to move
  1264. * @param {Transform} destination:Transform Transform whose position the tween will finally end on
  1265. * @param {float} time:float time The time to complete the tween in
  1266. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1267. * @example LeanTween.move(gameObject, anotherTransform, 2.0f) .setEase( LeanTweenType.easeOutQuad );
  1268. */
  1269. public static LTDescr move(GameObject gameObject, Transform to, float time){
  1270. return pushNewTween(gameObject, Vector3.zero, time, options().setTo(to).setMoveToTransform() );
  1271. }
  1272. /**
  1273. * Rotate a GameObject, to values are in passed in degrees
  1274. *
  1275. * @method LeanTween.rotate
  1276. * @param {GameObject} GameObject gameObject Gameobject that you wish to rotate
  1277. * @param {Vector3} Vector3 to The final rotation with which to rotate to
  1278. * @param {float} float time The time to complete the tween in
  1279. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1280. * @example LeanTween.rotate(cube, new Vector3(180f,30f,0f), 1.5f);
  1281. */
  1282. public static LTDescr rotate(GameObject gameObject, Vector3 to, float time){
  1283. return pushNewTween( gameObject, to, time, options().setRotate() );
  1284. }
  1285. /**
  1286. * Rotate a GUI element (using an LTRect object), to a value that is in degrees
  1287. *
  1288. * @method LeanTween.rotate
  1289. * @param {LTRect} ltRect:LTRect LTRect that you wish to rotate
  1290. * @param {float} to:float The final rotation with which to rotate to
  1291. * @param {float} time:float The time to complete the tween in
  1292. * @param {Array} optional:Array Object Array where you can pass <a href="#optional">optional items</a>.
  1293. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1294. * @example
  1295. * if(GUI.Button(buttonRect.rect, "Rotate"))<br>
  1296. * LeanTween.rotate( buttonRect4, 150.0f, 1.0f).setEase(LeanTweenType.easeOutElastic);<br>
  1297. * GUI.matrix = Matrix4x4.identity;<br>
  1298. */
  1299. public static LTDescr rotate(LTRect ltRect, float to, float time){
  1300. return pushNewTween( tweenEmpty, new Vector3(to,0f,0f), time, options().setGUIRotate().setRect( ltRect ) );
  1301. }
  1302. /**
  1303. * Rotate a GameObject in the objects local space (on the transforms localEulerAngles object)
  1304. *
  1305. * @method LeanTween.rotateLocal
  1306. * @param {GameObject} gameObject:GameObject Gameobject that you wish to rotate
  1307. * @param {Vector3} to:Vector3 The final rotation with which to rotate to
  1308. * @param {float} time:float The time to complete the rotation in
  1309. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1310. */
  1311. public static LTDescr rotateLocal(GameObject gameObject, Vector3 to, float time){
  1312. return pushNewTween( gameObject, to, time, options().setRotateLocal() );
  1313. }
  1314. /**
  1315. * Rotate a GameObject only on the X axis
  1316. *
  1317. * @method LeanTween.rotateX
  1318. * @param {GameObject} GameObject Gameobject that you wish to rotate
  1319. * @param {float} to:float The final x-axis rotation with which to rotate
  1320. * @param {float} time:float The time to complete the rotation in
  1321. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1322. */
  1323. public static LTDescr rotateX(GameObject gameObject, float to, float time){
  1324. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setRotateX() );
  1325. }
  1326. /**
  1327. * Rotate a GameObject only on the Y axis
  1328. *
  1329. * @method LeanTween.rotateY
  1330. * @param {GameObject} GameObject Gameobject that you wish to rotate
  1331. * @param {float} to:float The final y-axis rotation with which to rotate
  1332. * @param {float} time:float The time to complete the rotation in
  1333. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1334. */
  1335. public static LTDescr rotateY(GameObject gameObject, float to, float time){
  1336. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setRotateY() );
  1337. }
  1338. /**
  1339. * Rotate a GameObject only on the Z axis
  1340. *
  1341. * @method LeanTween.rotateZ
  1342. * @param {GameObject} GameObject Gameobject that you wish to rotate
  1343. * @param {float} to:float The final z-axis rotation with which to rotate
  1344. * @param {float} time:float The time to complete the rotation in
  1345. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1346. */
  1347. public static LTDescr rotateZ(GameObject gameObject, float to, float time){
  1348. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setRotateZ() );
  1349. }
  1350. /**
  1351. * Rotate a GameObject around a certain Axis (the best method to use when you want to rotate beyond 180 degrees)
  1352. *
  1353. * @method LeanTween.rotateAround
  1354. * @param {GameObject} gameObject:GameObject Gameobject that you wish to rotate
  1355. * @param {Vector3} vec:Vector3 axis in which to rotate around ex: Vector3.up
  1356. * @param {float} degrees:float the degrees in which to rotate
  1357. * @param {float} time:float time The time to complete the rotation in
  1358. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1359. * @example
  1360. * <i>Example:</i><br>
  1361. * LeanTween.rotateAround ( gameObject, Vector3.left, 90f, 1f );
  1362. */
  1363. public static LTDescr rotateAround(GameObject gameObject, Vector3 axis, float add, float time){
  1364. return pushNewTween( gameObject, new Vector3(add,0f,0f), time, options().setAxis(axis).setRotateAround() );
  1365. }
  1366. /**
  1367. * Rotate a GameObject around a certain Axis in Local Space (the best method to use when you want to rotate beyond 180 degrees)
  1368. *
  1369. * @method LeanTween.rotateAroundLocal
  1370. * @param {GameObject} gameObject:GameObject Gameobject that you wish to rotate
  1371. * @param {Vector3} vec:Vector3 axis in which to rotate around ex: Vector3.up
  1372. * @param {float} degrees:float the degrees in which to rotate
  1373. * @param {float} time:float time The time to complete the rotation in
  1374. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1375. * @example
  1376. * <i>Example:</i><br>
  1377. * LeanTween.rotateAround ( gameObject, Vector3.left, 90f, 1f );
  1378. */
  1379. public static LTDescr rotateAroundLocal(GameObject gameObject, Vector3 axis, float add, float time){
  1380. return pushNewTween( gameObject, new Vector3(add,0f,0f), time, options().setRotateAroundLocal().setAxis(axis) );
  1381. }
  1382. /**
  1383. * Scale a GameObject to a certain size
  1384. *
  1385. * @method LeanTween.scale
  1386. * @param {GameObject} gameObject:GameObject gameObject Gameobject that you wish to scale
  1387. * @param {Vector3} vec:Vector3 to The size with which to tween to
  1388. * @param {float} time:float time The time to complete the tween in
  1389. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1390. */
  1391. public static LTDescr scale(GameObject gameObject, Vector3 to, float time){
  1392. return pushNewTween( gameObject, to, time, options().setScale() );
  1393. }
  1394. /**
  1395. * Scale a GUI Element to a certain width and height
  1396. *
  1397. * @method LeanTween.scale (GUI)
  1398. * @param {LTRect} LTRect ltRect LTRect object that you wish to move
  1399. * @param {Vector2} Vector2 to The final width and height to scale to (pixel based)
  1400. * @param {float} float time The time to complete the tween in
  1401. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1402. * @example
  1403. * <i>Example Javascript: </i><br>
  1404. * var bRect:LTRect = new LTRect( 0, 0, 100, 50 );<br>
  1405. * LeanTween.scale( bRect, Vector2(bRect.rect.width, bRect.rect.height) * 1.3, 0.25 ).setEase(LeanTweenType.easeOutBounce);<br>
  1406. * function OnGUI(){<br>
  1407. * &nbsp; if(GUI.Button(bRect.rect, "Scale")){ }<br>
  1408. * }<br>
  1409. * <br>
  1410. * <i>Example C#: </i> <br>
  1411. * LTRect bRect = new LTRect( 0f, 0f, 100f, 50f );<br>
  1412. * LeanTween.scale( bRect, new Vector2(150f,75f), 0.25f ).setEase(LeanTweenType.easeOutBounce);<br>
  1413. * void OnGUI(){<br>
  1414. * &nbsp; if(GUI.Button(bRect.rect, "Scale")){ }<br>
  1415. * }<br>
  1416. */
  1417. public static LTDescr scale(LTRect ltRect, Vector2 to, float time){
  1418. return pushNewTween( tweenEmpty, to, time, options().setGUIScale().setRect( ltRect ) );
  1419. }
  1420. /**
  1421. * Scale a GameObject to a certain size along the x-axis only
  1422. *
  1423. * @method LeanTween.scaleX
  1424. * @param {GameObject} gameObject:GameObject Gameobject that you wish to scale
  1425. * @param {float} scaleTo:float the size with which to scale to
  1426. * @param {float} time:float the time to complete the tween in
  1427. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1428. */
  1429. public static LTDescr scaleX(GameObject gameObject, float to, float time){
  1430. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setScaleX() );
  1431. }
  1432. /**
  1433. * Scale a GameObject to a certain size along the y-axis only
  1434. *
  1435. * @method LeanTween.scaleY
  1436. * @param {GameObject} gameObject:GameObject Gameobject that you wish to scale
  1437. * @param {float} scaleTo:float the size with which to scale to
  1438. * @param {float} time:float the time to complete the tween in
  1439. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1440. */
  1441. public static LTDescr scaleY(GameObject gameObject, float to, float time){
  1442. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setScaleY() );
  1443. }
  1444. /**
  1445. * Scale a GameObject to a certain size along the z-axis only
  1446. *
  1447. * @method LeanTween.scaleZ
  1448. * @param {GameObject} gameObject:GameObject Gameobject that you wish to scale
  1449. * @param {float} scaleTo:float the size with which to scale to
  1450. * @param {float} time:float the time to complete the tween in
  1451. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1452. */
  1453. public static LTDescr scaleZ(GameObject gameObject, float to, float time){
  1454. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setScaleZ());
  1455. }
  1456. /**
  1457. * Tween any particular value (float)
  1458. *
  1459. * @method LeanTween.value (float)
  1460. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1461. * @param {float} from:float The original value to start the tween from
  1462. * @param {Vector3} to:float The final float with which to tween to
  1463. * @param {float} time:float The time to complete the tween in
  1464. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1465. * @example
  1466. * <i>Example Javascript: </i><br>
  1467. * LeanTween.value( gameObject, 1f, 5f, 5f).setOnUpdate( function( val:float ){ <br>
  1468. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1469. * } );<br>
  1470. * <br>
  1471. * <i>Example C#: </i> <br>
  1472. * LeanTween.value( gameObject, 1f, 5f, 5f).setOnUpdate( (float val)=>{ <br>
  1473. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1474. * } );<br>
  1475. */
  1476. public static LTDescr value(GameObject gameObject, float from, float to, float time){
  1477. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setCallback().setFrom( new Vector3(from,0,0) ) );
  1478. }
  1479. public static LTDescr value(float from, float to, float time){
  1480. return pushNewTween( tweenEmpty, new Vector3(to,0,0), time, options().setCallback().setFrom( new Vector3(from,0,0) ) );
  1481. }
  1482. /**
  1483. * Tween any particular value (Vector2)
  1484. *
  1485. * @method LeanTween.value (Vector2)
  1486. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1487. * @param {Vector2} from:Vector2 The original value to start the tween from
  1488. * @param {Vector3} to:Vector2 The final Vector2 with which to tween to
  1489. * @param {float} time:float The time to complete the tween in
  1490. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1491. * @example
  1492. * <i>Example Javascript: </i><br>
  1493. * LeanTween.value( gameObject, new Vector2(1f,0f), new Vector3(5f,0f), 5f).setOnUpdate( function( val:Vector2 ){ <br>
  1494. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1495. * } );<br>
  1496. * <br>
  1497. * <i>Example C#: </i> <br>
  1498. * LeanTween.value( gameObject, new Vector3(1f,0f), new Vector3(5f,0f), 5f).setOnUpdate( (Vector2 val)=>{ <br>
  1499. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1500. * } );<br>
  1501. */
  1502. public static LTDescr value(GameObject gameObject, Vector2 from, Vector2 to, float time){
  1503. return pushNewTween( gameObject, new Vector3(to.x,to.y,0), time, options().setValue3().setTo( new Vector3(to.x,to.y,0f) ).setFrom( new Vector3(from.x,from.y,0) ) );
  1504. }
  1505. /**
  1506. * Tween any particular value (Vector3)
  1507. *
  1508. * @method LeanTween.value (Vector3)
  1509. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1510. * @param {Vector3} from:Vector3 The original value to start the tween from
  1511. * @param {Vector3} to:Vector3 The final Vector3 with which to tween to
  1512. * @param {float} time:float The time to complete the tween in
  1513. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1514. * @example
  1515. * <i>Example Javascript: </i><br>
  1516. * LeanTween.value( gameObject, new Vector3(1f,0f,0f), new Vector3(5f,0f,0f), 5f).setOnUpdate( function( val:Vector3 ){ <br>
  1517. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1518. * } );<br>
  1519. * <br>
  1520. * <i>Example C#: </i> <br>
  1521. * LeanTween.value( gameObject, new Vector3(1f,0f,0f), new Vector3(5f,0f,0f), 5f).setOnUpdate( (Vector3 val)=>{ <br>
  1522. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1523. * } );<br>
  1524. */
  1525. public static LTDescr value(GameObject gameObject, Vector3 from, Vector3 to, float time){
  1526. return pushNewTween( gameObject, to, time, options().setValue3().setFrom( from ) );
  1527. }
  1528. /**
  1529. * Tween any particular value (Color)
  1530. *
  1531. * @method LeanTween.value (Color)
  1532. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1533. * @param {Color} from:Color The original value to start the tween from
  1534. * @param {Color} to:Color The final Color with which to tween to
  1535. * @param {float} time:float The time to complete the tween in
  1536. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1537. * @example
  1538. * <i>Example Javascript: </i><br>
  1539. * LeanTween.value( gameObject, Color.red, Color.yellow, 5f).setOnUpdate( function( val:Color ){ <br>
  1540. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1541. * } );<br>
  1542. * <br>
  1543. * <i>Example C#: </i> <br>
  1544. * LeanTween.value( gameObject, Color.red, Color.yellow, 5f).setOnUpdate( (Color val)=>{ <br>
  1545. * &nbsp;Debug.Log("tweened val:"+val);<br>
  1546. * } );<br>
  1547. */
  1548. public static LTDescr value(GameObject gameObject, Color from, Color to, float time){
  1549. LTDescr lt = pushNewTween( gameObject, new Vector3(1f, to.a, 0f), time, options().setCallbackColor().setPoint( new Vector3(to.r, to.g, to.b) )
  1550. .setFromColor(from).setHasInitialized(false) );
  1551. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  1552. SpriteRenderer ren = gameObject.GetComponent<SpriteRenderer>();
  1553. lt.spriteRen = ren;
  1554. #endif
  1555. return lt;
  1556. }
  1557. /**
  1558. * Tween any particular value, it does not need to be tied to any particular type or GameObject
  1559. *
  1560. * @method LeanTween.value (float)
  1561. * @param {GameObject} GameObject gameObject GameObject with which to tie the tweening with. This is only used when you need to cancel this tween, it does not actually perform any operations on this gameObject
  1562. * @param {Action<float>} callOnUpdate:Action<float> The function that is called on every Update frame, this function needs to accept a float value ex: function updateValue( float val ){ }
  1563. * @param {float} float from The original value to start the tween from
  1564. * @param {float} float to The value to end the tween on
  1565. * @param {float} float time The time to complete the tween in
  1566. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1567. * @example
  1568. * <i>Example Javascript: </i><br>
  1569. * LeanTween.value( gameObject, updateValueExampleCallback, 180f, 270f, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1570. * function updateValueExampleCallback( val:float ){<br>
  1571. * &nbsp; Debug.Log("tweened value:"+val+" set this to whatever variable you are tweening...");<br>
  1572. * }<br>
  1573. * <br>
  1574. * <i>Example C#: </i> <br>
  1575. * LeanTween.value( gameObject, updateValueExampleCallback, 180f, 270f, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1576. * void updateValueExampleCallback( float val ){<br>
  1577. * &nbsp; Debug.Log("tweened value:"+val+" set this to whatever variable you are tweening...");<br>
  1578. * }<br>
  1579. */
  1580. public static LTDescr value(GameObject gameObject, Action<float> callOnUpdate, float from, float to, float time){
  1581. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setCallback().setTo( new Vector3(to,0,0) ).setFrom( new Vector3(from,0,0) ).setOnUpdate(callOnUpdate) );
  1582. }
  1583. /**
  1584. * Tweens any float value, it does not need to be tied to any particular type or GameObject
  1585. *
  1586. * @method LeanTween.value (float)
  1587. * @param {GameObject} GameObject gameObject GameObject with which to tie the tweening with. This is only used when you need to cancel this tween, it does not actually perform any operations on this gameObject
  1588. * @param {Action<float, float>} callOnUpdateRatio:Action<float,float> Function that's called every Update frame. It must accept two float values ex: function updateValue( float val, float ratio){ }
  1589. * @param {float} float from The original value to start the tween from
  1590. * @param {float} float to The value to end the tween on
  1591. * @param {float} float time The time to complete the tween in
  1592. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1593. * @example
  1594. * <i>Example Javascript: </i><br>
  1595. * LeanTween.value( gameObject, updateValueExampleCallback, 180f, 270f, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1596. * function updateValueExampleCallback( val:float, ratio:float ){<br>
  1597. * &nbsp; Debug.Log("tweened value:"+val+" percent complete:"+ratio*100);<br>
  1598. * }<br>
  1599. * <br>
  1600. * <i>Example C#: </i> <br>
  1601. * LeanTween.value( gameObject, updateValueExampleCallback, 180f, 270f, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1602. * void updateValueExampleCallback( float val, float ratio ){<br>
  1603. * &nbsp; Debug.Log("tweened value:"+val+" percent complete:"+ratio*100);<br>
  1604. * }<br>
  1605. */
  1606. public static LTDescr value(GameObject gameObject, Action<float, float> callOnUpdateRatio, float from, float to, float time) {
  1607. return pushNewTween(gameObject, new Vector3(to, 0, 0), time, options().setCallback().setTo(new Vector3(to, 0, 0)).setFrom(new Vector3(from, 0, 0)).setOnUpdateRatio(callOnUpdateRatio));
  1608. }
  1609. /**
  1610. * Tween from one color to another
  1611. *
  1612. * @method LeanTween.value (Color)
  1613. * @param {GameObject} GameObject gameObject GameObject with which to tie the tweening with. This is only used when you need to cancel this tween, it does not actually perform any operations on this gameObject
  1614. * @param {Action<Color>} callOnUpdate:Action<Color> The function that is called on every Update frame, this function needs to accept a color value ex: function updateValue( Color val ){ }
  1615. * @param {Color} Color from The original value to start the tween from
  1616. * @param {Color} Color to The value to end the tween on
  1617. * @param {Color} Color time The time to complete the tween in
  1618. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1619. * @example
  1620. * <i>Example Javascript: </i><br>
  1621. * LeanTween.value( gameObject, updateValueExampleCallback, Color.red, Color.green, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1622. * function updateValueExampleCallback( val:Color ){<br>
  1623. * &nbsp; Debug.Log("tweened color:"+val+" set this to whatever variable you are tweening...");<br>
  1624. * }<br>
  1625. * <br>
  1626. * <i>Example C#: </i> <br>
  1627. * LeanTween.value( gameObject, updateValueExampleCallback, Color.red, Color.green, 1f).setEase(LeanTweenType.easeOutElastic);<br>
  1628. * void updateValueExampleCallback( Color val ){<br>
  1629. * &nbsp; Debug.Log("tweened color:"+val+" set this to whatever variable you are tweening...");<br>
  1630. * }<br>
  1631. */
  1632. public static LTDescr value(GameObject gameObject, Action<Color> callOnUpdate, Color from, Color to, float time){
  1633. return pushNewTween( gameObject, new Vector3(1.0f,to.a,0.0f), time, options().setCallbackColor().setPoint( new Vector3(to.r, to.g, to.b) )
  1634. .setAxis( new Vector3(from.r, from.g, from.b) ).setFrom( new Vector3(0.0f, from.a, 0.0f) ).setHasInitialized(false).setOnUpdateColor(callOnUpdate) );
  1635. }
  1636. public static LTDescr value(GameObject gameObject, Action<Color,object> callOnUpdate, Color from, Color to, float time){
  1637. return pushNewTween( gameObject, new Vector3(1.0f,to.a,0.0f), time, options().setCallbackColor().setPoint( new Vector3(to.r, to.g, to.b) )
  1638. .setAxis( new Vector3(from.r, from.g, from.b) ).setFrom( new Vector3(0.0f, from.a, 0.0f) ).setHasInitialized(false).setOnUpdateColor(callOnUpdate) );
  1639. }
  1640. /**
  1641. * Tween any particular value (Vector2), this could be used to tween an arbitrary value like offset property
  1642. *
  1643. * @method LeanTween.value (Vector2)
  1644. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1645. * @param {Action<Vector2>} callOnUpdate:Action<Vector2> The function that is called on every Update frame, this function needs to accept a float value ex: function updateValue( Vector3 val ){ }
  1646. * @param {float} from:Vector2 The original value to start the tween from
  1647. * @param {Vector2} to:Vector2 The final Vector3 with which to tween to
  1648. * @param {float} time:float The time to complete the tween in
  1649. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1650. */
  1651. public static LTDescr value(GameObject gameObject, Action<Vector2> callOnUpdate, Vector2 from, Vector2 to, float time){
  1652. return pushNewTween( gameObject, new Vector3(to.x,to.y,0f), time, options().setValue3().setTo( new Vector3(to.x,to.y,0f) ).setFrom( new Vector3(from.x,from.y,0f) ).setOnUpdateVector2(callOnUpdate) );
  1653. }
  1654. /**
  1655. * Tween any particular value (Vector3), this could be used to tween an arbitrary property that uses a Vector
  1656. *
  1657. * @method LeanTween.value (Vector3)
  1658. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1659. * @param {Action<Vector3>} callOnUpdate:Action<Vector3> The function that is called on every Update frame, this function needs to accept a float value ex: function updateValue( Vector3 val ){ }
  1660. * @param {float} from:Vector3 The original value to start the tween from
  1661. * @param {Vector3} to:Vector3 The final Vector3 with which to tween to
  1662. * @param {float} time:float The time to complete the tween in
  1663. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1664. */
  1665. public static LTDescr value(GameObject gameObject, Action<Vector3> callOnUpdate, Vector3 from, Vector3 to, float time){
  1666. return pushNewTween( gameObject, to, time, options().setValue3().setTo( to ).setFrom( from ).setOnUpdateVector3(callOnUpdate) );
  1667. }
  1668. /**
  1669. * Tween any particular value (float)
  1670. *
  1671. * @method LeanTween.value (float,object)
  1672. * @param {GameObject} gameObject:GameObject Gameobject that you wish to attach the tween to
  1673. * @param {Action<float,object>} callOnUpdate:Action<float,object> The function that is called on every Update frame, this function needs to accept a float value ex: function updateValue( Vector3 val, object obj ){ }
  1674. * @param {float} from:float The original value to start the tween from
  1675. * @param {Vector3} to:float The final Vector3 with which to tween to
  1676. * @param {float} time:float The time to complete the tween in
  1677. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1678. */
  1679. public static LTDescr value(GameObject gameObject, Action<float,object> callOnUpdate, float from, float to, float time){
  1680. return pushNewTween( gameObject, new Vector3(to,0,0), time, options().setCallback().setTo( new Vector3(to,0,0) ).setFrom( new Vector3(from,0,0) ).setOnUpdate(callOnUpdate, gameObject) );
  1681. }
  1682. public static LTDescr delayedSound( AudioClip audio, Vector3 pos, float volume ){
  1683. //Debug.LogError("Delay sound??");
  1684. return pushNewTween( tweenEmpty, pos, 0f, options().setDelayedSound().setTo( pos ).setFrom( new Vector3(volume,0,0) ).setAudio( audio ) );
  1685. }
  1686. public static LTDescr delayedSound( GameObject gameObject, AudioClip audio, Vector3 pos, float volume ){
  1687. //Debug.LogError("Delay sound??");
  1688. return pushNewTween( gameObject, pos, 0f, options().setDelayedSound().setTo( pos ).setFrom( new Vector3(volume,0,0) ).setAudio( audio ) );
  1689. }
  1690. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5
  1691. /**
  1692. * Move a RectTransform object (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1693. *
  1694. * @method LeanTween.move (RectTransform)
  1695. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1696. * @param {Vector3} to:Vector3 The final Vector3 with which to tween to
  1697. * @param {float} time:float The time to complete the tween in
  1698. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1699. * @example LeanTween.move(gameObject.GetComponent&lt;RectTransform&gt;(), new Vector3(200f,-100f,0f), 1f).setDelay(1f);
  1700. */
  1701. public static LTDescr move(RectTransform rectTrans, Vector3 to, float time){
  1702. return pushNewTween( rectTrans.gameObject, to, time, options().setCanvasMove().setRect( rectTrans ) );
  1703. }
  1704. /**
  1705. * Move a RectTransform object affecting x-axis only (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1706. *
  1707. * @method LeanTween.moveX (RectTransform)
  1708. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1709. * @param {float} to:float The final x location with which to tween to
  1710. * @param {float} time:float The time to complete the tween in
  1711. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1712. * @example LeanTween.moveX(gameObject.GetComponent&lt;RectTransform&gt;(), 200f, 1f).setDelay(1f);
  1713. */
  1714. public static LTDescr moveX(RectTransform rectTrans, float to, float time){
  1715. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasMoveX().setRect( rectTrans ) );
  1716. }
  1717. /**
  1718. * Move a RectTransform object affecting y-axis only (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1719. *
  1720. * @method LeanTween.moveY (RectTransform)
  1721. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1722. * @param {float} to:float The final y location with which to tween to
  1723. * @param {float} time:float The time to complete the tween in
  1724. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1725. * @example LeanTween.moveY(gameObject.GetComponent&lt;RectTransform&gt;(), 200f, 1f).setDelay(1f);
  1726. */
  1727. public static LTDescr moveY(RectTransform rectTrans, float to, float time){
  1728. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasMoveY().setRect( rectTrans ) );
  1729. }
  1730. /**
  1731. * Move a RectTransform object affecting z-axis only (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1732. *
  1733. * @method LeanTween.moveZ (RectTransform)
  1734. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1735. * @param {float} to:float The final x location with which to tween to
  1736. * @param {float} time:float The time to complete the tween in
  1737. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1738. * @example LeanTween.moveZ(gameObject.GetComponent&lt;RectTransform&gt;(), 200f, 1f).setDelay(1f);
  1739. */
  1740. public static LTDescr moveZ(RectTransform rectTrans, float to, float time){
  1741. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasMoveZ().setRect( rectTrans ) );
  1742. }
  1743. /**
  1744. * Rotate a RectTransform object (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1745. *
  1746. * @method LeanTween.rotate (RectTransform)
  1747. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1748. * @param {float} to:float The degree with which to rotate the RectTransform
  1749. * @param {float} time:float The time to complete the tween in
  1750. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1751. * @example LeanTween.rotate(gameObject.GetComponent&lt;RectTransform&gt;(), 90f, 1f).setDelay(1f);
  1752. */
  1753. public static LTDescr rotate(RectTransform rectTrans, float to, float time){
  1754. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasRotateAround().setRect( rectTrans ).setAxis(Vector3.forward) );
  1755. }
  1756. public static LTDescr rotate(RectTransform rectTrans, Vector3 to, float time){
  1757. return pushNewTween( rectTrans.gameObject, to, time, options().setCanvasRotateAround().setRect( rectTrans ).setAxis(Vector3.forward) );
  1758. }
  1759. /**
  1760. * Rotate a RectTransform object (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1761. *
  1762. * @method LeanTween.rotateAround (RectTransform)
  1763. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1764. * @param {Vector3} axis:Vector3 The axis in which to rotate the RectTransform (Vector3.forward is most commonly used)
  1765. * @param {float} to:float The degree with which to rotate the RectTransform
  1766. * @param {float} time:float The time to complete the tween in
  1767. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1768. * @example LeanTween.rotateAround(gameObject.GetComponent&lt;RectTransform&gt;(), Vector3.forward, 90f, 1f).setDelay(1f);
  1769. */
  1770. public static LTDescr rotateAround(RectTransform rectTrans, Vector3 axis, float to, float time){
  1771. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasRotateAround().setRect( rectTrans ).setAxis(axis) );
  1772. }
  1773. /**
  1774. * Rotate a RectTransform object around it's local axis (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1775. *
  1776. * @method LeanTween.rotateAroundLocal (RectTransform)
  1777. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1778. * @param {Vector3} axis:Vector3 The local axis in which to rotate the RectTransform (Vector3.forward is most commonly used)
  1779. * @param {float} to:float The degree with which to rotate the RectTransform
  1780. * @param {float} time:float The time to complete the tween in
  1781. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1782. * @example LeanTween.rotateAroundLocal(gameObject.GetComponent&lt;RectTransform&gt;(), Vector3.forward, 90f, 1f).setDelay(1f);
  1783. */
  1784. public static LTDescr rotateAroundLocal(RectTransform rectTrans, Vector3 axis, float to, float time){
  1785. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasRotateAroundLocal().setRect( rectTrans ).setAxis(axis) );
  1786. }
  1787. /**
  1788. * Scale a RectTransform object (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1789. *
  1790. * @method LeanTween.scale (RectTransform)
  1791. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1792. * @param {Vector3} to:Vector3 The final Vector3 with which to tween to (localScale)
  1793. * @param {float} time:float The time to complete the tween in
  1794. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1795. * @example LeanTween.scale(gameObject.GetComponent&lt;RectTransform&gt;(), gameObject.GetComponent&lt;RectTransform&gt;().localScale*2f, 1f).setDelay(1f);
  1796. */
  1797. public static LTDescr scale(RectTransform rectTrans, Vector3 to, float time){
  1798. return pushNewTween( rectTrans.gameObject, to, time, options().setCanvasScale().setRect( rectTrans ) );
  1799. }
  1800. /**
  1801. * Change the sizeDelta of a RectTransform object (used in Unity Canvas, for Buttons, Panel, Scrollbar, etc...)
  1802. *
  1803. * @method LeanTween.size (RectTransform)
  1804. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1805. * @param {Vector2} to:Vector2 The final Vector2 the tween will end at for sizeDelta property
  1806. * @param {float} time:float The time to complete the tween in
  1807. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1808. * @example LeanTween.size(gameObject.GetComponent&lt;RectTransform&gt;(), gameObject.GetComponent&lt;RectTransform&gt;().sizeDelta*2f, 1f).setDelay(1f);
  1809. */
  1810. public static LTDescr size(RectTransform rectTrans, Vector2 to, float time){
  1811. return pushNewTween( rectTrans.gameObject, to, time, options().setCanvasSizeDelta().setRect( rectTrans ) );
  1812. }
  1813. /**
  1814. * Alpha an Image Component attached to a RectTransform (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1815. *
  1816. * @method LeanTween.alpha (RectTransform)
  1817. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1818. * @param {float} to:float The final Vector3 with which to tween to (localScale)
  1819. * @param {float} time:float The time to complete the tween in
  1820. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1821. * @example LeanTween.alpha(gameObject.GetComponent&lt;RectTransform&gt;(), 0.5f, 1f).setDelay(1f);
  1822. */
  1823. public static LTDescr alpha(RectTransform rectTrans, float to, float time){
  1824. return pushNewTween( rectTrans.gameObject, new Vector3(to,0f,0f), time, options().setCanvasAlpha().setRect( rectTrans ) );
  1825. }
  1826. /**
  1827. * Change the Color of an Image Component attached to a RectTransform (used in Unity GUI in 4.6+, for Buttons, Panel, Scrollbar, etc...)
  1828. *
  1829. * @method LeanTween.alpha (RectTransform)
  1830. * @param {RectTransform} rectTrans:RectTransform RectTransform that you wish to attach the tween to
  1831. * @param {float} to:float The final Vector3 with which to tween to (localScale)
  1832. * @param {float} time:float The time to complete the tween in
  1833. * @return {LTDescr} LTDescr an object that distinguishes the tween
  1834. * @example LeanTween.color(gameObject.GetComponent&lt;RectTransform&gt;(), 0.5f, 1f).setDelay(1f);
  1835. */
  1836. public static LTDescr color(RectTransform rectTrans, Color to, float time){
  1837. return pushNewTween( rectTrans.gameObject, new Vector3(1.0f, to.a, 0.0f), time, options().setCanvasColor().setRect( rectTrans ).setPoint( new Vector3(to.r, to.g, to.b) ) );
  1838. }
  1839. #endif
  1840. // Tweening Functions - Thanks to Robert Penner and GFX47
  1841. public static float tweenOnCurve( LTDescr tweenDescr, float ratioPassed ){
  1842. // Debug.Log("single ratio:"+ratioPassed+" tweenDescr.animationCurve.Evaluate(ratioPassed):"+tweenDescr.animationCurve.Evaluate(ratioPassed));
  1843. return tweenDescr.from.x + (tweenDescr.diff.x) * tweenDescr.optional.animationCurve.Evaluate(ratioPassed);
  1844. }
  1845. public static Vector3 tweenOnCurveVector( LTDescr tweenDescr, float ratioPassed ){
  1846. return new Vector3(tweenDescr.from.x + (tweenDescr.diff.x) * tweenDescr.optional.animationCurve.Evaluate(ratioPassed),
  1847. tweenDescr.from.y + (tweenDescr.diff.y) * tweenDescr.optional.animationCurve.Evaluate(ratioPassed),
  1848. tweenDescr.from.z + (tweenDescr.diff.z) * tweenDescr.optional.animationCurve.Evaluate(ratioPassed) );
  1849. }
  1850. public static float easeOutQuadOpt( float start, float diff, float ratioPassed ){
  1851. return -diff * ratioPassed * (ratioPassed - 2) + start;
  1852. }
  1853. public static float easeInQuadOpt( float start, float diff, float ratioPassed ){
  1854. return diff * ratioPassed * ratioPassed + start;
  1855. }
  1856. public static float easeInOutQuadOpt( float start, float diff, float ratioPassed ){
  1857. ratioPassed /= .5f;
  1858. if (ratioPassed < 1) return diff / 2 * ratioPassed * ratioPassed + start;
  1859. ratioPassed--;
  1860. return -diff / 2 * (ratioPassed * (ratioPassed - 2) - 1) + start;
  1861. }
  1862. public static Vector3 easeInOutQuadOpt( Vector3 start, Vector3 diff, float ratioPassed ){
  1863. ratioPassed /= .5f;
  1864. if (ratioPassed < 1) return diff / 2 * ratioPassed * ratioPassed + start;
  1865. ratioPassed--;
  1866. return -diff / 2 * (ratioPassed * (ratioPassed - 2) - 1) + start;
  1867. }
  1868. public static float linear(float start, float end, float val){
  1869. return Mathf.Lerp(start, end, val);
  1870. }
  1871. public static float clerp(float start, float end, float val){
  1872. float min = 0.0f;
  1873. float max = 360.0f;
  1874. float half = Mathf.Abs((max - min) / 2.0f);
  1875. float retval = 0.0f;
  1876. float diff = 0.0f;
  1877. if ((end - start) < -half){
  1878. diff = ((max - start) + end) * val;
  1879. retval = start + diff;
  1880. }else if ((end - start) > half){
  1881. diff = -((max - end) + start) * val;
  1882. retval = start + diff;
  1883. }else retval = start + (end - start) * val;
  1884. return retval;
  1885. }
  1886. public static float spring(float start, float end, float val ){
  1887. val = Mathf.Clamp01(val);
  1888. val = (Mathf.Sin(val * Mathf.PI * (0.2f + 2.5f * val * val * val)) * Mathf.Pow(1f - val, 2.2f ) + val) * (1f + (1.2f * (1f - val) ));
  1889. return start + (end - start) * val;
  1890. }
  1891. public static float easeInQuad(float start, float end, float val){
  1892. end -= start;
  1893. return end * val * val + start;
  1894. }
  1895. public static float easeOutQuad(float start, float end, float val){
  1896. end -= start;
  1897. return -end * val * (val - 2) + start;
  1898. }
  1899. public static float easeInOutQuad(float start, float end, float val){
  1900. val /= .5f;
  1901. end -= start;
  1902. if (val < 1) return end / 2 * val * val + start;
  1903. val--;
  1904. return -end / 2 * (val * (val - 2) - 1) + start;
  1905. }
  1906. public static float easeInOutQuadOpt2(float start, float diffBy2, float val, float val2){
  1907. val /= .5f;
  1908. if (val < 1) return diffBy2 * val2 + start;
  1909. val--;
  1910. return -diffBy2 * ((val2 - 2) - 1f) + start;
  1911. }
  1912. public static float easeInCubic(float start, float end, float val){
  1913. end -= start;
  1914. return end * val * val * val + start;
  1915. }
  1916. public static float easeOutCubic(float start, float end, float val){
  1917. val--;
  1918. end -= start;
  1919. return end * (val * val * val + 1) + start;
  1920. }
  1921. public static float easeInOutCubic(float start, float end, float val){
  1922. val /= .5f;
  1923. end -= start;
  1924. if (val < 1) return end / 2 * val * val * val + start;
  1925. val -= 2;
  1926. return end / 2 * (val * val * val + 2) + start;
  1927. }
  1928. public static float easeInQuart(float start, float end, float val){
  1929. end -= start;
  1930. return end * val * val * val * val + start;
  1931. }
  1932. public static float easeOutQuart(float start, float end, float val){
  1933. val--;
  1934. end -= start;
  1935. return -end * (val * val * val * val - 1) + start;
  1936. }
  1937. public static float easeInOutQuart(float start, float end, float val){
  1938. val /= .5f;
  1939. end -= start;
  1940. if (val < 1) return end / 2 * val * val * val * val + start;
  1941. val -= 2;
  1942. return -end / 2 * (val * val * val * val - 2) + start;
  1943. }
  1944. public static float easeInQuint(float start, float end, float val){
  1945. end -= start;
  1946. return end * val * val * val * val * val + start;
  1947. }
  1948. public static float easeOutQuint(float start, float end, float val){
  1949. val--;
  1950. end -= start;
  1951. return end * (val * val * val * val * val + 1) + start;
  1952. }
  1953. public static float easeInOutQuint(float start, float end, float val){
  1954. val /= .5f;
  1955. end -= start;
  1956. if (val < 1) return end / 2 * val * val * val * val * val + start;
  1957. val -= 2;
  1958. return end / 2 * (val * val * val * val * val + 2) + start;
  1959. }
  1960. public static float easeInSine(float start, float end, float val){
  1961. end -= start;
  1962. return -end * Mathf.Cos(val / 1 * (Mathf.PI / 2)) + end + start;
  1963. }
  1964. public static float easeOutSine(float start, float end, float val){
  1965. end -= start;
  1966. return end * Mathf.Sin(val / 1 * (Mathf.PI / 2)) + start;
  1967. }
  1968. public static float easeInOutSine(float start, float end, float val){
  1969. end -= start;
  1970. return -end / 2 * (Mathf.Cos(Mathf.PI * val / 1) - 1) + start;
  1971. }
  1972. public static float easeInExpo(float start, float end, float val){
  1973. end -= start;
  1974. return end * Mathf.Pow(2, 10 * (val / 1 - 1)) + start;
  1975. }
  1976. public static float easeOutExpo(float start, float end, float val){
  1977. end -= start;
  1978. return end * (-Mathf.Pow(2, -10 * val / 1) + 1) + start;
  1979. }
  1980. public static float easeInOutExpo(float start, float end, float val){
  1981. val /= .5f;
  1982. end -= start;
  1983. if (val < 1) return end / 2 * Mathf.Pow(2, 10 * (val - 1)) + start;
  1984. val--;
  1985. return end / 2 * (-Mathf.Pow(2, -10 * val) + 2) + start;
  1986. }
  1987. public static float easeInCirc(float start, float end, float val){
  1988. end -= start;
  1989. return -end * (Mathf.Sqrt(1 - val * val) - 1) + start;
  1990. }
  1991. public static float easeOutCirc(float start, float end, float val){
  1992. val--;
  1993. end -= start;
  1994. return end * Mathf.Sqrt(1 - val * val) + start;
  1995. }
  1996. public static float easeInOutCirc(float start, float end, float val){
  1997. val /= .5f;
  1998. end -= start;
  1999. if (val < 1) return -end / 2 * (Mathf.Sqrt(1 - val * val) - 1) + start;
  2000. val -= 2;
  2001. return end / 2 * (Mathf.Sqrt(1 - val * val) + 1) + start;
  2002. }
  2003. public static float easeInBounce(float start, float end, float val){
  2004. end -= start;
  2005. float d = 1f;
  2006. return end - easeOutBounce(0, end, d-val) + start;
  2007. }
  2008. public static float easeOutBounce(float start, float end, float val){
  2009. val /= 1f;
  2010. end -= start;
  2011. if (val < (1 / 2.75f)){
  2012. return end * (7.5625f * val * val) + start;
  2013. }else if (val < (2 / 2.75f)){
  2014. val -= (1.5f / 2.75f);
  2015. return end * (7.5625f * (val) * val + .75f) + start;
  2016. }else if (val < (2.5 / 2.75)){
  2017. val -= (2.25f / 2.75f);
  2018. return end * (7.5625f * (val) * val + .9375f) + start;
  2019. }else{
  2020. val -= (2.625f / 2.75f);
  2021. return end * (7.5625f * (val) * val + .984375f) + start;
  2022. }
  2023. }
  2024. public static float easeInOutBounce(float start, float end, float val){
  2025. end -= start;
  2026. float d= 1f;
  2027. if (val < d/2) return easeInBounce(0, end, val*2) * 0.5f + start;
  2028. else return easeOutBounce(0, end, val*2-d) * 0.5f + end*0.5f + start;
  2029. }
  2030. public static float easeInBack(float start, float end, float val, float overshoot = 1.0f){
  2031. end -= start;
  2032. val /= 1;
  2033. float s= 1.70158f * overshoot;
  2034. return end * (val) * val * ((s + 1) * val - s) + start;
  2035. }
  2036. public static float easeOutBack(float start, float end, float val, float overshoot = 1.0f){
  2037. float s = 1.70158f * overshoot;
  2038. end -= start;
  2039. val = (val / 1) - 1;
  2040. return end * ((val) * val * ((s + 1) * val + s) + 1) + start;
  2041. }
  2042. public static float easeInOutBack(float start, float end, float val, float overshoot = 1.0f){
  2043. float s = 1.70158f * overshoot;
  2044. end -= start;
  2045. val /= .5f;
  2046. if ((val) < 1){
  2047. s *= (1.525f) * overshoot;
  2048. return end / 2 * (val * val * (((s) + 1) * val - s)) + start;
  2049. }
  2050. val -= 2;
  2051. s *= (1.525f) * overshoot;
  2052. return end / 2 * ((val) * val * (((s) + 1) * val + s) + 2) + start;
  2053. }
  2054. public static float easeInElastic(float start, float end, float val, float overshoot = 1.0f, float period = 0.3f){
  2055. end -= start;
  2056. float p = period;
  2057. float s = 0f;
  2058. float a = 0f;
  2059. if (val == 0f) return start;
  2060. if (val == 1f) return start + end;
  2061. if (a == 0f || a < Mathf.Abs(end)){
  2062. a = end;
  2063. s = p / 4f;
  2064. }else{
  2065. s = p / (2f * Mathf.PI) * Mathf.Asin(end / a);
  2066. }
  2067. if(overshoot>1f && val>0.6f )
  2068. overshoot = 1f + ((1f-val) / 0.4f * (overshoot-1f));
  2069. // Debug.Log("ease in elastic val:"+val+" a:"+a+" overshoot:"+overshoot);
  2070. val = val-1f;
  2071. return start-(a * Mathf.Pow(2f, 10f * val) * Mathf.Sin((val - s) * (2f * Mathf.PI) / p)) * overshoot;
  2072. }
  2073. public static float easeOutElastic(float start, float end, float val, float overshoot = 1.0f, float period = 0.3f){
  2074. end -= start;
  2075. float p = period;
  2076. float s = 0f;
  2077. float a = 0f;
  2078. if (val == 0f) return start;
  2079. // Debug.Log("ease out elastic val:"+val+" a:"+a);
  2080. if (val == 1f) return start + end;
  2081. if (a == 0f || a < Mathf.Abs(end)){
  2082. a = end;
  2083. s = p / 4f;
  2084. }else{
  2085. s = p / (2f * Mathf.PI) * Mathf.Asin(end / a);
  2086. }
  2087. if(overshoot>1f && val<0.4f )
  2088. overshoot = 1f + (val / 0.4f * (overshoot-1f));
  2089. // Debug.Log("ease out elastic val:"+val+" a:"+a+" overshoot:"+overshoot);
  2090. return start + end + a * Mathf.Pow(2f, -10f * val) * Mathf.Sin((val - s) * (2f * Mathf.PI) / p) * overshoot;
  2091. }
  2092. public static float easeInOutElastic(float start, float end, float val, float overshoot = 1.0f, float period = 0.3f)
  2093. {
  2094. end -= start;
  2095. float p = period;
  2096. float s = 0f;
  2097. float a = 0f;
  2098. if (val == 0f) return start;
  2099. val = val / (1f/2f);
  2100. if (val == 2f) return start + end;
  2101. if (a == 0f || a < Mathf.Abs(end)){
  2102. a = end;
  2103. s = p / 4f;
  2104. }else{
  2105. s = p / (2f * Mathf.PI) * Mathf.Asin(end / a);
  2106. }
  2107. if(overshoot>1f){
  2108. if( val<0.2f ){
  2109. overshoot = 1f + (val / 0.2f * (overshoot-1f));
  2110. }else if( val > 0.8f ){
  2111. overshoot = 1f + ((1f-val) / 0.2f * (overshoot-1f));
  2112. }
  2113. }
  2114. if (val < 1f){
  2115. val = val-1f;
  2116. return start - 0.5f * (a * Mathf.Pow(2f, 10f * val) * Mathf.Sin((val - s) * (2f * Mathf.PI) / p)) * overshoot;
  2117. }
  2118. val = val-1f;
  2119. return end + start + a * Mathf.Pow(2f, -10f * val) * Mathf.Sin((val - s) * (2f * Mathf.PI) / p) * 0.5f * overshoot;
  2120. }
  2121. // LeanTween Listening/Dispatch
  2122. private static System.Action<LTEvent>[] eventListeners;
  2123. private static GameObject[] goListeners;
  2124. private static int eventsMaxSearch = 0;
  2125. public static int EVENTS_MAX = 10;
  2126. public static int LISTENERS_MAX = 10;
  2127. private static int INIT_LISTENERS_MAX = LISTENERS_MAX;
  2128. public static void addListener( int eventId, System.Action<LTEvent> callback ){
  2129. addListener(tweenEmpty, eventId, callback);
  2130. }
  2131. /**
  2132. * Add a listener method to be called when the appropriate LeanTween.dispatchEvent is called
  2133. *
  2134. * @method LeanTween.addListener
  2135. * @param {GameObject} caller:GameObject the gameObject the listener is attached to
  2136. * @param {int} eventId:int a unique int that describes the event (best to use an enum)
  2137. * @param {System.Action<LTEvent>} callback:System.Action<LTEvent> the method to call when the event has been dispatched
  2138. * @example
  2139. * LeanTween.addListener(gameObject, (int)MyEvents.JUMP, jumpUp);<br>
  2140. * <br>
  2141. * void jumpUp( LTEvent e ){ Debug.Log("jump!"); }<br>
  2142. */
  2143. public static void addListener( GameObject caller, int eventId, System.Action<LTEvent> callback ){
  2144. if(eventListeners==null){
  2145. INIT_LISTENERS_MAX = LISTENERS_MAX;
  2146. eventListeners = new System.Action<LTEvent>[ EVENTS_MAX * LISTENERS_MAX ];
  2147. goListeners = new GameObject[ EVENTS_MAX * LISTENERS_MAX ];
  2148. }
  2149. // Debug.Log("searching for an empty space for:"+caller + " eventid:"+event);
  2150. for(i = 0; i < INIT_LISTENERS_MAX; i++){
  2151. int point = eventId*INIT_LISTENERS_MAX + i;
  2152. if(goListeners[ point ]==null || eventListeners[ point ]==null){
  2153. eventListeners[ point ] = callback;
  2154. goListeners[ point ] = caller;
  2155. if(i>=eventsMaxSearch)
  2156. eventsMaxSearch = i+1;
  2157. // Debug.Log("adding event for:"+caller.name);
  2158. return;
  2159. }
  2160. #if UNITY_FLASH
  2161. if(goListeners[ point ] == caller && System.Object.ReferenceEquals( eventListeners[ point ], callback)){
  2162. // Debug.Log("This event is already being listened for.");
  2163. return;
  2164. }
  2165. #else
  2166. if(goListeners[ point ] == caller && System.Object.Equals( eventListeners[ point ], callback)){
  2167. // Debug.Log("This event is already being listened for.");
  2168. return;
  2169. }
  2170. #endif
  2171. }
  2172. Debug.LogError("You ran out of areas to add listeners, consider increasing LISTENERS_MAX, ex: LeanTween.LISTENERS_MAX = "+(LISTENERS_MAX*2));
  2173. }
  2174. public static bool removeListener( int eventId, System.Action<LTEvent> callback ){
  2175. return removeListener( tweenEmpty, eventId, callback);
  2176. }
  2177. public static bool removeListener( int eventId ){
  2178. int point = eventId*INIT_LISTENERS_MAX + i;
  2179. eventListeners[ point ] = null;
  2180. goListeners[ point ] = null;
  2181. return true;
  2182. }
  2183. /**
  2184. * Remove an event listener you have added
  2185. * @method LeanTween.removeListener
  2186. * @param {GameObject} caller:GameObject the gameObject the listener is attached to
  2187. * @param {int} eventId:int a unique int that describes the event (best to use an enum)
  2188. * @param {System.Action<LTEvent>} callback:System.Action<LTEvent> the method that was specified to call when the event has been dispatched
  2189. * @example
  2190. * LeanTween.removeListener(gameObject, (int)MyEvents.JUMP, jumpUp);<br>
  2191. * <br>
  2192. * void jumpUp( LTEvent e ){ }<br>
  2193. */
  2194. public static bool removeListener( GameObject caller, int eventId, System.Action<LTEvent> callback ){
  2195. for(i = 0; i < eventsMaxSearch; i++){
  2196. int point = eventId*INIT_LISTENERS_MAX + i;
  2197. #if UNITY_FLASH
  2198. if(goListeners[ point ] == caller && System.Object.ReferenceEquals( eventListeners[ point ], callback) ){
  2199. #else
  2200. if(goListeners[ point ] == caller && System.Object.Equals( eventListeners[ point ], callback) ){
  2201. #endif
  2202. eventListeners[ point ] = null;
  2203. goListeners[ point ] = null;
  2204. return true;
  2205. }
  2206. }
  2207. return false;
  2208. }
  2209. /**
  2210. * Tell the added listeners that you are dispatching the event
  2211. * @method LeanTween.dispatchEvent
  2212. * @param {int} eventId:int a unique int that describes the event (best to use an enum)
  2213. * @example
  2214. * LeanTween.dispatchEvent( (int)MyEvents.JUMP );<br>
  2215. */
  2216. public static void dispatchEvent( int eventId ){
  2217. dispatchEvent( eventId, null);
  2218. }
  2219. /**
  2220. * Tell the added listeners that you are dispatching the event
  2221. * @method LeanTween.dispatchEvent
  2222. * @param {int} eventId:int a unique int that describes the event (best to use an enum)
  2223. * @param {object} data:object Pass data to the listener, access it from the listener with *.data on the LTEvent object
  2224. * @example
  2225. * LeanTween.dispatchEvent( (int)MyEvents.JUMP, transform );<br>
  2226. * <br>
  2227. * void jumpUp( LTEvent e ){<br>
  2228. * &nbsp; Transform tran = (Transform)e.data;<br>
  2229. * }<br>
  2230. */
  2231. public static void dispatchEvent( int eventId, object data ){
  2232. for(int k = 0; k < eventsMaxSearch; k++){
  2233. int point = eventId*INIT_LISTENERS_MAX + k;
  2234. if(eventListeners[ point ]!=null){
  2235. if(goListeners[point]){
  2236. eventListeners[ point ]( new LTEvent(eventId, data) );
  2237. }else{
  2238. eventListeners[ point ] = null;
  2239. }
  2240. }
  2241. }
  2242. }
  2243. } // End LeanTween class
  2244. public class LTUtility {
  2245. public static Vector3[] reverse( Vector3[] arr ){
  2246. int length = arr.Length;
  2247. int left = 0;
  2248. int right = length - 1;
  2249. for (; left < right; left += 1, right -= 1){
  2250. Vector3 temporary = arr[left];
  2251. arr[left] = arr[right];
  2252. arr[right] = temporary;
  2253. }
  2254. return arr;
  2255. }
  2256. }
  2257. public class LTBezier {
  2258. public float length;
  2259. private Vector3 a;
  2260. private Vector3 aa;
  2261. private Vector3 bb;
  2262. private Vector3 cc;
  2263. private float len;
  2264. private float[] arcLengths;
  2265. public LTBezier(Vector3 a, Vector3 b, Vector3 c, Vector3 d, float precision){
  2266. this.a = a;
  2267. aa = (-a + 3*(b-c) + d);
  2268. bb = 3*(a+c) - 6*b;
  2269. cc = 3*(b-a);
  2270. this.len = 1.0f / precision;
  2271. arcLengths = new float[(int)this.len + (int)1];
  2272. arcLengths[0] = 0;
  2273. Vector3 ov = a;
  2274. Vector3 v;
  2275. float clen = 0.0f;
  2276. for(int i = 1; i <= this.len; i++) {
  2277. v = bezierPoint(i * precision);
  2278. clen += (ov - v).magnitude;
  2279. this.arcLengths[i] = clen;
  2280. ov = v;
  2281. }
  2282. this.length = clen;
  2283. }
  2284. private float map(float u) {
  2285. float targetLength = u * this.arcLengths[(int)this.len];
  2286. int low = 0;
  2287. int high = (int)this.len;
  2288. int index = 0;
  2289. while (low < high) {
  2290. index = low + ((int)((high - low) / 2.0f) | 0);
  2291. if (this.arcLengths[index] < targetLength) {
  2292. low = index + 1;
  2293. } else {
  2294. high = index;
  2295. }
  2296. }
  2297. if(this.arcLengths[index] > targetLength)
  2298. index--;
  2299. if(index<0)
  2300. index = 0;
  2301. return (index + (targetLength - arcLengths[index]) / (arcLengths[index + 1] - arcLengths[index])) / this.len;
  2302. }
  2303. private Vector3 bezierPoint(float t){
  2304. return ((aa* t + (bb))* t + cc)* t + a;
  2305. }
  2306. public Vector3 point(float t){
  2307. return bezierPoint( map(t) );
  2308. }
  2309. }
  2310. /**
  2311. * Manually animate along a bezier path with this class
  2312. * @class LTBezierPath
  2313. * @constructor
  2314. * @param {Vector3 Array} pts A set of points that define one or many bezier paths (the paths should be passed in multiples of 4, which correspond to each individual bezier curve)<br>
  2315. * It goes in the order: <strong>startPoint</strong>,endControl,startControl,<strong>endPoint</strong> - <strong>Note:</strong> the control for the end and start are reversed! This is just a *quirk* of the API.<br>
  2316. * <img src="http://dentedpixel.com/assets/LTBezierExplanation.gif" width="413" height="196" style="margin-top:10px" />
  2317. * @example
  2318. * LTBezierPath ltPath = new LTBezierPath( new Vector3[] { new Vector3(0f,0f,0f),new Vector3(1f,0f,0f), new Vector3(1f,0f,0f), new Vector3(1f,1f,0f)} );<br><br>
  2319. * LeanTween.move(lt, ltPath.vec3, 4.0f).setOrientToPath(true).setDelay(1f).setEase(LeanTweenType.easeInOutQuad); // animate <br>
  2320. * Vector3 pt = ltPath.point( 0.6f ); // retrieve a point along the path
  2321. */
  2322. public class LTBezierPath {
  2323. public Vector3[] pts;
  2324. public float length;
  2325. public bool orientToPath;
  2326. public bool orientToPath2d;
  2327. private LTBezier[] beziers;
  2328. private float[] lengthRatio;
  2329. private int currentBezier=0,previousBezier=0;
  2330. public LTBezierPath(){ }
  2331. public LTBezierPath( Vector3[] pts_ ){
  2332. setPoints( pts_ );
  2333. }
  2334. public void setPoints( Vector3[] pts_ ){
  2335. if(pts_.Length<4)
  2336. LeanTween.logError( "LeanTween - When passing values for a vector path, you must pass four or more values!" );
  2337. if(pts_.Length%4!=0)
  2338. LeanTween.logError( "LeanTween - When passing values for a vector path, they must be in sets of four: controlPoint1, controlPoint2, endPoint2, controlPoint2, controlPoint2..." );
  2339. pts = pts_;
  2340. int k = 0;
  2341. beziers = new LTBezier[ pts.Length / 4 ];
  2342. lengthRatio = new float[ beziers.Length ];
  2343. int i;
  2344. length = 0;
  2345. for(i = 0; i < pts.Length; i+=4){
  2346. beziers[k] = new LTBezier(pts[i+0],pts[i+2],pts[i+1],pts[i+3],0.05f);
  2347. length += beziers[k].length;
  2348. k++;
  2349. }
  2350. // Debug.Log("beziers.Length:"+beziers.Length + " beziers:"+beziers);
  2351. for(i = 0; i < beziers.Length; i++){
  2352. lengthRatio[i] = beziers[i].length / length;
  2353. }
  2354. }
  2355. /**
  2356. * @property {float} distance distance of the path (in unity units)
  2357. */
  2358. public float distance{
  2359. get{
  2360. return length;
  2361. }
  2362. }
  2363. /**
  2364. * Retrieve a point along a path
  2365. *
  2366. * @method point
  2367. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2368. * @return {Vector3} Vector3 position of the point along the path
  2369. * @example
  2370. * transform.position = ltPath.point( 0.6f );
  2371. */
  2372. public Vector3 point( float ratio ){
  2373. float added = 0.0f;
  2374. for(int i = 0; i < lengthRatio.Length; i++){
  2375. added += lengthRatio[i];
  2376. if(added >= ratio)
  2377. return beziers[i].point( (ratio-(added-lengthRatio[i])) / lengthRatio[i] );
  2378. }
  2379. return beziers[lengthRatio.Length-1].point( 1.0f );
  2380. }
  2381. public void place2d( Transform transform, float ratio ){
  2382. transform.position = point( ratio );
  2383. ratio += 0.001f;
  2384. if(ratio<=1.0f){
  2385. Vector3 v3Dir = point( ratio ) - transform.position;
  2386. float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
  2387. transform.eulerAngles = new Vector3(0, 0, angle);
  2388. }
  2389. }
  2390. public void placeLocal2d( Transform transform, float ratio ){
  2391. transform.localPosition = point( ratio );
  2392. ratio += 0.001f;
  2393. if(ratio<=1.0f){
  2394. Vector3 v3Dir = point( ratio ) - transform.localPosition;
  2395. float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
  2396. transform.localEulerAngles = new Vector3(0, 0, angle);
  2397. }
  2398. }
  2399. /**
  2400. * Place an object along a certain point on the path (facing the direction perpendicular to the path)
  2401. *
  2402. * @method place
  2403. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2404. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2405. * @example
  2406. * ltPath.place( transform, 0.6f );
  2407. */
  2408. public void place( Transform transform, float ratio ){
  2409. place( transform, ratio, Vector3.up );
  2410. }
  2411. /**
  2412. * Place an object along a certain point on the path, with it facing a certain direction perpendicular to the path
  2413. *
  2414. * @method place
  2415. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2416. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2417. * @param {Vector3} rotation:Vector3 the direction in which to place the transform ex: Vector3.up
  2418. * @example
  2419. * ltPath.place( transform, 0.6f, Vector3.left );
  2420. */
  2421. public void place( Transform transform, float ratio, Vector3 worldUp ){
  2422. transform.position = point( ratio );
  2423. ratio += 0.001f;
  2424. if(ratio<=1.0f)
  2425. transform.LookAt( point( ratio ), worldUp );
  2426. }
  2427. /**
  2428. * Place an object along a certain point on the path (facing the direction perpendicular to the path) - Local Space, not world-space
  2429. *
  2430. * @method placeLocal
  2431. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2432. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2433. * @example
  2434. * ltPath.placeLocal( transform, 0.6f );
  2435. */
  2436. public void placeLocal( Transform transform, float ratio ){
  2437. placeLocal( transform, ratio, Vector3.up );
  2438. }
  2439. /**
  2440. * Place an object along a certain point on the path, with it facing a certain direction perpendicular to the path - Local Space, not world-space
  2441. *
  2442. * @method placeLocal
  2443. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2444. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2445. * @param {Vector3} rotation:Vector3 the direction in which to place the transform ex: Vector3.up
  2446. * @example
  2447. * ltPath.placeLocal( transform, 0.6f, Vector3.left );
  2448. */
  2449. public void placeLocal( Transform transform, float ratio, Vector3 worldUp ){
  2450. ratio = getRationInOneRange (ratio);
  2451. transform.localPosition = point( ratio );
  2452. ratio = getRationInOneRange (ratio + 0.001f);
  2453. if(ratio<=1.0f)
  2454. transform.LookAt( transform.parent.TransformPoint( point( ratio ) ), worldUp );
  2455. }
  2456. public float getRationInOneRange(float ratio){
  2457. if (ratio >= 0.0f && ratio <= 1.0f) {
  2458. return ratio;
  2459. } else if (ratio < 0.0f) {
  2460. return Mathf.Ceil(ratio) - ratio; //if -1.4 => it returns 0.4
  2461. } else {
  2462. return ratio - Mathf.Floor(ratio); //if 1.4 => it return 0.4
  2463. }
  2464. }
  2465. public void gizmoDraw(float t = -1.0f)
  2466. {
  2467. Vector3 prevPt = point(0);
  2468. for (int i = 1; i <= 120; i++)
  2469. {
  2470. float pm = (float)i / 120f;
  2471. Vector3 currPt2 = point(pm);
  2472. //Gizmos.color = new Color(UnityEngine.Random.Range(0f,1f),UnityEngine.Random.Range(0f,1f),UnityEngine.Random.Range(0f,1f),1);
  2473. Gizmos.color = (previousBezier == currentBezier) ? Color.magenta : Color.grey;
  2474. Gizmos.DrawLine(currPt2, prevPt);
  2475. prevPt = currPt2;
  2476. previousBezier = currentBezier;
  2477. }
  2478. }
  2479. }
  2480. /**
  2481. * Animate along a set of points that need to be in the format: controlPoint, point1, point2.... pointLast, endControlPoint
  2482. * @class LTSpline
  2483. * @constructor
  2484. * @param {Vector3 Array} pts A set of points that define the points the path will pass through (starting with starting control point, and ending with a control point)<br>
  2485. <i><strong>Note:</strong> The first and last item just define the angle of the end points, they are not actually used in the spline path itself. If you do not care about the angle you can jus set the first two items and last two items as the same value.</i>
  2486. * @example
  2487. * LTSpline ltSpline = new LTSpline( new Vector3[] { new Vector3(0f,0f,0f),new Vector3(0f,0f,0f), new Vector3(0f,0.5f,0f), new Vector3(1f,1f,0f), new Vector3(1f,1f,0f)} );<br><br>
  2488. * LeanTween.moveSpline(lt, ltSpline.vec3, 4.0f).setOrientToPath(true).setDelay(1f).setEase(LeanTweenType.easeInOutQuad); // animate <br>
  2489. * Vector3 pt = ltSpline.point( 0.6f ); // retrieve a point along the path
  2490. */
  2491. [System.Serializable]
  2492. public class LTSpline {
  2493. public static int DISTANCE_COUNT = 3; // increase for a more accurate constant speed
  2494. public static int SUBLINE_COUNT = 20; // increase for a more accurate smoothing of the curves into lines
  2495. /**
  2496. * @property {float} distance distance of the spline (in unity units)
  2497. */
  2498. public float distance = 0f;
  2499. public bool constantSpeed = true;
  2500. public Vector3[] pts;
  2501. [System.NonSerialized]
  2502. public Vector3[] ptsAdj;
  2503. public int ptsAdjLength;
  2504. public bool orientToPath;
  2505. public bool orientToPath2d;
  2506. private int numSections;
  2507. private int currPt;
  2508. public LTSpline( Vector3[] pts ){
  2509. init( pts, true);
  2510. }
  2511. public LTSpline( Vector3[] pts, bool constantSpeed ) {
  2512. this.constantSpeed = constantSpeed;
  2513. init(pts, constantSpeed);
  2514. }
  2515. private void init( Vector3[] pts, bool constantSpeed){
  2516. if(pts.Length<4){
  2517. LeanTween.logError( "LeanTween - When passing values for a spline path, you must pass four or more values!" );
  2518. return;
  2519. }
  2520. this.pts = new Vector3[pts.Length];
  2521. System.Array.Copy(pts, this.pts, pts.Length);
  2522. numSections = pts.Length - 3;
  2523. float minSegment = float.PositiveInfinity;
  2524. Vector3 earlierPoint = this.pts[1];
  2525. float totalDistance = 0f;
  2526. for(int i=1; i < this.pts.Length-1; i++){
  2527. // float pointDistance = (this.pts[i]-earlierPoint).sqrMagnitude;
  2528. float pointDistance = Vector3.Distance(this.pts[i], earlierPoint);
  2529. //Debug.Log("pointDist:"+pointDistance);
  2530. if(pointDistance < minSegment){
  2531. minSegment = pointDistance;
  2532. }
  2533. totalDistance += pointDistance;
  2534. }
  2535. if(constantSpeed){
  2536. minSegment = totalDistance / (numSections*SUBLINE_COUNT);
  2537. //Debug.Log("minSegment:"+minSegment+" numSections:"+numSections);
  2538. float minPrecision = minSegment / SUBLINE_COUNT; // number of subdivisions in each segment
  2539. int precision = (int)Mathf.Ceil(totalDistance / minPrecision) * DISTANCE_COUNT;
  2540. // Debug.Log("precision:"+precision);
  2541. if(precision<=1) // precision has to be greater than one
  2542. precision = 2;
  2543. ptsAdj = new Vector3[ precision ];
  2544. earlierPoint = interp( 0f );
  2545. int num = 1;
  2546. ptsAdj[0] = earlierPoint;
  2547. distance = 0f;
  2548. for(int i = 0; i < precision + 1; i++){
  2549. float fract = ((float)(i)) / precision;
  2550. // Debug.Log("fract:"+fract);
  2551. Vector3 point = interp( fract );
  2552. float dist = Vector3.Distance(point, earlierPoint);
  2553. // float dist = (point-earlierPoint).sqrMagnitude;
  2554. if(dist>=minPrecision || fract>=1.0f){
  2555. ptsAdj[num] = point;
  2556. distance += dist; // only add it to the total distance once we know we are adding it as an adjusted point
  2557. earlierPoint = point;
  2558. // Debug.Log("fract:"+fract+" point:"+point);
  2559. num++;
  2560. }
  2561. }
  2562. // make sure there is a point at the very end
  2563. /*num++;
  2564. Vector3 endPoint = interp( 1f );
  2565. ptsAdj[num] = endPoint;*/
  2566. // Debug.Log("fract 1f endPoint:"+endPoint);
  2567. ptsAdjLength = num;
  2568. }
  2569. // Debug.Log("map 1f:"+map(1f)+" end:"+ptsAdj[ ptsAdjLength-1 ]);
  2570. // Debug.Log("ptsAdjLength:"+ptsAdjLength+" minPrecision:"+minPrecision+" precision:"+precision);
  2571. }
  2572. public Vector3 map( float u ){
  2573. if(u>=1f)
  2574. return pts[ pts.Length - 2];
  2575. float t = u * (ptsAdjLength-1);
  2576. int first = (int)Mathf.Floor( t );
  2577. int next = (int)Mathf.Ceil( t );
  2578. if(first<0)
  2579. first = 0;
  2580. Vector3 val = ptsAdj[ first ];
  2581. Vector3 nextVal = ptsAdj[ next ];
  2582. float diff = t - first;
  2583. // Debug.Log("u:"+u+" val:"+val +" nextVal:"+nextVal+" diff:"+diff+" first:"+first+" next:"+next);
  2584. val = val + (nextVal - val) * diff;
  2585. return val;
  2586. }
  2587. public Vector3 interp(float t) {
  2588. currPt = Mathf.Min(Mathf.FloorToInt(t * (float) numSections), numSections - 1);
  2589. float u = t * (float) numSections - (float) currPt;
  2590. //Debug.Log("currPt:"+currPt+" numSections:"+numSections+" pts.Length :"+pts.Length );
  2591. Vector3 a = pts[currPt];
  2592. Vector3 b = pts[currPt + 1];
  2593. Vector3 c = pts[currPt + 2];
  2594. Vector3 d = pts[currPt + 3];
  2595. Vector3 val = (.5f * (
  2596. (-a + 3f * b - 3f * c + d) * (u * u * u)
  2597. + (2f * a - 5f * b + 4f * c - d) * (u * u)
  2598. + (-a + c) * u
  2599. + 2f * b));
  2600. // Debug.Log("currPt:"+currPt+" t:"+t+" val.x"+val.x+" y:"+val.y+" z:"+val.z);
  2601. return val;
  2602. }
  2603. /**
  2604. * Retrieve a point along a path
  2605. *
  2606. * @method ratioAtPoint
  2607. * @param {Vector3} point:Vector3 given a current location it makes the best approximiation of where it is along the path ratio-wise (0-1)
  2608. * @return {float} float of ratio along the path
  2609. * @example
  2610. * ratioIter = ltSpline.ratioAtPoint( transform.position );
  2611. */
  2612. public float ratioAtPoint( Vector3 pt ){
  2613. float closestDist = float.MaxValue;
  2614. int closestI = 0;
  2615. for (int i = 0; i < ptsAdjLength; i++) {
  2616. float dist = Vector3.Distance(pt, ptsAdj[i]);
  2617. // Debug.Log("i:"+i+" dist:"+dist);
  2618. if(dist<closestDist){
  2619. closestDist = dist;
  2620. closestI = i;
  2621. }
  2622. }
  2623. // Debug.Log("closestI:"+closestI+" ptsAdjLength:"+ptsAdjLength);
  2624. return (float) closestI / (float)(ptsAdjLength-1);
  2625. }
  2626. /**
  2627. * Retrieve a point along a path
  2628. *
  2629. * @method point
  2630. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2631. * @return {Vector3} Vector3 position of the point along the path
  2632. * @example
  2633. * transform.position = ltSpline.point( 0.6f );
  2634. */
  2635. public Vector3 point( float ratio ){
  2636. float t = ratio>1f?1f:ratio;
  2637. return constantSpeed ? map(t) : interp(t);
  2638. }
  2639. public void place2d( Transform transform, float ratio ){
  2640. transform.position = point( ratio );
  2641. ratio += 0.001f;
  2642. if(ratio<=1.0f){
  2643. Vector3 v3Dir = point( ratio ) - transform.position;
  2644. float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
  2645. transform.eulerAngles = new Vector3(0, 0, angle);
  2646. }
  2647. }
  2648. public void placeLocal2d( Transform transform, float ratio ){
  2649. Transform trans = transform.parent;
  2650. if(trans==null){ // this has no parent, just do a regular transform
  2651. place2d(transform, ratio);
  2652. return;
  2653. }
  2654. transform.localPosition = point( ratio );
  2655. ratio += 0.001f;
  2656. if(ratio<=1.0f){
  2657. Vector3 ptAhead = point( ratio );//trans.TransformPoint( );
  2658. Vector3 v3Dir = ptAhead - transform.localPosition;
  2659. float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
  2660. transform.localEulerAngles = new Vector3(0, 0, angle);
  2661. }
  2662. }
  2663. /**
  2664. * Place an object along a certain point on the path (facing the direction perpendicular to the path)
  2665. *
  2666. * @method place
  2667. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2668. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2669. * @example
  2670. * ltPath.place( transform, 0.6f );
  2671. */
  2672. public void place( Transform transform, float ratio ){
  2673. place(transform, ratio, Vector3.up);
  2674. }
  2675. /**
  2676. * Place an object along a certain point on the path, with it facing a certain direction perpendicular to the path
  2677. *
  2678. * @method place
  2679. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2680. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2681. * @param {Vector3} rotation:Vector3 the direction in which to place the transform ex: Vector3.up
  2682. * @example
  2683. * ltPath.place( transform, 0.6f, Vector3.left );
  2684. */
  2685. public void place( Transform transform, float ratio, Vector3 worldUp ){
  2686. // ratio = Mathf.Repeat(ratio, 1.0f); // make sure ratio is always between 0-1
  2687. transform.position = point( ratio );
  2688. ratio += 0.001f;
  2689. if(ratio<=1.0f)
  2690. transform.LookAt( point( ratio ), worldUp );
  2691. }
  2692. /**
  2693. * Place an object along a certain point on the path (facing the direction perpendicular to the path) - Local Space, not world-space
  2694. *
  2695. * @method placeLocal
  2696. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2697. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2698. * @example
  2699. * ltPath.placeLocal( transform, 0.6f );
  2700. */
  2701. public void placeLocal( Transform transform, float ratio ){
  2702. placeLocal( transform, ratio, Vector3.up );
  2703. }
  2704. /**
  2705. * Place an object along a certain point on the path, with it facing a certain direction perpendicular to the path - Local Space, not world-space
  2706. *
  2707. * @method placeLocal
  2708. * @param {Transform} transform:Transform the transform of the object you wish to place along the path
  2709. * @param {float} ratio:float ratio of the point along the path you wish to receive (0-1)
  2710. * @param {Vector3} rotation:Vector3 the direction in which to place the transform ex: Vector3.up
  2711. * @example
  2712. * ltPath.placeLocal( transform, 0.6f, Vector3.left );
  2713. */
  2714. public void placeLocal( Transform transform, float ratio, Vector3 worldUp ){
  2715. transform.localPosition = point( ratio );
  2716. ratio += 0.001f;
  2717. if(ratio<=1.0f)
  2718. transform.LookAt( transform.parent.TransformPoint( point( ratio ) ), worldUp );
  2719. }
  2720. public void gizmoDraw(float t = -1.0f) {
  2721. if(ptsAdj==null || ptsAdj.Length<=0)
  2722. return;
  2723. Vector3 prevPt = ptsAdj[0];
  2724. for (int i = 0; i < ptsAdjLength; i++) {
  2725. Vector3 currPt2 = ptsAdj[i];
  2726. // Debug.Log("currPt2:"+currPt2);
  2727. //Gizmos.color = new Color(UnityEngine.Random.Range(0f,1f),UnityEngine.Random.Range(0f,1f),UnityEngine.Random.Range(0f,1f),1);
  2728. Gizmos.DrawLine(prevPt, currPt2);
  2729. prevPt = currPt2;
  2730. }
  2731. }
  2732. public void drawGizmo( Color color ) {
  2733. if( this.ptsAdjLength>=4){
  2734. Vector3 prevPt = this.ptsAdj[0];
  2735. Color colorBefore = Gizmos.color;
  2736. Gizmos.color = color;
  2737. for (int i = 0; i < this.ptsAdjLength; i++) {
  2738. Vector3 currPt2 = this.ptsAdj[i];
  2739. // Debug.Log("currPt2:"+currPt2);
  2740. Gizmos.DrawLine(prevPt, currPt2);
  2741. prevPt = currPt2;
  2742. }
  2743. Gizmos.color = colorBefore;
  2744. }
  2745. }
  2746. public static void drawGizmo(Transform[] arr, Color color) {
  2747. if(arr.Length>=4){
  2748. Vector3[] vec3s = new Vector3[arr.Length];
  2749. for(int i = 0; i < arr.Length; i++){
  2750. vec3s[i] = arr[i].position;
  2751. }
  2752. LTSpline spline = new LTSpline(vec3s);
  2753. Vector3 prevPt = spline.ptsAdj[0];
  2754. Color colorBefore = Gizmos.color;
  2755. Gizmos.color = color;
  2756. for (int i = 0; i < spline.ptsAdjLength; i++) {
  2757. Vector3 currPt2 = spline.ptsAdj[i];
  2758. // Debug.Log("currPt2:"+currPt2);
  2759. Gizmos.DrawLine(prevPt, currPt2);
  2760. prevPt = currPt2;
  2761. }
  2762. Gizmos.color = colorBefore;
  2763. }
  2764. }
  2765. public static void drawLine(Transform[] arr, float width, Color color) {
  2766. if(arr.Length>=4){
  2767. }
  2768. }
  2769. /*public Vector3 Velocity(float t) {
  2770. t = map( t );
  2771. int numSections = pts.Length - 3;
  2772. int currPt = Mathf.Min(Mathf.FloorToInt(t * (float) numSections), numSections - 1);
  2773. float u = t * (float) numSections - (float) currPt;
  2774. Vector3 a = pts[currPt];
  2775. Vector3 b = pts[currPt + 1];
  2776. Vector3 c = pts[currPt + 2];
  2777. Vector3 d = pts[currPt + 3];
  2778. return 1.5f * (-a + 3f * b - 3f * c + d) * (u * u)
  2779. + (2f * a -5f * b + 4f * c - d) * u
  2780. + .5f * c - .5f * a;
  2781. }*/
  2782. public void drawLinesGLLines(Material outlineMaterial, Color color, float width){
  2783. GL.PushMatrix();
  2784. outlineMaterial.SetPass(0);
  2785. GL.LoadPixelMatrix();
  2786. GL.Begin(GL.LINES);
  2787. GL.Color(color);
  2788. if (constantSpeed) {
  2789. if (this.ptsAdjLength >= 4) {
  2790. Vector3 prevPt = this.ptsAdj[0];
  2791. for (int i = 0; i < this.ptsAdjLength; i++) {
  2792. Vector3 currPt2 = this.ptsAdj[i];
  2793. GL.Vertex(prevPt);
  2794. GL.Vertex(currPt2);
  2795. prevPt = currPt2;
  2796. }
  2797. }
  2798. } else {
  2799. if (this.pts.Length >= 4) {
  2800. Vector3 prevPt = this.pts[0];
  2801. float split = 1f / ((float)this.pts.Length * 10f);
  2802. float iter = 0f;
  2803. while (iter < 1f) {
  2804. float at = iter / 1f;
  2805. Vector3 currPt2 = interp(at);
  2806. // Debug.Log("currPt2:"+currPt2);
  2807. GL.Vertex(prevPt);
  2808. GL.Vertex(currPt2);
  2809. prevPt = currPt2;
  2810. iter += split;
  2811. }
  2812. }
  2813. }
  2814. GL.End();
  2815. GL.PopMatrix();
  2816. }
  2817. public Vector3[] generateVectors(){
  2818. if (this.pts.Length >= 4) {
  2819. List<Vector3> meshPoints = new List<Vector3>();
  2820. Vector3 prevPt = this.pts[0];
  2821. meshPoints.Add(prevPt);
  2822. float split = 1f / ((float)this.pts.Length * 10f);
  2823. float iter = 0f;
  2824. while (iter < 1f) {
  2825. float at = iter / 1f;
  2826. Vector3 currPt2 = interp(at);
  2827. // Debug.Log("currPt2:"+currPt2);
  2828. // GL.Vertex(prevPt);
  2829. // GL.Vertex(currPt2);
  2830. meshPoints.Add(currPt2);
  2831. // prevPt = currPt2;
  2832. iter += split;
  2833. }
  2834. meshPoints.ToArray();
  2835. }
  2836. return null;
  2837. }
  2838. }
  2839. /**
  2840. * Animate GUI Elements by creating this object and passing the *.rect variable to the GUI method<br><br>
  2841. * <strong>Example Javascript: </strong><br>var bRect:LTRect = new LTRect( 0, 0, 100, 50 );<br>
  2842. * LeanTween.scale( bRect, Vector2(bRect.rect.width, bRect.rect.height) * 1.3, 0.25 );<br>
  2843. * function OnGUI(){<br>
  2844. * &nbsp; if(GUI.Button(bRect.rect, "Scale")){ }<br>
  2845. * }<br>
  2846. * <br>
  2847. * <strong>Example C#: </strong> <br>
  2848. * LTRect bRect = new LTRect( 0f, 0f, 100f, 50f );<br>
  2849. * LeanTween.scale( bRect, new Vector2(150f,75f), 0.25f );<br>
  2850. * void OnGUI(){<br>
  2851. * &nbsp; if(GUI.Button(bRect.rect, "Scale")){ }<br>
  2852. * }<br>
  2853. *
  2854. * @class LTRect
  2855. * @constructor
  2856. * @param {float} x:float X location
  2857. * @param {float} y:float Y location
  2858. * @param {float} width:float Width
  2859. * @param {float} height:float Height
  2860. * @param {float} alpha:float (Optional) initial alpha amount (0-1)
  2861. * @param {float} rotation:float (Optional) initial rotation in degrees (0-360)
  2862. */
  2863. [System.Serializable]
  2864. public class LTRect : System.Object{
  2865. /**
  2866. * Pass this value to the GUI Methods
  2867. *
  2868. * @property rect
  2869. * @type {Rect} rect:Rect Rect object that controls the positioning and size
  2870. */
  2871. public Rect _rect;
  2872. public float alpha = 1f;
  2873. public float rotation;
  2874. public Vector2 pivot;
  2875. public Vector2 margin;
  2876. public Rect relativeRect = new Rect(0f,0f,float.PositiveInfinity,float.PositiveInfinity);
  2877. public bool rotateEnabled;
  2878. [HideInInspector]
  2879. public bool rotateFinished;
  2880. public bool alphaEnabled;
  2881. public string labelStr;
  2882. public LTGUI.Element_Type type;
  2883. public GUIStyle style;
  2884. public bool useColor = false;
  2885. public Color color = Color.white;
  2886. public bool fontScaleToFit;
  2887. public bool useSimpleScale;
  2888. public bool sizeByHeight;
  2889. public Texture texture;
  2890. private int _id = -1;
  2891. [HideInInspector]
  2892. public int counter;
  2893. public static bool colorTouched;
  2894. public LTRect(){
  2895. reset();
  2896. this.rotateEnabled = this.alphaEnabled = true;
  2897. _rect = new Rect(0f,0f,1f,1f);
  2898. }
  2899. public LTRect(Rect rect){
  2900. _rect = rect;
  2901. reset();
  2902. }
  2903. public LTRect(float x, float y, float width, float height){
  2904. _rect = new Rect(x,y,width,height);
  2905. this.alpha = 1.0f;
  2906. this.rotation = 0.0f;
  2907. this.rotateEnabled = this.alphaEnabled = false;
  2908. }
  2909. public LTRect(float x, float y, float width, float height, float alpha){
  2910. _rect = new Rect(x,y,width,height);
  2911. this.alpha = alpha;
  2912. this.rotation = 0.0f;
  2913. this.rotateEnabled = this.alphaEnabled = false;
  2914. }
  2915. public LTRect(float x, float y, float width, float height, float alpha, float rotation){
  2916. _rect = new Rect(x,y,width,height);
  2917. this.alpha = alpha;
  2918. this.rotation = rotation;
  2919. this.rotateEnabled = this.alphaEnabled = false;
  2920. if(rotation!=0.0f){
  2921. this.rotateEnabled = true;
  2922. resetForRotation();
  2923. }
  2924. }
  2925. public bool hasInitiliazed{
  2926. get{
  2927. return _id!=-1;
  2928. }
  2929. }
  2930. public int id{
  2931. get{
  2932. int toId = _id | counter << 16;
  2933. /*uint backId = toId & 0xFFFF;
  2934. uint backCounter = toId >> 16;
  2935. if(_id!=backId || backCounter!=counter){
  2936. Debug.LogError("BAD CONVERSION toId:"+_id);
  2937. }*/
  2938. return toId;
  2939. }
  2940. }
  2941. public void setId( int id, int counter){
  2942. this._id = id;
  2943. this.counter = counter;
  2944. }
  2945. public void reset(){
  2946. this.alpha = 1.0f;
  2947. this.rotation = 0.0f;
  2948. this.rotateEnabled = this.alphaEnabled = false;
  2949. this.margin = Vector2.zero;
  2950. this.sizeByHeight = false;
  2951. this.useColor = false;
  2952. }
  2953. public void resetForRotation(){
  2954. Vector3 scale = new Vector3(GUI.matrix[0,0], GUI.matrix[1,1], GUI.matrix[2,2]);
  2955. if(pivot==Vector2.zero){
  2956. pivot = new Vector2((_rect.x+((_rect.width)*0.5f )) * scale.x + GUI.matrix[0,3], (_rect.y+((_rect.height)*0.5f )) * scale.y + GUI.matrix[1,3]);
  2957. }
  2958. }
  2959. public float x{
  2960. get{ return _rect.x; }
  2961. set{ _rect.x = value; }
  2962. }
  2963. public float y{
  2964. get{ return _rect.y; }
  2965. set{ _rect.y = value; }
  2966. }
  2967. public float width{
  2968. get{ return _rect.width; }
  2969. set{ _rect.width = value; }
  2970. }
  2971. public float height{
  2972. get{ return _rect.height; }
  2973. set{ _rect.height = value; }
  2974. }
  2975. public Rect rect{
  2976. get{
  2977. if(colorTouched){
  2978. colorTouched = false;
  2979. GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,1.0f);
  2980. }
  2981. if(rotateEnabled){
  2982. if(rotateFinished){
  2983. rotateFinished = false;
  2984. rotateEnabled = false;
  2985. //this.rotation = 0.0f;
  2986. pivot = Vector2.zero;
  2987. }else{
  2988. GUIUtility.RotateAroundPivot(rotation, pivot);
  2989. }
  2990. }
  2991. if(alphaEnabled){
  2992. GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);
  2993. colorTouched = true;
  2994. }
  2995. if(fontScaleToFit){
  2996. if(this.useSimpleScale){
  2997. style.fontSize = (int)(_rect.height*this.relativeRect.height);
  2998. }else{
  2999. style.fontSize = (int)_rect.height;
  3000. }
  3001. }
  3002. return _rect;
  3003. }
  3004. set{
  3005. _rect = value;
  3006. }
  3007. }
  3008. public LTRect setStyle( GUIStyle style ){
  3009. this.style = style;
  3010. return this;
  3011. }
  3012. public LTRect setFontScaleToFit( bool fontScaleToFit ){
  3013. this.fontScaleToFit = fontScaleToFit;
  3014. return this;
  3015. }
  3016. public LTRect setColor( Color color ){
  3017. this.color = color;
  3018. this.useColor = true;
  3019. return this;
  3020. }
  3021. public LTRect setAlpha( float alpha ){
  3022. this.alpha = alpha;
  3023. return this;
  3024. }
  3025. public LTRect setLabel( String str ){
  3026. this.labelStr = str;
  3027. return this;
  3028. }
  3029. public LTRect setUseSimpleScale( bool useSimpleScale, Rect relativeRect){
  3030. this.useSimpleScale = useSimpleScale;
  3031. this.relativeRect = relativeRect;
  3032. return this;
  3033. }
  3034. public LTRect setUseSimpleScale( bool useSimpleScale){
  3035. this.useSimpleScale = useSimpleScale;
  3036. this.relativeRect = new Rect(0f,0f,Screen.width,Screen.height);
  3037. return this;
  3038. }
  3039. public LTRect setSizeByHeight( bool sizeByHeight){
  3040. this.sizeByHeight = sizeByHeight;
  3041. return this;
  3042. }
  3043. public override string ToString(){
  3044. return "x:"+_rect.x+" y:"+_rect.y+" width:"+_rect.width+" height:"+_rect.height;
  3045. }
  3046. }
  3047. /**
  3048. * Object that describes the event to an event listener
  3049. * @class LTEvent
  3050. * @constructor
  3051. * @param {object} data:object Data that has been passed from the dispatchEvent method
  3052. */
  3053. public class LTEvent {
  3054. public int id;
  3055. public object data;
  3056. public LTEvent(int id, object data){
  3057. this.id = id;
  3058. this.data = data;
  3059. }
  3060. }
  3061. public class LTGUI {
  3062. public static int RECT_LEVELS = 5;
  3063. public static int RECTS_PER_LEVEL = 10;
  3064. public static int BUTTONS_MAX = 24;
  3065. private static LTRect[] levels;
  3066. private static int[] levelDepths;
  3067. private static Rect[] buttons;
  3068. private static int[] buttonLevels;
  3069. private static int[] buttonLastFrame;
  3070. private static LTRect r;
  3071. private static Color color = Color.white;
  3072. private static bool isGUIEnabled = false;
  3073. private static int global_counter = 0;
  3074. public enum Element_Type{
  3075. Texture,
  3076. Label
  3077. }
  3078. public static void init(){
  3079. if(levels==null){
  3080. levels = new LTRect[RECT_LEVELS*RECTS_PER_LEVEL];
  3081. levelDepths = new int[RECT_LEVELS];
  3082. }
  3083. }
  3084. public static void initRectCheck(){
  3085. if(buttons==null){
  3086. buttons = new Rect[BUTTONS_MAX];
  3087. buttonLevels = new int[BUTTONS_MAX];
  3088. buttonLastFrame = new int[BUTTONS_MAX];
  3089. for(int i = 0; i < buttonLevels.Length; i++){
  3090. buttonLevels[i] = -1;
  3091. }
  3092. }
  3093. }
  3094. public static void reset(){
  3095. if(isGUIEnabled){
  3096. isGUIEnabled = false;
  3097. for(int i = 0; i < levels.Length; i++){
  3098. levels[i] = null;
  3099. }
  3100. for(int i = 0; i < levelDepths.Length; i++){
  3101. levelDepths[i] = 0;
  3102. }
  3103. }
  3104. }
  3105. public static void update( int updateLevel ){
  3106. if(isGUIEnabled){
  3107. init();
  3108. if(levelDepths[updateLevel]>0){
  3109. color = GUI.color;
  3110. int baseI = updateLevel*RECTS_PER_LEVEL;
  3111. int maxLoop = baseI + levelDepths[updateLevel];// RECTS_PER_LEVEL;//;
  3112. for(int i = baseI; i < maxLoop; i++){
  3113. r = levels[i];
  3114. // Debug.Log("r:"+r+" i:"+i);
  3115. if(r!=null /*&& checkOnScreen(r.rect)*/){
  3116. //Debug.Log("label:"+r.labelStr+" textColor:"+r.style.normal.textColor);
  3117. if(r.useColor)
  3118. GUI.color = r.color;
  3119. if(r.type == Element_Type.Label){
  3120. if(r.style!=null)
  3121. GUI.skin.label = r.style;
  3122. if(r.useSimpleScale){
  3123. GUI.Label( new Rect((r.rect.x + r.margin.x + r.relativeRect.x)*r.relativeRect.width, (r.rect.y + r.margin.y + r.relativeRect.y)*r.relativeRect.height, r.rect.width*r.relativeRect.width, r.rect.height*r.relativeRect.height), r.labelStr );
  3124. }else{
  3125. GUI.Label( new Rect(r.rect.x + r.margin.x, r.rect.y + r.margin.y, r.rect.width, r.rect.height), r.labelStr );
  3126. }
  3127. }else if(r.type == Element_Type.Texture && r.texture!=null){
  3128. Vector2 size = r.useSimpleScale ? new Vector2(0f, r.rect.height*r.relativeRect.height) : new Vector2(r.rect.width, r.rect.height);
  3129. if(r.sizeByHeight){
  3130. size.x = (float)r.texture.width/(float)r.texture.height * size.y;
  3131. }
  3132. if(r.useSimpleScale){
  3133. GUI.DrawTexture( new Rect((r.rect.x + r.margin.x + r.relativeRect.x)*r.relativeRect.width, (r.rect.y + r.margin.y + r.relativeRect.y)*r.relativeRect.height, size.x, size.y), r.texture );
  3134. }else{
  3135. GUI.DrawTexture( new Rect(r.rect.x + r.margin.x, r.rect.y + r.margin.y, size.x, size.y), r.texture );
  3136. }
  3137. }
  3138. }
  3139. }
  3140. GUI.color = color;
  3141. }
  3142. }
  3143. }
  3144. public static bool checkOnScreen(Rect rect){
  3145. bool offLeft = rect.x + rect.width < 0f;
  3146. bool offRight = rect.x > Screen.width;
  3147. bool offBottom = rect.y > Screen.height;
  3148. bool offTop = rect.y + rect.height < 0f;
  3149. return !(offLeft || offRight || offBottom || offTop);
  3150. }
  3151. public static void destroy( int id ){
  3152. int backId = id & 0xFFFF;
  3153. int backCounter = id >> 16;
  3154. if(id>=0 && levels[backId]!=null && levels[backId].hasInitiliazed && levels[backId].counter==backCounter)
  3155. levels[backId] = null;
  3156. }
  3157. public static void destroyAll( int depth ){ // clears all gui elements on depth
  3158. int maxLoop = depth*RECTS_PER_LEVEL + RECTS_PER_LEVEL;
  3159. for(int i = depth*RECTS_PER_LEVEL; levels!=null && i < maxLoop; i++){
  3160. levels[i] = null;
  3161. }
  3162. }
  3163. public static LTRect label( Rect rect, string label, int depth){
  3164. return LTGUI.label(new LTRect(rect), label, depth);
  3165. }
  3166. public static LTRect label( LTRect rect, string label, int depth){
  3167. rect.type = Element_Type.Label;
  3168. rect.labelStr = label;
  3169. return element(rect, depth);
  3170. }
  3171. public static LTRect texture( Rect rect, Texture texture, int depth){
  3172. return LTGUI.texture( new LTRect(rect), texture, depth);
  3173. }
  3174. public static LTRect texture( LTRect rect, Texture texture, int depth){
  3175. rect.type = Element_Type.Texture;
  3176. rect.texture = texture;
  3177. return element(rect, depth);
  3178. }
  3179. public static LTRect element( LTRect rect, int depth){
  3180. isGUIEnabled = true;
  3181. init();
  3182. int maxLoop = depth*RECTS_PER_LEVEL + RECTS_PER_LEVEL;
  3183. int k = 0;
  3184. if(rect!=null){
  3185. destroy(rect.id);
  3186. }
  3187. if(rect.type==LTGUI.Element_Type.Label && rect.style!=null){
  3188. if(rect.style.normal.textColor.a<=0f){
  3189. Debug.LogWarning("Your GUI normal color has an alpha of zero, and will not be rendered.");
  3190. }
  3191. }
  3192. if(rect.relativeRect.width==float.PositiveInfinity){
  3193. rect.relativeRect = new Rect(0f,0f,Screen.width,Screen.height);
  3194. }
  3195. for(int i = depth*RECTS_PER_LEVEL; i < maxLoop; i++){
  3196. r = levels[i];
  3197. if(r==null){
  3198. r = rect;
  3199. r.rotateEnabled = true;
  3200. r.alphaEnabled = true;
  3201. r.setId( i, global_counter );
  3202. levels[i] = r;
  3203. // Debug.Log("k:"+k+ " maxDepth:"+levelDepths[depth]);
  3204. if(k>=levelDepths[depth]){
  3205. levelDepths[depth] = k + 1;
  3206. }
  3207. global_counter++;
  3208. return r;
  3209. }
  3210. k++;
  3211. }
  3212. Debug.LogError("You ran out of GUI Element spaces");
  3213. return null;
  3214. }
  3215. public static bool hasNoOverlap( Rect rect, int depth ){
  3216. initRectCheck();
  3217. bool hasNoOverlap = true;
  3218. bool wasAddedToList = false;
  3219. for(int i = 0; i < buttonLevels.Length; i++){
  3220. // Debug.Log("buttonLastFrame["+i+"]:"+buttonLastFrame[i]);
  3221. //Debug.Log("buttonLevels["+i+"]:"+buttonLevels[i]);
  3222. if(buttonLevels[i]>=0){
  3223. //Debug.Log("buttonLastFrame["+i+"]:"+buttonLastFrame[i]+" Time.frameCount:"+Time.frameCount);
  3224. if( buttonLastFrame[i] + 1 < Time.frameCount ){ // It has to have been visible within the current, or
  3225. buttonLevels[i] = -1;
  3226. // Debug.Log("resetting i:"+i);
  3227. }else{
  3228. //if(buttonLevels[i]>=0)
  3229. // Debug.Log("buttonLevels["+i+"]:"+buttonLevels[i]);
  3230. if(buttonLevels[i]>depth){
  3231. /*if(firstTouch().x > 0){
  3232. Debug.Log("buttons["+i+"]:"+buttons[i] + " firstTouch:");
  3233. Debug.Log(firstTouch());
  3234. Debug.Log(buttonLevels[i]);
  3235. }*/
  3236. if(pressedWithinRect( buttons[i] )){
  3237. hasNoOverlap = false; // there is an overlapping button that is higher
  3238. }
  3239. }
  3240. }
  3241. }
  3242. if(wasAddedToList==false && buttonLevels[i]<0){
  3243. wasAddedToList = true;
  3244. buttonLevels[i] = depth;
  3245. buttons[i] = rect;
  3246. buttonLastFrame[i] = Time.frameCount;
  3247. }
  3248. }
  3249. return hasNoOverlap;
  3250. }
  3251. public static bool pressedWithinRect( Rect rect ){
  3252. Vector2 vec2 = firstTouch();
  3253. if(vec2.x<0f)
  3254. return false;
  3255. float vecY = Screen.height-vec2.y;
  3256. return (vec2.x > rect.x && vec2.x < rect.x + rect.width && vecY > rect.y && vecY < rect.y + rect.height);
  3257. }
  3258. public static bool checkWithinRect(Vector2 vec2, Rect rect){
  3259. vec2.y = Screen.height-vec2.y;
  3260. return (vec2.x > rect.x && vec2.x < rect.x + rect.width && vec2.y > rect.y && vec2.y < rect.y + rect.height);
  3261. }
  3262. public static Vector2 firstTouch(){
  3263. if(Input.touchCount>0){
  3264. return Input.touches[0].position;
  3265. }else if(Input.GetMouseButton(0)){
  3266. return Input.mousePosition;
  3267. }
  3268. return new Vector2(Mathf.NegativeInfinity,Mathf.NegativeInfinity);
  3269. }
  3270. }
  3271. namespace DentedPixel { public class LeanDummy {} }
  3272. //}