LitMotionTextMeshProExtensions.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. #if LITMOTION_SUPPORT_TMP
  2. using System.Buffers;
  3. using UnityEngine;
  4. using Unity.Collections;
  5. using TMPro;
  6. #if LITMOTION_SUPPORT_ZSTRING
  7. using Cysharp.Text;
  8. #endif
  9. namespace LitMotion.Extensions
  10. {
  11. /// <summary>
  12. /// Provides binding extension methods for TMP_Text
  13. /// </summary>
  14. public static class LitMotionTextMeshProExtensions
  15. {
  16. /// <summary>
  17. /// Create a motion data and bind it to TMP_Text.fontSize
  18. /// </summary>
  19. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  20. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  21. /// <param name="builder">This builder</param>
  22. /// <param name="text">Target TMP_Text</param>
  23. /// <returns>Handle of the created motion data.</returns>
  24. public static MotionHandle BindToFontSize<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  25. where TOptions : unmanaged, IMotionOptions
  26. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  27. {
  28. Error.IsNull(text);
  29. return builder.BindWithState(text, static (x, target) =>
  30. {
  31. target.fontSize = x;
  32. });
  33. }
  34. /// <summary>
  35. /// Create a motion data and bind it to TMP_Text.maxVisibleCharacters
  36. /// </summary>
  37. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  38. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  39. /// <param name="builder">This builder</param>
  40. /// <param name="text">Target TMP_Text</param>
  41. /// <returns>Handle of the created motion data.</returns>
  42. public static MotionHandle BindToMaxVisibleCharacters<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
  43. where TOptions : unmanaged, IMotionOptions
  44. where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
  45. {
  46. Error.IsNull(text);
  47. return builder.BindWithState(text, static (x, target) =>
  48. {
  49. target.maxVisibleCharacters = x;
  50. });
  51. }
  52. /// <summary>
  53. /// Create a motion data and bind it to TMP_Text.maxVisibleLines
  54. /// </summary>
  55. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  56. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  57. /// <param name="builder">This builder</param>
  58. /// <param name="text">Target TMP_Text</param>
  59. /// <returns>Handle of the created motion data.</returns>
  60. public static MotionHandle BindToMaxVisibleLines<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
  61. where TOptions : unmanaged, IMotionOptions
  62. where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
  63. {
  64. Error.IsNull(text);
  65. return builder.BindWithState(text, static (x, target) =>
  66. {
  67. target.maxVisibleLines = x;
  68. });
  69. }
  70. /// <summary>
  71. /// Create a motion data and bind it to TMP_Text.maxVisibleWords
  72. /// </summary>
  73. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  74. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  75. /// <param name="builder">This builder</param>
  76. /// <param name="text">Target TMP_Text</param>
  77. /// <returns>Handle of the created motion data.</returns>
  78. public static MotionHandle BindToMaxVisibleWords<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
  79. where TOptions : unmanaged, IMotionOptions
  80. where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
  81. {
  82. Error.IsNull(text);
  83. return builder.BindWithState(text, static (x, target) =>
  84. {
  85. target.maxVisibleWords = x;
  86. });
  87. }
  88. /// <summary>
  89. /// Create a motion data and bind it to TMP_Text.color
  90. /// </summary>
  91. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  92. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  93. /// <param name="builder">This builder</param>
  94. /// <param name="text">Target TMP_Text</param>
  95. /// <returns>Handle of the created motion data.</returns>
  96. public static MotionHandle BindToColor<TOptions, TAdapter>(this MotionBuilder<Color, TOptions, TAdapter> builder, TMP_Text text)
  97. where TOptions : unmanaged, IMotionOptions
  98. where TAdapter : unmanaged, IMotionAdapter<Color, TOptions>
  99. {
  100. Error.IsNull(text);
  101. return builder.BindWithState(text, static (x, target) =>
  102. {
  103. target.color = x;
  104. });
  105. }
  106. /// <summary>
  107. /// Create a motion data and bind it to TMP_Text.color.r
  108. /// </summary>
  109. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  110. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  111. /// <param name="builder">This builder</param>
  112. /// <param name="text">Target TMP_Text</param>
  113. /// <returns>Handle of the created motion data.</returns>
  114. public static MotionHandle BindToColorR<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  115. where TOptions : unmanaged, IMotionOptions
  116. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  117. {
  118. Error.IsNull(text);
  119. return builder.BindWithState(text, static (x, target) =>
  120. {
  121. var c = target.color;
  122. c.r = x;
  123. target.color = c;
  124. });
  125. }
  126. /// <summary>
  127. /// Create a motion data and bind it to TMP_Text.color.g
  128. /// </summary>
  129. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  130. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  131. /// <param name="builder">This builder</param>
  132. /// <param name="text">Target TMP_Text</param>
  133. /// <returns>Handle of the created motion data.</returns>
  134. public static MotionHandle BindToColorG<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  135. where TOptions : unmanaged, IMotionOptions
  136. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  137. {
  138. Error.IsNull(text);
  139. return builder.BindWithState(text, static (x, target) =>
  140. {
  141. var c = target.color;
  142. c.g = x;
  143. target.color = c;
  144. });
  145. }
  146. /// <summary>
  147. /// Create a motion data and bind it to TMP_Text.color.b
  148. /// </summary>
  149. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  150. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  151. /// <param name="builder">This builder</param>
  152. /// <param name="text">Target TMP_Text</param>
  153. /// <returns>Handle of the created motion data.</returns>
  154. public static MotionHandle BindToColorB<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  155. where TOptions : unmanaged, IMotionOptions
  156. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  157. {
  158. Error.IsNull(text);
  159. return builder.BindWithState(text, static (x, target) =>
  160. {
  161. var c = target.color;
  162. c.b = x;
  163. target.color = c;
  164. });
  165. }
  166. /// <summary>
  167. /// Create a motion data and bind it to TMP_Text.color.a
  168. /// </summary>
  169. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  170. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  171. /// <param name="builder">This builder</param>
  172. /// <param name="text">Target TMP_Text</param>
  173. /// <returns>Handle of the created motion data.</returns>
  174. public static MotionHandle BindToColorA<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  175. where TOptions : unmanaged, IMotionOptions
  176. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  177. {
  178. Error.IsNull(text);
  179. return builder.BindWithState(text, static (x, target) =>
  180. {
  181. var c = target.color;
  182. c.a = x;
  183. target.color = c;
  184. });
  185. }
  186. /// <summary>
  187. /// Create a motion data and bind it to TMP_Text.text.
  188. /// </summary>
  189. /// <remarks>
  190. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  191. /// </remarks>
  192. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  193. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  194. /// <param name="builder">This builder</param>
  195. /// <param name="text">Target TMP_Text</param>
  196. /// <returns>Handle of the created motion data.</returns>
  197. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString32Bytes, TOptions, TAdapter> builder, TMP_Text text)
  198. where TOptions : unmanaged, IMotionOptions
  199. where TAdapter : unmanaged, IMotionAdapter<FixedString32Bytes, TOptions>
  200. {
  201. Error.IsNull(text);
  202. return builder.BindWithState(text, static (x, target) =>
  203. {
  204. var enumerator = x.GetEnumerator();
  205. var length = 0;
  206. var buffer = ArrayPool<char>.Shared.Rent(64);
  207. fixed (char* c = buffer)
  208. {
  209. Unicode.Utf8ToUtf16(x.GetUnsafePtr(), x.Length, c, out length, x.Length * 2);
  210. }
  211. target.SetText(buffer, 0, length);
  212. ArrayPool<char>.Shared.Return(buffer);
  213. });
  214. }
  215. /// <summary>
  216. /// Create a motion data and bind it to TMP_Text.text.
  217. /// </summary>
  218. /// <remarks>
  219. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  220. /// </remarks>
  221. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  222. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  223. /// <param name="builder">This builder</param>
  224. /// <param name="text">Target TMP_Text</param>
  225. /// <returns>Handle of the created motion data.</returns>
  226. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString64Bytes, TOptions, TAdapter> builder, TMP_Text text)
  227. where TOptions : unmanaged, IMotionOptions
  228. where TAdapter : unmanaged, IMotionAdapter<FixedString64Bytes, TOptions>
  229. {
  230. Error.IsNull(text);
  231. return builder.BindWithState(text, static (x, target) =>
  232. {
  233. var enumerator = x.GetEnumerator();
  234. var length = 0;
  235. var buffer = ArrayPool<char>.Shared.Rent(128);
  236. fixed (char* c = buffer)
  237. {
  238. Unicode.Utf8ToUtf16(x.GetUnsafePtr(), x.Length, c, out length, x.Length * 2);
  239. }
  240. target.SetText(buffer, 0, length);
  241. ArrayPool<char>.Shared.Return(buffer);
  242. });
  243. }
  244. /// <summary>
  245. /// Create a motion data and bind it to TMP_Text.text.
  246. /// </summary>
  247. /// <remarks>
  248. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  249. /// </remarks>
  250. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  251. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  252. /// <param name="builder">This builder</param>
  253. /// <param name="text">Target TMP_Text</param>
  254. /// <returns>Handle of the created motion data.</returns>
  255. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString128Bytes, TOptions, TAdapter> builder, TMP_Text text)
  256. where TOptions : unmanaged, IMotionOptions
  257. where TAdapter : unmanaged, IMotionAdapter<FixedString128Bytes, TOptions>
  258. {
  259. Error.IsNull(text);
  260. return builder.BindWithState(text, static (x, target) =>
  261. {
  262. var enumerator = x.GetEnumerator();
  263. var length = 0;
  264. var buffer = ArrayPool<char>.Shared.Rent(256);
  265. fixed (char* c = buffer)
  266. {
  267. Unicode.Utf8ToUtf16(x.GetUnsafePtr(), x.Length, c, out length, x.Length * 2);
  268. }
  269. target.SetText(buffer, 0, length);
  270. ArrayPool<char>.Shared.Return(buffer);
  271. });
  272. }
  273. /// <summary>
  274. /// Create a motion data and bind it to TMP_Text.text.
  275. /// </summary>
  276. /// <remarks>
  277. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  278. /// </remarks>
  279. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  280. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  281. /// <param name="builder">This builder</param>
  282. /// <param name="text">Target TMP_Text</param>
  283. /// <returns>Handle of the created motion data.</returns>
  284. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString512Bytes, TOptions, TAdapter> builder, TMP_Text text)
  285. where TOptions : unmanaged, IMotionOptions
  286. where TAdapter : unmanaged, IMotionAdapter<FixedString512Bytes, TOptions>
  287. {
  288. Error.IsNull(text);
  289. return builder.BindWithState(text, static (x, target) =>
  290. {
  291. var enumerator = x.GetEnumerator();
  292. var length = 0;
  293. var buffer = ArrayPool<char>.Shared.Rent(1024);
  294. fixed (char* c = buffer)
  295. {
  296. Unicode.Utf8ToUtf16(x.GetUnsafePtr(), x.Length, c, out length, x.Length * 2);
  297. }
  298. target.SetText(buffer, 0, length);
  299. ArrayPool<char>.Shared.Return(buffer);
  300. });
  301. }
  302. /// <summary>
  303. /// Create a motion data and bind it to TMP_Text.text.
  304. /// </summary>
  305. /// <remarks>
  306. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  307. /// </remarks>
  308. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  309. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  310. /// <param name="builder">This builder</param>
  311. /// <param name="text">Target TMP_Text</param>
  312. /// <returns>Handle of the created motion data.</returns>
  313. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString4096Bytes, TOptions, TAdapter> builder, TMP_Text text)
  314. where TOptions : unmanaged, IMotionOptions
  315. where TAdapter : unmanaged, IMotionAdapter<FixedString4096Bytes, TOptions>
  316. {
  317. Error.IsNull(text);
  318. return builder.BindWithState(text, static (x, target) =>
  319. {
  320. var enumerator = x.GetEnumerator();
  321. var length = 0;
  322. var buffer = ArrayPool<char>.Shared.Rent(8192);
  323. fixed (char* c = buffer)
  324. {
  325. Unicode.Utf8ToUtf16(x.GetUnsafePtr(), x.Length, c, out length, x.Length * 2);
  326. }
  327. target.SetText(buffer, 0, length);
  328. ArrayPool<char>.Shared.Return(buffer);
  329. });
  330. }
  331. /// <summary>
  332. /// Create a motion data and bind it to TMP_Text.text.
  333. /// </summary>
  334. /// <remarks>
  335. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  336. /// </remarks>
  337. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  338. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  339. /// <param name="builder">This builder</param>
  340. /// <returns>Handle of the created motion data.</returns>
  341. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
  342. where TOptions : unmanaged, IMotionOptions
  343. where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
  344. {
  345. Error.IsNull(text);
  346. return builder.BindWithState(text, static (x, target) =>
  347. {
  348. var buffer = ArrayPool<char>.Shared.Rent(128);
  349. var bufferOffset = 0;
  350. Utf16StringHelper.WriteInt32(ref buffer, ref bufferOffset, x);
  351. target.SetText(buffer, 0, bufferOffset);
  352. ArrayPool<char>.Shared.Return(buffer);
  353. });
  354. }
  355. /// <summary>
  356. /// Create a motion data and bind it to TMP_Text.text.
  357. /// </summary>
  358. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  359. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  360. /// <param name="builder">This builder</param>
  361. /// <param name="text">Target TMP_Text</param>
  362. /// <param name="format">Format string</param>
  363. /// <returns>Handle of the created motion data.</returns>
  364. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text, string format)
  365. where TOptions : unmanaged, IMotionOptions
  366. where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
  367. {
  368. Error.IsNull(text);
  369. return builder.BindWithState(text, format, static (x, target, format) =>
  370. {
  371. #if LITMOTION_SUPPORT_ZSTRING
  372. target.SetTextFormat(format, x);
  373. #else
  374. target.text = string.Format(format, x);
  375. #endif
  376. });
  377. }
  378. /// <summary>
  379. /// Create a motion data and bind it to TMP_Text.text.
  380. /// </summary>
  381. /// <remarks>
  382. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  383. /// </remarks>
  384. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  385. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  386. /// <param name="builder">This builder</param>
  387. /// <returns>Handle of the created motion data.</returns>
  388. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<long, TOptions, TAdapter> builder, TMP_Text text)
  389. where TOptions : unmanaged, IMotionOptions
  390. where TAdapter : unmanaged, IMotionAdapter<long, TOptions>
  391. {
  392. Error.IsNull(text);
  393. return builder.BindWithState(text, static (x, target) =>
  394. {
  395. var buffer = ArrayPool<char>.Shared.Rent(128);
  396. var bufferOffset = 0;
  397. Utf16StringHelper.WriteInt64(ref buffer, ref bufferOffset, x);
  398. target.SetText(buffer, 0, bufferOffset);
  399. ArrayPool<char>.Shared.Return(buffer);
  400. });
  401. }
  402. /// <summary>
  403. /// Create a motion data and bind it to TMP_Text.text.
  404. /// </summary>
  405. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  406. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  407. /// <param name="builder">This builder</param>
  408. /// <param name="text">Target TMP_Text</param>
  409. /// <param name="format">Format string</param>
  410. /// <returns>Handle of the created motion data.</returns>
  411. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<long, TOptions, TAdapter> builder, TMP_Text text, string format)
  412. where TOptions : unmanaged, IMotionOptions
  413. where TAdapter : unmanaged, IMotionAdapter<long, TOptions>
  414. {
  415. Error.IsNull(text);
  416. return builder.BindWithState(text, format, static (x, target, format) =>
  417. {
  418. #if LITMOTION_SUPPORT_ZSTRING
  419. target.SetTextFormat(format, x);
  420. #else
  421. target.text = string.Format(format, x);
  422. #endif
  423. });
  424. }
  425. /// <summary>
  426. /// Create a motion data and bind it to TMP_Text.text.
  427. /// </summary>
  428. /// <remarks>
  429. /// Note: This extension method uses TMP_Text.SetText() to achieve zero allocation, so it is recommended to use this method when binding to text.
  430. /// </remarks>
  431. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  432. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  433. /// <param name="builder">This builder</param>
  434. /// <returns>Handle of the created motion data.</returns>
  435. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
  436. where TOptions : unmanaged, IMotionOptions
  437. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  438. {
  439. const string format = "{0}";
  440. Error.IsNull(text);
  441. return builder.BindWithState(text, static (x, target) =>
  442. {
  443. #if LITMOTION_SUPPORT_ZSTRING
  444. target.SetTextFormat(format, x);
  445. #else
  446. target.SetText(format, x);
  447. #endif
  448. });
  449. }
  450. /// <summary>
  451. /// Create a motion data and bind it to TMP_Text.text.
  452. /// </summary>
  453. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  454. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  455. /// <param name="builder">This builder</param>
  456. /// <param name="text">Target TMP_Text</param>
  457. /// <param name="format">Format string</param>
  458. /// <returns>Handle of the created motion data.</returns>
  459. public unsafe static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, string format)
  460. where TOptions : unmanaged, IMotionOptions
  461. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  462. {
  463. Error.IsNull(text);
  464. return builder.BindWithState(text, format, static (x, target, format) =>
  465. {
  466. #if LITMOTION_SUPPORT_ZSTRING
  467. target.SetTextFormat(format, x);
  468. #else
  469. target.text = string.Format(format, x);
  470. #endif
  471. });
  472. }
  473. /// <summary>
  474. /// Create motion data and bind it to the character color.
  475. /// </summary>
  476. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  477. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  478. /// <param name="builder">This builder</param>
  479. /// <param name="text">Target TMP_Text</param>
  480. /// <param name="charIndex">Target character index</param>
  481. /// <returns>Handle of the created motion data.</returns>
  482. public static MotionHandle BindToTMPCharColor<TOptions, TAdapter>(this MotionBuilder<Color, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  483. where TOptions : unmanaged, IMotionOptions
  484. where TAdapter : unmanaged, IMotionAdapter<Color, TOptions>
  485. {
  486. Error.IsNull(text);
  487. var animator = TextMeshProMotionAnimator.Get(text);
  488. animator.EnsureCapacity(charIndex + 1);
  489. var handle = builder.BindWithState(animator, (x, target) =>
  490. {
  491. animator.charInfoArray[charIndex].color = x;
  492. });
  493. animator.motionHandleList.Add(handle);
  494. return handle;
  495. }
  496. /// <summary>
  497. /// Create motion data and bind it to the character color.r.
  498. /// </summary>
  499. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  500. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  501. /// <param name="builder">This builder</param>
  502. /// <param name="text">Target TMP_Text</param>
  503. /// <param name="charIndex">Target character index</param>
  504. /// <returns>Handle of the created motion data.</returns>
  505. public static MotionHandle BindToTMPCharColorR<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  506. where TOptions : unmanaged, IMotionOptions
  507. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  508. {
  509. Error.IsNull(text);
  510. var animator = TextMeshProMotionAnimator.Get(text);
  511. animator.EnsureCapacity(charIndex + 1);
  512. var handle = builder.BindWithState(animator, (x, target) =>
  513. {
  514. animator.charInfoArray[charIndex].color.r = x;
  515. });
  516. animator.motionHandleList.Add(handle);
  517. return handle;
  518. }
  519. /// <summary>
  520. /// Create motion data and bind it to the character color.g.
  521. /// </summary>
  522. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  523. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  524. /// <param name="builder">This builder</param>
  525. /// <param name="text">Target TMP_Text</param>
  526. /// <param name="charIndex">Target character index</param>
  527. /// <returns>Handle of the created motion data.</returns>
  528. public static MotionHandle BindToTMPCharColorG<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  529. where TOptions : unmanaged, IMotionOptions
  530. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  531. {
  532. Error.IsNull(text);
  533. var animator = TextMeshProMotionAnimator.Get(text);
  534. animator.EnsureCapacity(charIndex + 1);
  535. var handle = builder.BindWithState(animator, (x, target) =>
  536. {
  537. animator.charInfoArray[charIndex].color.g = x;
  538. });
  539. animator.motionHandleList.Add(handle);
  540. return handle;
  541. }
  542. /// <summary>
  543. /// Create motion data and bind it to the character color.b.
  544. /// </summary>
  545. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  546. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  547. /// <param name="builder">This builder</param>
  548. /// <param name="text">Target TMP_Text</param>
  549. /// <param name="charIndex">Target character index</param>
  550. /// <returns>Handle of the created motion data.</returns>
  551. public static MotionHandle BindToTMPCharColorB<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  552. where TOptions : unmanaged, IMotionOptions
  553. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  554. {
  555. Error.IsNull(text);
  556. var animator = TextMeshProMotionAnimator.Get(text);
  557. animator.EnsureCapacity(charIndex + 1);
  558. var handle = builder.BindWithState(animator, (x, target) =>
  559. {
  560. animator.charInfoArray[charIndex].color.b = x;
  561. });
  562. animator.motionHandleList.Add(handle);
  563. return handle;
  564. }
  565. /// <summary>
  566. /// Create motion data and bind it to the character color.a.
  567. /// </summary>
  568. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  569. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  570. /// <param name="builder">This builder</param>
  571. /// <param name="text">Target TMP_Text</param>
  572. /// <param name="charIndex">Target character index</param>
  573. /// <returns>Handle of the created motion data.</returns>
  574. public static MotionHandle BindToTMPCharColorA<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  575. where TOptions : unmanaged, IMotionOptions
  576. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  577. {
  578. Error.IsNull(text);
  579. var animator = TextMeshProMotionAnimator.Get(text);
  580. animator.EnsureCapacity(charIndex + 1);
  581. var handle = builder.BindWithState(animator, (x, target) =>
  582. {
  583. animator.charInfoArray[charIndex].color.a = x;
  584. });
  585. animator.motionHandleList.Add(handle);
  586. return handle;
  587. }
  588. /// <summary>
  589. /// Create motion data and bind it to the character position.
  590. /// </summary>
  591. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  592. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  593. /// <param name="builder">This builder</param>
  594. /// <param name="text">Target TMP_Text</param>
  595. /// <param name="charIndex">Target character index</param>
  596. /// <returns>Handle of the created motion data.</returns>
  597. public static MotionHandle BindToTMPCharPosition<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  598. where TOptions : unmanaged, IMotionOptions
  599. where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
  600. {
  601. Error.IsNull(text);
  602. var animator = TextMeshProMotionAnimator.Get(text);
  603. animator.EnsureCapacity(charIndex + 1);
  604. var handle = builder.BindWithState(animator, (x, target) =>
  605. {
  606. animator.charInfoArray[charIndex].position = x;
  607. });
  608. animator.motionHandleList.Add(handle);
  609. return handle;
  610. }
  611. /// <summary>
  612. /// Create motion data and bind it to the character position.x.
  613. /// </summary>
  614. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  615. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  616. /// <param name="builder">This builder</param>
  617. /// <param name="text">Target TMP_Text</param>
  618. /// <param name="charIndex">Target character index</param>
  619. /// <returns>Handle of the created motion data.</returns>
  620. public static MotionHandle BindToTMPCharPositionX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  621. where TOptions : unmanaged, IMotionOptions
  622. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  623. {
  624. Error.IsNull(text);
  625. var animator = TextMeshProMotionAnimator.Get(text);
  626. animator.EnsureCapacity(charIndex + 1);
  627. var handle = builder.BindWithState(animator, (x, target) =>
  628. {
  629. animator.charInfoArray[charIndex].position.x = x;
  630. });
  631. animator.motionHandleList.Add(handle);
  632. return handle;
  633. }
  634. /// <summary>
  635. /// Create motion data and bind it to the character position.y.
  636. /// </summary>
  637. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  638. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  639. /// <param name="builder">This builder</param>
  640. /// <param name="text">Target TMP_Text</param>
  641. /// <param name="charIndex">Target character index</param>
  642. /// <returns>Handle of the created motion data.</returns>
  643. public static MotionHandle BindToTMPCharPositionY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  644. where TOptions : unmanaged, IMotionOptions
  645. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  646. {
  647. Error.IsNull(text);
  648. var animator = TextMeshProMotionAnimator.Get(text);
  649. animator.EnsureCapacity(charIndex + 1);
  650. var handle = builder.BindWithState(animator, (x, target) =>
  651. {
  652. animator.charInfoArray[charIndex].position.y = x;
  653. });
  654. animator.motionHandleList.Add(handle);
  655. return handle;
  656. }
  657. /// <summary>
  658. /// Create motion data and bind it to the character position.z.
  659. /// </summary>
  660. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  661. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  662. /// <param name="builder">This builder</param>
  663. /// <param name="text">Target TMP_Text</param>
  664. /// <param name="charIndex">Target character index</param>
  665. /// <returns>Handle of the created motion data.</returns>
  666. public static MotionHandle BindToTMPCharPositionZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  667. where TOptions : unmanaged, IMotionOptions
  668. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  669. {
  670. Error.IsNull(text);
  671. var animator = TextMeshProMotionAnimator.Get(text);
  672. animator.EnsureCapacity(charIndex + 1);
  673. var handle = builder.BindWithState(animator, (x, target) =>
  674. {
  675. animator.charInfoArray[charIndex].position.z = x;
  676. });
  677. animator.motionHandleList.Add(handle);
  678. return handle;
  679. }
  680. /// <summary>
  681. /// Create motion data and bind it to the character rotation.
  682. /// </summary>
  683. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  684. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  685. /// <param name="builder">This builder</param>
  686. /// <param name="text">Target TMP_Text</param>
  687. /// <param name="charIndex">Target character index</param>
  688. /// <returns>Handle of the created motion data.</returns>
  689. public static MotionHandle BindToTMPCharRotation<TOptions, TAdapter>(this MotionBuilder<Quaternion, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  690. where TOptions : unmanaged, IMotionOptions
  691. where TAdapter : unmanaged, IMotionAdapter<Quaternion, TOptions>
  692. {
  693. Error.IsNull(text);
  694. var animator = TextMeshProMotionAnimator.Get(text);
  695. animator.EnsureCapacity(charIndex + 1);
  696. var handle = builder.BindWithState(animator, (x, target) =>
  697. {
  698. animator.charInfoArray[charIndex].rotation = x;
  699. });
  700. animator.motionHandleList.Add(handle);
  701. return handle;
  702. }
  703. /// <summary>
  704. /// Create motion data and bind it to the character rotation (using euler angles).
  705. /// </summary>
  706. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  707. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  708. /// <param name="builder">This builder</param>
  709. /// <param name="text">Target TMP_Text</param>
  710. /// <param name="charIndex">Target character index</param>
  711. /// <returns>Handle of the created motion data.</returns>
  712. public static MotionHandle BindToTMPCharEulerAngles<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  713. where TOptions : unmanaged, IMotionOptions
  714. where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
  715. {
  716. Error.IsNull(text);
  717. var animator = TextMeshProMotionAnimator.Get(text);
  718. animator.EnsureCapacity(charIndex + 1);
  719. var handle = builder.BindWithState(animator, (x, target) =>
  720. {
  721. animator.charInfoArray[charIndex].rotation = Quaternion.Euler(x);
  722. });
  723. animator.motionHandleList.Add(handle);
  724. return handle;
  725. }
  726. /// <summary>
  727. /// Create motion data and bind it to the character rotation (using euler angles).
  728. /// </summary>
  729. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  730. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  731. /// <param name="builder">This builder</param>
  732. /// <param name="text">Target TMP_Text</param>
  733. /// <param name="charIndex">Target character index</param>
  734. /// <returns>Handle of the created motion data.</returns>
  735. public static MotionHandle BindToTMPCharEulerAnglesX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  736. where TOptions : unmanaged, IMotionOptions
  737. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  738. {
  739. Error.IsNull(text);
  740. var animator = TextMeshProMotionAnimator.Get(text);
  741. animator.EnsureCapacity(charIndex + 1);
  742. var handle = builder.BindWithState(animator, (x, target) =>
  743. {
  744. var eulerAngles = animator.charInfoArray[charIndex].rotation.eulerAngles;
  745. eulerAngles.x = x;
  746. animator.charInfoArray[charIndex].rotation = Quaternion.Euler(eulerAngles);
  747. });
  748. animator.motionHandleList.Add(handle);
  749. return handle;
  750. }
  751. /// <summary>
  752. /// Create motion data and bind it to the character rotation (using euler angles).
  753. /// </summary>
  754. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  755. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  756. /// <param name="builder">This builder</param>
  757. /// <param name="text">Target TMP_Text</param>
  758. /// <param name="charIndex">Target character index</param>
  759. /// <returns>Handle of the created motion data.</returns>
  760. public static MotionHandle BindToTMPCharEulerAnglesY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  761. where TOptions : unmanaged, IMotionOptions
  762. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  763. {
  764. Error.IsNull(text);
  765. var animator = TextMeshProMotionAnimator.Get(text);
  766. animator.EnsureCapacity(charIndex + 1);
  767. var handle = builder.BindWithState(animator, (x, target) =>
  768. {
  769. var eulerAngles = animator.charInfoArray[charIndex].rotation.eulerAngles;
  770. eulerAngles.y = x;
  771. animator.charInfoArray[charIndex].rotation = Quaternion.Euler(eulerAngles);
  772. });
  773. animator.motionHandleList.Add(handle);
  774. return handle;
  775. }
  776. /// <summary>
  777. /// Create motion data and bind it to the character rotation (using euler angles).
  778. /// </summary>
  779. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  780. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  781. /// <param name="builder">This builder</param>
  782. /// <param name="text">Target TMP_Text</param>
  783. /// <param name="charIndex">Target character index</param>
  784. /// <returns>Handle of the created motion data.</returns>
  785. public static MotionHandle BindToTMPCharEulerAnglesZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  786. where TOptions : unmanaged, IMotionOptions
  787. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  788. {
  789. Error.IsNull(text);
  790. var animator = TextMeshProMotionAnimator.Get(text);
  791. animator.EnsureCapacity(charIndex + 1);
  792. var handle = builder.BindWithState(animator, (x, target) =>
  793. {
  794. var eulerAngles = animator.charInfoArray[charIndex].rotation.eulerAngles;
  795. eulerAngles.z = x;
  796. animator.charInfoArray[charIndex].rotation = Quaternion.Euler(eulerAngles);
  797. });
  798. animator.motionHandleList.Add(handle);
  799. return handle;
  800. }
  801. /// <summary>
  802. /// Create motion data and bind it to the character scale.
  803. /// </summary>
  804. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  805. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  806. /// <param name="builder">This builder</param>
  807. /// <param name="text">Target TMP_Text</param>
  808. /// <param name="charIndex">Target character index</param>
  809. /// <returns>Handle of the created motion data.</returns>
  810. public static MotionHandle BindToTMPCharScale<TOptions, TAdapter>(this MotionBuilder<Vector3, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  811. where TOptions : unmanaged, IMotionOptions
  812. where TAdapter : unmanaged, IMotionAdapter<Vector3, TOptions>
  813. {
  814. Error.IsNull(text);
  815. var animator = TextMeshProMotionAnimator.Get(text);
  816. animator.EnsureCapacity(charIndex + 1);
  817. var handle = builder.BindWithState(animator, (x, target) =>
  818. {
  819. animator.charInfoArray[charIndex].scale = x;
  820. });
  821. animator.motionHandleList.Add(handle);
  822. return handle;
  823. }
  824. /// <summary>
  825. /// Create motion data and bind it to the character scale.x.
  826. /// </summary>
  827. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  828. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  829. /// <param name="builder">This builder</param>
  830. /// <param name="text">Target TMP_Text</param>
  831. /// <param name="charIndex">Target character index</param>
  832. /// <returns>Handle of the created motion data.</returns>
  833. public static MotionHandle BindToTMPCharScaleX<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  834. where TOptions : unmanaged, IMotionOptions
  835. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  836. {
  837. Error.IsNull(text);
  838. var animator = TextMeshProMotionAnimator.Get(text);
  839. animator.EnsureCapacity(charIndex + 1);
  840. var handle = builder.BindWithState(animator, (x, target) =>
  841. {
  842. animator.charInfoArray[charIndex].scale.x = x;
  843. });
  844. animator.motionHandleList.Add(handle);
  845. return handle;
  846. }
  847. /// <summary>
  848. /// Create motion data and bind it to the character scale.y.
  849. /// </summary>
  850. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  851. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  852. /// <param name="builder">This builder</param>
  853. /// <param name="text">Target TMP_Text</param>
  854. /// <param name="charIndex">Target character index</param>
  855. /// <returns>Handle of the created motion data.</returns>
  856. public static MotionHandle BindToTMPCharScaleY<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  857. where TOptions : unmanaged, IMotionOptions
  858. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  859. {
  860. Error.IsNull(text);
  861. var animator = TextMeshProMotionAnimator.Get(text);
  862. animator.EnsureCapacity(charIndex + 1);
  863. var handle = builder.BindWithState(animator, (x, target) =>
  864. {
  865. animator.charInfoArray[charIndex].scale.y = x;
  866. });
  867. animator.motionHandleList.Add(handle);
  868. return handle;
  869. }
  870. /// <summary>
  871. /// Create motion data and bind it to the character scale.z.
  872. /// </summary>
  873. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  874. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  875. /// <param name="builder">This builder</param>
  876. /// <param name="text">Target TMP_Text</param>
  877. /// <param name="charIndex">Target character index</param>
  878. /// <returns>Handle of the created motion data.</returns>
  879. public static MotionHandle BindToTMPCharScaleZ<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, int charIndex)
  880. where TOptions : unmanaged, IMotionOptions
  881. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  882. {
  883. Error.IsNull(text);
  884. var animator = TextMeshProMotionAnimator.Get(text);
  885. animator.EnsureCapacity(charIndex + 1);
  886. var handle = builder.BindWithState(animator, (x, target) =>
  887. {
  888. animator.charInfoArray[charIndex].scale.z = x;
  889. });
  890. animator.motionHandleList.Add(handle);
  891. return handle;
  892. }
  893. }
  894. }
  895. #endif