DOTweenTextMeshPro.cs 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2015/03/27 19:02
  3. //
  4. // License Copyright (c) Daniele Giardini.
  5. // This work is subject to the terms at http://dotween.demigiant.com/license.php
  6. #if true // MODULE_MARKER
  7. using System;
  8. using System.Globalization;
  9. using System.Collections.Generic;
  10. using DG.Tweening.Core;
  11. using DG.Tweening.Plugins.Options;
  12. using UnityEngine;
  13. using TMPro;
  14. using Object = UnityEngine.Object;
  15. namespace DG.Tweening
  16. {
  17. public enum TMPSkewSpanMode
  18. {
  19. /// <summary>Applies the skew as-is (like normal skew works): the longer the text-span the higher the last character will be</summary>
  20. Default,
  21. /// <summary>Applies the skew scaled by the size of the text-span: the max skew/displacement will be the given skew factor</summary>
  22. AsMaxSkewFactor
  23. }
  24. /// <summary>
  25. /// Methods that extend TMP_Text objects and allow to directly create and control tweens from their instances.
  26. /// </summary>
  27. public static class ShortcutExtensionsTMPText
  28. {
  29. #region Colors
  30. /// <summary>Tweens a TextMeshPro's color to the given value.
  31. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  32. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  33. public static TweenerCore<Color, Color, ColorOptions> DOColor(this TMP_Text target, Color endValue, float duration)
  34. {
  35. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  36. t.SetTarget(target);
  37. return t;
  38. }
  39. /// <summary>Tweens a TextMeshPro's faceColor to the given value.
  40. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  41. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  42. public static TweenerCore<Color, Color, ColorOptions> DOFaceColor(this TMP_Text target, Color32 endValue, float duration)
  43. {
  44. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.faceColor, x => target.faceColor = x, endValue, duration);
  45. t.SetTarget(target);
  46. return t;
  47. }
  48. /// <summary>Tweens a TextMeshPro's outlineColor to the given value.
  49. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  50. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  51. public static TweenerCore<Color, Color, ColorOptions> DOOutlineColor(this TMP_Text target, Color32 endValue, float duration)
  52. {
  53. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.outlineColor, x => target.outlineColor = x, endValue, duration);
  54. t.SetTarget(target);
  55. return t;
  56. }
  57. /// <summary>Tweens a TextMeshPro's glow color to the given value.
  58. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  59. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  60. /// <param name="useSharedMaterial">If TRUE will use the fontSharedMaterial instead than the fontMaterial</param>
  61. public static TweenerCore<Color, Color, ColorOptions> DOGlowColor(this TMP_Text target, Color endValue, float duration, bool useSharedMaterial = false)
  62. {
  63. TweenerCore<Color, Color, ColorOptions> t = useSharedMaterial
  64. ? target.fontSharedMaterial.DOColor(endValue, "_GlowColor", duration)
  65. : target.fontMaterial.DOColor(endValue, "_GlowColor", duration);
  66. t.SetTarget(target);
  67. return t;
  68. }
  69. /// <summary>Tweens a TextMeshPro's alpha color to the given value.
  70. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  71. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  72. public static TweenerCore<Color, Color, ColorOptions> DOFade(this TMP_Text target, float endValue, float duration)
  73. {
  74. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  75. t.SetTarget(target);
  76. return t;
  77. }
  78. /// <summary>Tweens a TextMeshPro faceColor's alpha to the given value.
  79. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  80. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  81. public static TweenerCore<Color, Color, ColorOptions> DOFaceFade(this TMP_Text target, float endValue, float duration)
  82. {
  83. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.faceColor, x => target.faceColor = x, endValue, duration);
  84. t.SetTarget(target);
  85. return t;
  86. }
  87. #endregion
  88. #region Other
  89. /// <summary>Tweens a TextMeshPro's scale to the given value (using correct uniform scale as TMP requires).
  90. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  91. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  92. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScale(this TMP_Text target, float endValue, float duration)
  93. {
  94. Transform trans = target.transform;
  95. Vector3 endValueV3 = new Vector3(endValue, endValue, endValue);
  96. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => trans.localScale, x => trans.localScale = x, endValueV3, duration);
  97. t.SetTarget(target);
  98. return t;
  99. }
  100. /// <summary>
  101. /// Tweens a TextMeshPro's text from one integer to another, with options for thousands separators
  102. /// </summary>
  103. /// <param name="fromValue">The value to start from</param>
  104. /// <param name="endValue">The end value to reach</param>
  105. /// <param name="duration">The duration of the tween</param>
  106. /// <param name="addThousandsSeparator">If TRUE (default) also adds thousands separators</param>
  107. /// <param name="culture">The <see cref="CultureInfo"/> to use (InvariantCulture if NULL)</param>
  108. public static TweenerCore<int, int, NoOptions> DOCounter(
  109. this TMP_Text target, int fromValue, int endValue, float duration, bool addThousandsSeparator = true, CultureInfo culture = null
  110. ){
  111. int v = fromValue;
  112. CultureInfo cInfo = !addThousandsSeparator ? null : culture ?? CultureInfo.InvariantCulture;
  113. TweenerCore<int, int, NoOptions> t = DOTween.To(() => v, x => {
  114. v = x;
  115. target.text = addThousandsSeparator
  116. ? v.ToString("N0", cInfo)
  117. : v.ToString();
  118. }, endValue, duration);
  119. t.SetTarget(target);
  120. return t;
  121. }
  122. /// <summary>Tweens a TextMeshPro's fontSize to the given value.
  123. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  124. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  125. public static TweenerCore<float, float, FloatOptions> DOFontSize(this TMP_Text target, float endValue, float duration)
  126. {
  127. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.fontSize, x => target.fontSize = x, endValue, duration);
  128. t.SetTarget(target);
  129. return t;
  130. }
  131. /// <summary>Tweens a TextMeshPro's maxVisibleCharacters to the given value.
  132. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  133. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  134. public static TweenerCore<int, int, NoOptions> DOMaxVisibleCharacters(this TMP_Text target, int endValue, float duration)
  135. {
  136. TweenerCore<int, int, NoOptions> t = DOTween.To(() => target.maxVisibleCharacters, x => target.maxVisibleCharacters = x, endValue, duration);
  137. t.SetTarget(target);
  138. return t;
  139. }
  140. /// <summary>Tweens a TextMeshPro's text to the given value.
  141. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  142. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  143. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  144. /// otherwise all tags will be considered as normal text</param>
  145. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  146. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  147. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  148. /// Leave it to NULL (default) to use default ones</param>
  149. public static TweenerCore<string, string, StringOptions> DOText(this TMP_Text target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  150. {
  151. TweenerCore<string, string, StringOptions> t = DOTween.To(() => target.text, x => target.text = x, endValue, duration);
  152. t.SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  153. .SetTarget(target);
  154. return t;
  155. }
  156. #endregion
  157. }
  158. #region DOTweenTMPAnimator
  159. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  160. // ███ CLASS ███████████████████████████████████████████████████████████████████████████████████████████████████████████
  161. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  162. /// <summary>
  163. /// Wrapper for <see cref="TMP_Text"/> objects that enables per-character tweening
  164. /// (you don't need this if instead you want to animate the whole text object).
  165. /// It also contains various handy methods to simply deform text without animating it ;)
  166. /// <para><code>EXAMPLE:<para/>
  167. /// DOTweenTMPAnimator animator = new DOTweenTMPAnimator(myTextMeshProTextField);<para/>
  168. /// Tween tween = animator.DOCharScale(characterIndex, scaleValue, duration);
  169. /// </code></para>
  170. /// </summary>
  171. public class DOTweenTMPAnimator : IDisposable
  172. {
  173. static readonly Dictionary<TMP_Text,DOTweenTMPAnimator> _targetToAnimator = new Dictionary<TMP_Text,DOTweenTMPAnimator>();
  174. /// <summary><see cref="TMP_Text"/> that this animator is linked to</summary>
  175. public TMP_Text target { get; private set; }
  176. public TMP_TextInfo textInfo { get; private set; }
  177. readonly List<CharTransform> _charTransforms = new List<CharTransform>();
  178. TMP_MeshInfo[] _cachedMeshInfos;
  179. bool _ignoreTextChangedEvent;
  180. /// <summary>
  181. /// Creates a new instance of the <see cref="DOTweenTMPAnimator"/>, which is necessary to animate <see cref="TMP_Text"/> by single characters.<para/>
  182. /// If a <see cref="DOTweenTMPAnimator"/> already exists for the same <see cref="TMP_Text"/> object it will be disposed
  183. /// (but not its tweens, those you will have to kill manually).
  184. /// If you want to animate the whole text object you don't need this, and you can use direct <see cref="TMP_Text"/> DO shortcuts instead.<para/>
  185. /// IMPORTANT: the <see cref="TMP_Text"/> target must have been enabled/activated at least once before you can use it with this
  186. /// </summary>
  187. /// <param name="target">The <see cref="TMP_Text"/> that will be linked to this animator</param>
  188. public DOTweenTMPAnimator(TMP_Text target)
  189. {
  190. if (target == null) {
  191. Debugger.LogError("DOTweenTMPAnimator target can't be null");
  192. return;
  193. }
  194. if (!target.gameObject.activeInHierarchy) {
  195. Debugger.LogError("You can't create a DOTweenTMPAnimator if its target is disabled");
  196. return;
  197. }
  198. // Verify that there's no other animators for the same target, and in case dispose them
  199. if (_targetToAnimator.ContainsKey(target)) {
  200. if (Debugger.logPriority >= 2) {
  201. Debugger.Log(string.Format(
  202. "A DOTweenTMPAnimator for \"{0}\" already exists: disposing it because you can't have more than one DOTweenTMPAnimator" +
  203. " for the same TextMesh Pro object. If you have tweens running on the disposed DOTweenTMPAnimator you should kill them manually",
  204. target
  205. ));
  206. }
  207. _targetToAnimator[target].Dispose();
  208. _targetToAnimator.Remove(target);
  209. }
  210. //
  211. this.target = target;
  212. _targetToAnimator.Add(target, this);
  213. Refresh();
  214. // Listeners
  215. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(OnTextChanged);
  216. }
  217. /// <summary>
  218. /// If a <see cref="DOTweenTMPAnimator"/> instance exists for the given target disposes it
  219. /// </summary>
  220. public static void DisposeInstanceFor(TMP_Text target)
  221. {
  222. if (!_targetToAnimator.ContainsKey(target)) return;
  223. _targetToAnimator[target].Dispose();
  224. _targetToAnimator.Remove(target);
  225. }
  226. /// <summary>
  227. /// Clears and disposes of this object
  228. /// </summary>
  229. public void Dispose()
  230. {
  231. target = null;
  232. _charTransforms.Clear();
  233. textInfo = null;
  234. _cachedMeshInfos = null;
  235. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(OnTextChanged);
  236. }
  237. /// <summary>
  238. /// Refreshes the animator text data and resets all transformation data. Call this after you change the target <see cref="TMP_Text"/>
  239. /// </summary>
  240. public void Refresh()
  241. {
  242. _ignoreTextChangedEvent = true;
  243. target.ForceMeshUpdate(true);
  244. textInfo = target.textInfo;
  245. _cachedMeshInfos = textInfo.CopyMeshInfoVertexData();
  246. int totChars = textInfo.characterCount;
  247. int totCurrent = _charTransforms.Count;
  248. if (totCurrent > totChars) {
  249. _charTransforms.RemoveRange(totChars, totCurrent - totChars);
  250. totCurrent = totChars;
  251. }
  252. for (int i = 0; i < totCurrent; ++i) {
  253. CharTransform c = _charTransforms[i];
  254. c.ResetTransformationData();
  255. c.Refresh(textInfo, _cachedMeshInfos);
  256. _charTransforms[i] = c;
  257. }
  258. for (int i = totCurrent; i < totChars; ++i) _charTransforms.Add(new CharTransform(i, textInfo, _cachedMeshInfos));
  259. _ignoreTextChangedEvent = false;
  260. }
  261. /// <summary>
  262. /// Resets all deformations
  263. /// </summary>
  264. public void Reset()
  265. {
  266. int totCurrent = _charTransforms.Count;
  267. for (int i = 0; i < totCurrent; ++i) _charTransforms[i].ResetAll(target, textInfo.meshInfo, _cachedMeshInfos);
  268. }
  269. void OnTextChanged(Object obj)
  270. {
  271. if (_ignoreTextChangedEvent || target == null || obj != target) return;
  272. Refresh();
  273. }
  274. bool ValidateChar(int charIndex, bool isTween = true)
  275. {
  276. if (textInfo.characterCount <= charIndex) {
  277. Debugger.LogError(string.Format("CharIndex {0} doesn't exist", charIndex));
  278. return false;
  279. }
  280. if (!textInfo.characterInfo[charIndex].isVisible) {
  281. if (Debugger.logPriority > 1) {
  282. if (isTween) {
  283. Debugger.Log(string.Format(
  284. "CharIndex {0} isn't visible, ignoring it and returning an empty tween (TextMesh Pro will behave weirdly if invisible chars are included in the animation)",
  285. charIndex
  286. ));
  287. } else {
  288. Debugger.Log(string.Format("CharIndex {0} isn't visible, ignoring it", charIndex));
  289. }
  290. }
  291. return false;
  292. }
  293. return true;
  294. }
  295. bool ValidateSpan(int fromCharIndex, int toCharIndex, out int firstVisibleCharIndex, out int lastVisibleCharIndex)
  296. {
  297. firstVisibleCharIndex = -1; // First visible/existing charIndex from given index
  298. lastVisibleCharIndex = -1; // Last visible/existing charIndex backwards from given index
  299. int charCount = textInfo.characterCount;
  300. if (fromCharIndex >= charCount) return false;
  301. if (toCharIndex >= charCount) toCharIndex = charCount - 1;
  302. for (int i = fromCharIndex; i < toCharIndex + 1; ++i) {
  303. if (!_charTransforms[i].isVisible) continue;
  304. firstVisibleCharIndex = i;
  305. break;
  306. }
  307. if (firstVisibleCharIndex == -1) return false;
  308. for (int i = toCharIndex; i > firstVisibleCharIndex - 1; --i) {
  309. if (!_charTransforms[i].isVisible) continue;
  310. lastVisibleCharIndex = i;
  311. break;
  312. }
  313. if (lastVisibleCharIndex == -1) return false;
  314. return true;
  315. }
  316. #region Word Setters
  317. /// <summary>
  318. /// Skews a span of characters uniformly (like normal skew works in graphic applications)
  319. /// </summary>
  320. /// <param name="fromCharIndex">First char index of the span to skew</param>
  321. /// <param name="toCharIndex">Last char index of the span to skew</param>
  322. /// <param name="skewFactor">Skew factor</param>
  323. /// <param name="skewTop">If TRUE skews the top side of the span, otherwise the bottom one</param>
  324. public void SkewSpanX(int fromCharIndex, int toCharIndex, float skewFactor, bool skewTop = true)
  325. {
  326. int firstVisibleCharIndex, lastVisibleCharIndex;
  327. if (!ValidateSpan(fromCharIndex, toCharIndex, out firstVisibleCharIndex, out lastVisibleCharIndex)) return;
  328. for (int i = firstVisibleCharIndex; i < lastVisibleCharIndex + 1; ++i) {
  329. if (!_charTransforms[i].isVisible) continue;
  330. CharVertices v = _charTransforms[i].GetVertices();
  331. float skew = SkewCharX(i, skewFactor, skewTop);
  332. }
  333. }
  334. /// <summary>
  335. /// Skews a span of characters uniformly (like normal skew works in graphic applications)
  336. /// </summary>
  337. /// <param name="fromCharIndex">First char index of the span to skew</param>
  338. /// <param name="toCharIndex">Last char index of the span to skew</param>
  339. /// <param name="skewFactor">Skew factor</param>
  340. /// <param name="mode">Skew mode</param>
  341. /// <param name="skewRight">If TRUE skews the right side of the span, otherwise the left one</param>
  342. public void SkewSpanY(
  343. int fromCharIndex, int toCharIndex, float skewFactor,
  344. TMPSkewSpanMode mode = TMPSkewSpanMode.Default, bool skewRight = true
  345. ){
  346. int firstVisibleCharIndex, lastVisibleCharIndex;
  347. if (!ValidateSpan(fromCharIndex, toCharIndex, out firstVisibleCharIndex, out lastVisibleCharIndex)) return;
  348. if (mode == TMPSkewSpanMode.AsMaxSkewFactor) {
  349. CharVertices firstVisibleCharVertices = _charTransforms[firstVisibleCharIndex].GetVertices();
  350. CharVertices lastVisibleCharVertices = _charTransforms[lastVisibleCharIndex].GetVertices();
  351. float spanW = Mathf.Abs(lastVisibleCharVertices.bottomRight.x - firstVisibleCharVertices.bottomLeft.x);
  352. float spanH = Mathf.Abs(lastVisibleCharVertices.topRight.y - lastVisibleCharVertices.bottomRight.y);
  353. float ratio = spanH / spanW;
  354. skewFactor *= ratio;
  355. }
  356. float offsetY = 0;
  357. CharVertices prevCharVertices = new CharVertices();
  358. float prevCharSkew = 0;
  359. if (skewRight) {
  360. for (int i = firstVisibleCharIndex; i < lastVisibleCharIndex + 1; ++i) {
  361. if (!_charTransforms[i].isVisible) continue;
  362. CharVertices v = _charTransforms[i].GetVertices();
  363. float skew = SkewCharY(i, skewFactor, skewRight);
  364. if (i > firstVisibleCharIndex) {
  365. float prevCharW = Mathf.Abs(prevCharVertices.bottomLeft.x - prevCharVertices.bottomRight.x);
  366. float charsDist = Mathf.Abs(v.bottomLeft.x - prevCharVertices.bottomRight.x);
  367. offsetY += prevCharSkew + (prevCharSkew * charsDist) / prevCharW;
  368. SetCharOffset(i, new Vector3(0, _charTransforms[i].offset.y + offsetY, 0));
  369. }
  370. prevCharVertices = v;
  371. prevCharSkew = skew;
  372. }
  373. } else {
  374. for (int i = lastVisibleCharIndex; i > firstVisibleCharIndex - 1; --i) {
  375. if (!_charTransforms[i].isVisible) continue;
  376. CharVertices v = _charTransforms[i].GetVertices();
  377. float skew = SkewCharY(i, skewFactor, skewRight);
  378. if (i < lastVisibleCharIndex) {
  379. float prevCharW = Mathf.Abs(prevCharVertices.bottomLeft.x - prevCharVertices.bottomRight.x);
  380. float charsDist = Mathf.Abs(v.bottomRight.x - prevCharVertices.bottomLeft.x);
  381. offsetY += prevCharSkew + (prevCharSkew * charsDist) / prevCharW;
  382. SetCharOffset(i, new Vector3(0, _charTransforms[i].offset.y + offsetY, 0));
  383. }
  384. prevCharVertices = v;
  385. prevCharSkew = skew;
  386. }
  387. }
  388. }
  389. #endregion
  390. #region Char Getters
  391. /// <summary>
  392. /// Returns the current color of the given character, if it exists and is visible.
  393. /// </summary>
  394. /// <param name="charIndex">Character index</param>
  395. public Color GetCharColor(int charIndex)
  396. {
  397. if (!ValidateChar(charIndex)) return Color.white;
  398. return _charTransforms[charIndex].GetColor(textInfo.meshInfo);
  399. }
  400. /// <summary>
  401. /// Returns the current offset of the given character, if it exists and is visible.
  402. /// </summary>
  403. /// <param name="charIndex">Character index</param>
  404. public Vector3 GetCharOffset(int charIndex)
  405. {
  406. if (!ValidateChar(charIndex)) return Vector3.zero;
  407. return _charTransforms[charIndex].offset;
  408. }
  409. /// <summary>
  410. /// Returns the current rotation of the given character, if it exists and is visible.
  411. /// </summary>
  412. /// <param name="charIndex">Character index</param>
  413. public Vector3 GetCharRotation(int charIndex)
  414. {
  415. if (!ValidateChar(charIndex)) return Vector3.zero;
  416. return _charTransforms[charIndex].rotation.eulerAngles;
  417. }
  418. /// <summary>
  419. /// Returns the current scale of the given character, if it exists and is visible.
  420. /// </summary>
  421. /// <param name="charIndex">Character index</param>
  422. public Vector3 GetCharScale(int charIndex)
  423. {
  424. if (!ValidateChar(charIndex)) return Vector3.zero;
  425. return _charTransforms[charIndex].scale;
  426. }
  427. #endregion
  428. #region Char Setters
  429. /// <summary>
  430. /// Immediately sets the color of the given character.
  431. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  432. /// </summary>
  433. /// <param name="charIndex">Character index</param>
  434. /// <param name="color">Color to set</param>
  435. public void SetCharColor(int charIndex, Color32 color)
  436. {
  437. if (!ValidateChar(charIndex)) return;
  438. CharTransform c = _charTransforms[charIndex];
  439. c.UpdateColor(target, color, textInfo.meshInfo);
  440. _charTransforms[charIndex] = c;
  441. }
  442. /// <summary>
  443. /// Immediately sets the offset of the given character.
  444. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  445. /// </summary>
  446. /// <param name="charIndex">Character index</param>
  447. /// <param name="offset">Offset to set</param>
  448. public void SetCharOffset(int charIndex, Vector3 offset)
  449. {
  450. if (!ValidateChar(charIndex)) return;
  451. CharTransform c = _charTransforms[charIndex];
  452. c.UpdateGeometry(target, offset, c.rotation, c.scale, _cachedMeshInfos);
  453. _charTransforms[charIndex] = c;
  454. }
  455. /// <summary>
  456. /// Immediately sets the rotation of the given character.
  457. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  458. /// </summary>
  459. /// <param name="charIndex">Character index</param>
  460. /// <param name="rotation">Rotation to set</param>
  461. public void SetCharRotation(int charIndex, Vector3 rotation)
  462. {
  463. if (!ValidateChar(charIndex)) return;
  464. CharTransform c = _charTransforms[charIndex];
  465. c.UpdateGeometry(target, c.offset, Quaternion.Euler(rotation), c.scale, _cachedMeshInfos);
  466. _charTransforms[charIndex] = c;
  467. }
  468. /// <summary>
  469. /// Immediately sets the scale of the given character.
  470. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  471. /// </summary>
  472. /// <param name="charIndex">Character index</param>
  473. /// <param name="scale">Scale to set</param>
  474. public void SetCharScale(int charIndex, Vector3 scale)
  475. {
  476. if (!ValidateChar(charIndex)) return;
  477. CharTransform c = _charTransforms[charIndex];
  478. c.UpdateGeometry(target, c.offset, c.rotation, scale, _cachedMeshInfos);
  479. _charTransforms[charIndex] = c;
  480. }
  481. /// <summary>
  482. /// Immediately shifts the vertices of the given character by the given factor.
  483. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  484. /// </summary>
  485. /// <param name="charIndex">Character index</param>
  486. /// <param name="topLeftShift">Top left offset</param>
  487. /// <param name="topRightShift">Top right offset</param>
  488. /// <param name="bottomLeftShift">Bottom left offset</param>
  489. /// <param name="bottomRightShift">Bottom right offset</param>
  490. public void ShiftCharVertices(int charIndex, Vector3 topLeftShift, Vector3 topRightShift, Vector3 bottomLeftShift, Vector3 bottomRightShift)
  491. {
  492. if (!ValidateChar(charIndex)) return;
  493. CharTransform c = _charTransforms[charIndex];
  494. c.ShiftVertices(target, topLeftShift, topRightShift, bottomLeftShift, bottomRightShift);
  495. _charTransforms[charIndex] = c;
  496. }
  497. /// <summary>
  498. /// Skews the given character horizontally along the X axis and returns the skew amount applied (based on the character's size)
  499. /// </summary>
  500. /// <param name="charIndex">Character index</param>
  501. /// <param name="skewFactor">skew amount</param>
  502. /// <param name="skewTop">If TRUE skews the top side of the character, otherwise the bottom one</param>
  503. public float SkewCharX(int charIndex, float skewFactor, bool skewTop = true)
  504. {
  505. if (!ValidateChar(charIndex)) return 0;
  506. Vector3 skewV = new Vector3(skewFactor, 0, 0);
  507. CharTransform c = _charTransforms[charIndex];
  508. if (skewTop) c.ShiftVertices(target, skewV, skewV, Vector3.zero, Vector3.zero);
  509. else c.ShiftVertices(target, Vector3.zero, Vector3.zero, skewV, skewV);
  510. _charTransforms[charIndex] = c;
  511. return skewFactor;
  512. }
  513. /// <summary>
  514. /// Skews the given character vertically along the Y axis and returns the skew amount applied (based on the character's size)
  515. /// </summary>
  516. /// <param name="charIndex">Character index</param>
  517. /// <param name="skewFactor">skew amount</param>
  518. /// <param name="skewRight">If TRUE skews the right side of the character, otherwise the left one</param>
  519. /// <param name="fixedSkew">If TRUE applies exactly the given <see cref="skewFactor"/>,
  520. /// otherwise modifies it based on the aspectRation of the character</param>
  521. public float SkewCharY(int charIndex, float skewFactor, bool skewRight = true, bool fixedSkew = false)
  522. {
  523. if (!ValidateChar(charIndex)) return 0;
  524. float skew = fixedSkew ? skewFactor : skewFactor * textInfo.characterInfo[charIndex].aspectRatio;
  525. Vector3 skewV = new Vector3(0, skew, 0);
  526. CharTransform c = _charTransforms[charIndex];
  527. if (skewRight) c.ShiftVertices(target, Vector3.zero, skewV, Vector3.zero, skewV);
  528. else c.ShiftVertices(target, skewV, Vector3.zero, skewV, Vector3.zero);
  529. _charTransforms[charIndex] = c;
  530. return skew;
  531. }
  532. /// <summary>
  533. /// Resets the eventual vertices shift applied to the given character via <see cref="ShiftCharVertices"/>.
  534. /// Will do nothing if the <see cref="charIndex"/> is invalid or the character isn't visible
  535. /// </summary>
  536. /// <param name="charIndex">Character index</param>
  537. public void ResetVerticesShift(int charIndex)
  538. {
  539. if (!ValidateChar(charIndex)) return;
  540. CharTransform c = _charTransforms[charIndex];
  541. c.ResetVerticesShift(target);
  542. _charTransforms[charIndex] = c;
  543. }
  544. #endregion
  545. #region Char Tweens
  546. /// <summary>Tweens a character's alpha to the given value and returns the <see cref="Tween"/>.
  547. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  548. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  549. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  550. public TweenerCore<Color, Color, ColorOptions> DOFadeChar(int charIndex, float endValue, float duration)
  551. {
  552. if (!ValidateChar(charIndex)) return null;
  553. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => _charTransforms[charIndex].GetColor(textInfo.meshInfo), x => {
  554. _charTransforms[charIndex].UpdateAlpha(target, x, textInfo.meshInfo);
  555. }, endValue, duration);
  556. return t;
  557. }
  558. /// <summary>Tweens a character's color to the given value and returns the <see cref="Tween"/>.
  559. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  560. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  561. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  562. public TweenerCore<Color, Color, ColorOptions> DOColorChar(int charIndex, Color endValue, float duration)
  563. {
  564. if (!ValidateChar(charIndex)) return null;
  565. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => _charTransforms[charIndex].GetColor(textInfo.meshInfo), x => {
  566. _charTransforms[charIndex].UpdateColor(target, x, textInfo.meshInfo);
  567. }, endValue, duration);
  568. return t;
  569. }
  570. /// <summary>Tweens a character's offset to the given value and returns the <see cref="Tween"/>.
  571. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  572. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  573. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  574. public TweenerCore<Vector3, Vector3, VectorOptions> DOOffsetChar(int charIndex, Vector3 endValue, float duration)
  575. {
  576. if (!ValidateChar(charIndex)) return null;
  577. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => _charTransforms[charIndex].offset, x => {
  578. CharTransform charT = _charTransforms[charIndex];
  579. charT.UpdateGeometry(target, x, charT.rotation, charT.scale, _cachedMeshInfos);
  580. _charTransforms[charIndex] = charT;
  581. }, endValue, duration);
  582. return t;
  583. }
  584. /// <summary>Tweens a character's rotation to the given value and returns the <see cref="Tween"/>.
  585. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  586. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  587. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  588. /// <param name="mode">Rotation mode</param>
  589. public TweenerCore<Quaternion, Vector3, QuaternionOptions> DORotateChar(int charIndex, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast)
  590. {
  591. if (!ValidateChar(charIndex)) return null;
  592. TweenerCore<Quaternion, Vector3, QuaternionOptions> t = DOTween.To(() => _charTransforms[charIndex].rotation, x => {
  593. CharTransform charT = _charTransforms[charIndex];
  594. charT.UpdateGeometry(target, charT.offset, x, charT.scale, _cachedMeshInfos);
  595. _charTransforms[charIndex] = charT;
  596. }, endValue, duration);
  597. t.plugOptions.rotateMode = mode;
  598. return t;
  599. }
  600. /// <summary>Tweens a character's scale to the given value and returns the <see cref="Tween"/>.
  601. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  602. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  603. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  604. public TweenerCore<Vector3, Vector3, VectorOptions> DOScaleChar(int charIndex, float endValue, float duration)
  605. {
  606. return DOScaleChar(charIndex, new Vector3(endValue, endValue, endValue), duration);
  607. }
  608. /// <summary>Tweens a character's color to the given value and returns the <see cref="Tween"/>.
  609. /// Will return NULL if the <see cref="charIndex"/> is invalid or the character isn't visible.</summary>
  610. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  611. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  612. public TweenerCore<Vector3, Vector3, VectorOptions> DOScaleChar(int charIndex, Vector3 endValue, float duration)
  613. {
  614. if (!ValidateChar(charIndex)) return null;
  615. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => _charTransforms[charIndex].scale, x => {
  616. CharTransform charT = _charTransforms[charIndex];
  617. charT.UpdateGeometry(target, charT.offset, charT.rotation, x, _cachedMeshInfos);
  618. _charTransforms[charIndex] = charT;
  619. }, endValue, duration);
  620. return t;
  621. }
  622. /// <summary>Punches a character's offset towards the given direction and then back to the starting one
  623. /// as if it was connected to the starting position via an elastic.</summary>
  624. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  625. /// <param name="punch">The punch strength</param>
  626. /// <param name="duration">The duration of the tween</param>
  627. /// <param name="vibrato">Indicates how much will the punch vibrate per second</param>
  628. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting size when bouncing backwards.
  629. /// 1 creates a full oscillation between the punch offset and the opposite offset,
  630. /// while 0 oscillates only between the punch offset and the start offset</param>
  631. public Tweener DOPunchCharOffset(int charIndex, Vector3 punch, float duration, int vibrato = 10, float elasticity = 1)
  632. {
  633. if (!ValidateChar(charIndex)) return null;
  634. if (duration <= 0) {
  635. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  636. return null;
  637. }
  638. return DOTween.Punch(() => _charTransforms[charIndex].offset, x => {
  639. CharTransform charT = _charTransforms[charIndex];
  640. charT.UpdateGeometry(target, x, charT.rotation, charT.scale, _cachedMeshInfos);
  641. _charTransforms[charIndex] = charT;
  642. }, punch, duration, vibrato, elasticity);
  643. }
  644. /// <summary>Punches a character's rotation towards the given direction and then back to the starting one
  645. /// as if it was connected to the starting position via an elastic.</summary>
  646. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  647. /// <param name="punch">The punch strength</param>
  648. /// <param name="duration">The duration of the tween</param>
  649. /// <param name="vibrato">Indicates how much will the punch vibrate per second</param>
  650. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting size when bouncing backwards.
  651. /// 1 creates a full oscillation between the punch rotation and the opposite rotation,
  652. /// while 0 oscillates only between the punch rotation and the start rotation</param>
  653. public Tweener DOPunchCharRotation(int charIndex, Vector3 punch, float duration, int vibrato = 10, float elasticity = 1)
  654. {
  655. if (!ValidateChar(charIndex)) return null;
  656. if (duration <= 0) {
  657. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  658. return null;
  659. }
  660. return DOTween.Punch(() => _charTransforms[charIndex].rotation.eulerAngles, x => {
  661. CharTransform charT = _charTransforms[charIndex];
  662. charT.UpdateGeometry(target, charT.offset, Quaternion.Euler(x), charT.scale, _cachedMeshInfos);
  663. _charTransforms[charIndex] = charT;
  664. }, punch, duration, vibrato, elasticity);
  665. }
  666. /// <summary>Punches a character's scale towards the given direction and then back to the starting one
  667. /// as if it was connected to the starting position via an elastic.</summary>
  668. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  669. /// <param name="punch">The punch strength (added to the character's current scale)</param>
  670. /// <param name="duration">The duration of the tween</param>
  671. /// <param name="vibrato">Indicates how much will the punch vibrate per second</param>
  672. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting size when bouncing backwards.
  673. /// 1 creates a full oscillation between the punch scale and the opposite scale,
  674. /// while 0 oscillates only between the punch scale and the start scale</param>
  675. public Tweener DOPunchCharScale(int charIndex, float punch, float duration, int vibrato = 10, float elasticity = 1)
  676. {
  677. return DOPunchCharScale(charIndex, new Vector3(punch, punch, punch), duration, vibrato, elasticity);
  678. }
  679. /// <summary>Punches a character's scale towards the given direction and then back to the starting one
  680. /// as if it was connected to the starting position via an elastic.</summary>
  681. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  682. /// <param name="punch">The punch strength (added to the character's current scale)</param>
  683. /// <param name="duration">The duration of the tween</param>
  684. /// <param name="vibrato">Indicates how much will the punch vibrate per second</param>
  685. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting size when bouncing backwards.
  686. /// 1 creates a full oscillation between the punch scale and the opposite scale,
  687. /// while 0 oscillates only between the punch scale and the start scale</param>
  688. public Tweener DOPunchCharScale(int charIndex, Vector3 punch, float duration, int vibrato = 10, float elasticity = 1)
  689. {
  690. if (!ValidateChar(charIndex)) return null;
  691. if (duration <= 0) {
  692. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  693. return null;
  694. }
  695. return DOTween.Punch(() => _charTransforms[charIndex].scale, x => {
  696. CharTransform charT = _charTransforms[charIndex];
  697. charT.UpdateGeometry(target, charT.offset, charT.rotation, x, _cachedMeshInfos);
  698. _charTransforms[charIndex] = charT;
  699. }, punch, duration, vibrato, elasticity);
  700. }
  701. /// <summary>Shakes a character's offset with the given values.</summary>
  702. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  703. /// <param name="duration">The duration of the tween</param>
  704. /// <param name="strength">The shake strength</param>
  705. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  706. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  707. /// Setting it to 0 will shake along a single direction.</param>
  708. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  709. public Tweener DOShakeCharOffset(int charIndex, float duration, float strength, int vibrato = 10, float randomness = 90, bool fadeOut = true)
  710. {
  711. return DOShakeCharOffset(charIndex, duration, new Vector3(strength, strength, strength), vibrato, randomness, fadeOut);
  712. }
  713. /// <summary>Shakes a character's offset with the given values.</summary>
  714. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  715. /// <param name="duration">The duration of the tween</param>
  716. /// <param name="strength">The shake strength</param>
  717. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  718. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  719. /// Setting it to 0 will shake along a single direction.</param>
  720. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  721. public Tweener DOShakeCharOffset(int charIndex, float duration, Vector3 strength, int vibrato = 10, float randomness = 90, bool fadeOut = true)
  722. {
  723. if (!ValidateChar(charIndex)) return null;
  724. if (duration <= 0) {
  725. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  726. return null;
  727. }
  728. return DOTween.Shake(() => _charTransforms[charIndex].offset, x => {
  729. CharTransform charT = _charTransforms[charIndex];
  730. charT.UpdateGeometry(target, x, charT.rotation, charT.scale, _cachedMeshInfos);
  731. _charTransforms[charIndex] = charT;
  732. }, duration, strength, vibrato, randomness, fadeOut);
  733. }
  734. /// <summary>Shakes a character's rotation with the given values.</summary>
  735. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  736. /// <param name="duration">The duration of the tween</param>
  737. /// <param name="strength">The shake strength</param>
  738. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  739. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  740. /// Setting it to 0 will shake along a single direction.</param>
  741. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  742. public Tweener DOShakeCharRotation(int charIndex, float duration, Vector3 strength, int vibrato = 10, float randomness = 90, bool fadeOut = true)
  743. {
  744. if (!ValidateChar(charIndex)) return null;
  745. if (duration <= 0) {
  746. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  747. return null;
  748. }
  749. return DOTween.Shake(() => _charTransforms[charIndex].rotation.eulerAngles, x => {
  750. CharTransform charT = _charTransforms[charIndex];
  751. charT.UpdateGeometry(target, charT.offset, Quaternion.Euler(x), charT.scale, _cachedMeshInfos);
  752. _charTransforms[charIndex] = charT;
  753. }, duration, strength, vibrato, randomness, fadeOut);
  754. }
  755. /// <summary>Shakes a character's scale with the given values.</summary>
  756. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  757. /// <param name="duration">The duration of the tween</param>
  758. /// <param name="strength">The shake strength</param>
  759. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  760. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  761. /// Setting it to 0 will shake along a single direction.</param>
  762. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  763. public Tweener DOShakeCharScale(int charIndex, float duration, float strength, int vibrato = 10, float randomness = 90, bool fadeOut = true)
  764. {
  765. return DOShakeCharScale(charIndex, duration, new Vector3(strength, strength, strength), vibrato, randomness, fadeOut);
  766. }
  767. /// <summary>Shakes a character's scale with the given values.</summary>
  768. /// <param name="charIndex">The index of the character to tween (will throw an error if it doesn't exist)</param>
  769. /// <param name="duration">The duration of the tween</param>
  770. /// <param name="strength">The shake strength</param>
  771. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  772. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  773. /// Setting it to 0 will shake along a single direction.</param>
  774. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  775. public Tweener DOShakeCharScale(int charIndex, float duration, Vector3 strength, int vibrato = 10, float randomness = 90, bool fadeOut = true)
  776. {
  777. if (!ValidateChar(charIndex)) return null;
  778. if (duration <= 0) {
  779. if (Debugger.logPriority > 0) Debug.LogWarning("Duration can't be 0, returning NULL without creating a tween");
  780. return null;
  781. }
  782. return DOTween.Shake(() => _charTransforms[charIndex].scale, x => {
  783. CharTransform charT = _charTransforms[charIndex];
  784. charT.UpdateGeometry(target, charT.offset, charT.rotation, x, _cachedMeshInfos);
  785. _charTransforms[charIndex] = charT;
  786. }, duration, strength, vibrato, randomness, fadeOut);
  787. }
  788. #endregion
  789. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  790. struct CharVertices
  791. {
  792. public Vector3 bottomLeft, topLeft, topRight, bottomRight;
  793. public CharVertices(Vector3 bottomLeft, Vector3 topLeft, Vector3 topRight, Vector3 bottomRight)
  794. {
  795. this.bottomLeft = bottomLeft;
  796. this.topLeft = topLeft;
  797. this.topRight = topRight;
  798. this.bottomRight = bottomRight;
  799. }
  800. }
  801. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  802. // Vertices of each character are:
  803. // 0 : bottom left, 1 : top left, 2 : top right, 3 : bottom right
  804. struct CharTransform
  805. {
  806. public int charIndex;
  807. public bool isVisible { get; private set; } // FALSE both if it's invisible or if it's a space
  808. public Vector3 offset;
  809. public Quaternion rotation;
  810. public Vector3 scale;
  811. Vector3 _topLeftShift, _topRightShift, _bottomLeftShift, _bottomRightShift;
  812. Vector3 _charMidBaselineOffset;
  813. int _matIndex, _firstVertexIndex;
  814. TMP_MeshInfo _meshInfo;
  815. public CharTransform(int charIndex, TMP_TextInfo textInfo, TMP_MeshInfo[] cachedMeshInfos) : this()
  816. {
  817. this.charIndex = charIndex;
  818. offset = Vector3.zero;
  819. rotation = Quaternion.identity;
  820. scale = Vector3.one;
  821. Refresh(textInfo, cachedMeshInfos);
  822. }
  823. public void Refresh(TMP_TextInfo textInfo, TMP_MeshInfo[] cachedMeshInfos)
  824. {
  825. TMP_CharacterInfo charInfo = textInfo.characterInfo[charIndex];
  826. bool isSpaceChar = charInfo.character == ' ';
  827. isVisible = charInfo.isVisible && !isSpaceChar;
  828. _matIndex = charInfo.materialReferenceIndex;
  829. _firstVertexIndex = charInfo.vertexIndex;
  830. _meshInfo = textInfo.meshInfo[_matIndex];
  831. Vector3[] cachedVertices = cachedMeshInfos[_matIndex].vertices;
  832. _charMidBaselineOffset = isSpaceChar
  833. ? Vector3.zero
  834. : (cachedVertices[_firstVertexIndex] + cachedVertices[_firstVertexIndex + 2]) * 0.5f;
  835. }
  836. public void ResetAll(TMP_Text target, TMP_MeshInfo[] meshInfos, TMP_MeshInfo[] cachedMeshInfos)
  837. {
  838. ResetGeometry(target, cachedMeshInfos);
  839. ResetColors(target, meshInfos);
  840. }
  841. public void ResetTransformationData()
  842. {
  843. offset = Vector3.zero;
  844. rotation = Quaternion.identity;
  845. scale = Vector3.one;
  846. _topLeftShift = _topRightShift = _bottomLeftShift = _bottomRightShift = Vector3.zero;
  847. }
  848. public void ResetGeometry(TMP_Text target, TMP_MeshInfo[] cachedMeshInfos)
  849. {
  850. ResetTransformationData();
  851. Vector3[] destinationVertices = _meshInfo.vertices;
  852. Vector3[] cachedVertices = cachedMeshInfos[_matIndex].vertices;
  853. destinationVertices[_firstVertexIndex + 0] = cachedVertices[_firstVertexIndex + 0];
  854. destinationVertices[_firstVertexIndex + 1] = cachedVertices[_firstVertexIndex + 1];
  855. destinationVertices[_firstVertexIndex + 2] = cachedVertices[_firstVertexIndex + 2];
  856. destinationVertices[_firstVertexIndex + 3] = cachedVertices[_firstVertexIndex + 3];
  857. _meshInfo.mesh.vertices = _meshInfo.vertices;
  858. target.UpdateGeometry(_meshInfo.mesh, _matIndex);
  859. }
  860. public void ResetColors(TMP_Text target, TMP_MeshInfo[] meshInfos)
  861. {
  862. Color color = target.color;
  863. Color32[] vertexCols = meshInfos[_matIndex].colors32;
  864. vertexCols[_firstVertexIndex] = color;
  865. vertexCols[_firstVertexIndex + 1] = color;
  866. vertexCols[_firstVertexIndex + 2] = color;
  867. vertexCols[_firstVertexIndex + 3] = color;
  868. target.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
  869. }
  870. public Color32 GetColor(TMP_MeshInfo[] meshInfos)
  871. {
  872. return meshInfos[_matIndex].colors32[_firstVertexIndex];
  873. }
  874. public CharVertices GetVertices()
  875. {
  876. return new CharVertices(
  877. _meshInfo.vertices[_firstVertexIndex], _meshInfo.vertices[_firstVertexIndex + 1],
  878. _meshInfo.vertices[_firstVertexIndex + 2], _meshInfo.vertices[_firstVertexIndex + 3]
  879. );
  880. }
  881. public void UpdateAlpha(TMP_Text target, Color alphaColor, TMP_MeshInfo[] meshInfos, bool apply = true)
  882. {
  883. byte alphaByte = (byte)(alphaColor.a * 255);
  884. Color32[] vertexCols = meshInfos[_matIndex].colors32;
  885. vertexCols[_firstVertexIndex].a = alphaByte;
  886. vertexCols[_firstVertexIndex + 1].a = alphaByte;
  887. vertexCols[_firstVertexIndex + 2].a = alphaByte;
  888. vertexCols[_firstVertexIndex + 3].a = alphaByte;
  889. if (apply) target.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
  890. }
  891. public void UpdateColor(TMP_Text target, Color32 color, TMP_MeshInfo[] meshInfos, bool apply = true)
  892. {
  893. Color32[] vertexCols = meshInfos[_matIndex].colors32;
  894. vertexCols[_firstVertexIndex] = color;
  895. vertexCols[_firstVertexIndex + 1] = color;
  896. vertexCols[_firstVertexIndex + 2] = color;
  897. vertexCols[_firstVertexIndex + 3] = color;
  898. if (apply) target.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
  899. }
  900. public void UpdateGeometry(TMP_Text target, Vector3 offset, Quaternion rotation, Vector3 scale, TMP_MeshInfo[] cachedMeshInfos, bool apply = true)
  901. {
  902. this.offset = offset;
  903. this.rotation = rotation;
  904. this.scale = scale;
  905. if (!apply) return;
  906. Vector3[] destinationVertices = _meshInfo.vertices;
  907. Vector3[] cachedVertices = cachedMeshInfos[_matIndex].vertices;
  908. destinationVertices[_firstVertexIndex] = cachedVertices[_firstVertexIndex + 0] - _charMidBaselineOffset;
  909. destinationVertices[_firstVertexIndex + 1] = cachedVertices[_firstVertexIndex + 1] - _charMidBaselineOffset;
  910. destinationVertices[_firstVertexIndex + 2] = cachedVertices[_firstVertexIndex + 2] - _charMidBaselineOffset;
  911. destinationVertices[_firstVertexIndex + 3] = cachedVertices[_firstVertexIndex + 3] - _charMidBaselineOffset;
  912. Matrix4x4 matrix = Matrix4x4.TRS(this.offset, this.rotation, this.scale);
  913. destinationVertices[_firstVertexIndex]
  914. = matrix.MultiplyPoint3x4(destinationVertices[_firstVertexIndex + 0]) + _charMidBaselineOffset + _bottomLeftShift;
  915. destinationVertices[_firstVertexIndex + 1]
  916. = matrix.MultiplyPoint3x4(destinationVertices[_firstVertexIndex + 1]) + _charMidBaselineOffset + _topLeftShift;
  917. destinationVertices[_firstVertexIndex + 2]
  918. = matrix.MultiplyPoint3x4(destinationVertices[_firstVertexIndex + 2]) + _charMidBaselineOffset + _topRightShift;
  919. destinationVertices[_firstVertexIndex + 3]
  920. = matrix.MultiplyPoint3x4(destinationVertices[_firstVertexIndex + 3]) + _charMidBaselineOffset + _bottomRightShift;
  921. _meshInfo.mesh.vertices = _meshInfo.vertices;
  922. target.UpdateGeometry(_meshInfo.mesh, _matIndex);
  923. }
  924. public void ShiftVertices(TMP_Text target, Vector3 topLeftShift, Vector3 topRightShift, Vector3 bottomLeftShift, Vector3 bottomRightShift)
  925. {
  926. _topLeftShift += topLeftShift;
  927. _topRightShift += topRightShift;
  928. _bottomLeftShift += bottomLeftShift;
  929. _bottomRightShift += bottomRightShift;
  930. Vector3[] destinationVertices = _meshInfo.vertices;
  931. destinationVertices[_firstVertexIndex] = destinationVertices[_firstVertexIndex] + _bottomLeftShift;
  932. destinationVertices[_firstVertexIndex + 1] = destinationVertices[_firstVertexIndex + 1] + _topLeftShift;
  933. destinationVertices[_firstVertexIndex + 2] = destinationVertices[_firstVertexIndex + 2] + _topRightShift;
  934. destinationVertices[_firstVertexIndex + 3] = destinationVertices[_firstVertexIndex + 3] + _bottomRightShift;
  935. _meshInfo.mesh.vertices = _meshInfo.vertices;
  936. target.UpdateGeometry(_meshInfo.mesh, _matIndex);
  937. }
  938. public void ResetVerticesShift(TMP_Text target)
  939. {
  940. Vector3[] destinationVertices = _meshInfo.vertices;
  941. destinationVertices[_firstVertexIndex] = destinationVertices[_firstVertexIndex] - _bottomLeftShift;
  942. destinationVertices[_firstVertexIndex + 1] = destinationVertices[_firstVertexIndex + 1] - _topLeftShift;
  943. destinationVertices[_firstVertexIndex + 2] = destinationVertices[_firstVertexIndex + 2] - _topRightShift;
  944. destinationVertices[_firstVertexIndex + 3] = destinationVertices[_firstVertexIndex + 3] - _bottomRightShift;
  945. _meshInfo.mesh.vertices = _meshInfo.vertices;
  946. target.UpdateGeometry(_meshInfo.mesh, _matIndex);
  947. _topLeftShift = _topRightShift = _bottomLeftShift = _bottomRightShift = Vector3.zero;
  948. }
  949. }
  950. }
  951. #endregion
  952. }
  953. #endif