LeanTween.cs 168 KB

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