LocalizeTarget_TextMeshPro_Label.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using TMPro;
  3. using UnityEditor;
  4. using UnityEngine;
  5. #if TextMeshPro
  6. namespace I2.Loc
  7. {
  8. #if UNITY_EDITOR
  9. [InitializeOnLoad]
  10. #endif
  11. public class LocalizeTarget_TextMeshPro_Label : LocalizeTarget<TextMeshPro>
  12. {
  13. static LocalizeTarget_TextMeshPro_Label() { AutoRegister(); }
  14. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void AutoRegister() { LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<TextMeshPro, LocalizeTarget_TextMeshPro_Label> { Name = "TextMeshPro Label", Priority = 100 }); }
  15. TextAlignmentOptions mAlignment_RTL = TextAlignmentOptions.Right;
  16. TextAlignmentOptions mAlignment_LTR = TextAlignmentOptions.Left;
  17. bool mAlignmentWasRTL;
  18. bool mInitializeAlignment = true;
  19. public override eTermType GetPrimaryTermType(Localize cmp) { return eTermType.Text; }
  20. public override eTermType GetSecondaryTermType(Localize cmp) { return eTermType.Font; }
  21. public override bool CanUseSecondaryTerm() { return true; }
  22. public override bool AllowMainTermToBeRTL() { return true; }
  23. public override bool AllowSecondTermToBeRTL() { return false; }
  24. public override void GetFinalTerms ( Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
  25. {
  26. primaryTerm = mTarget ? mTarget.text : null;
  27. secondaryTerm = mTarget.font != null ? mTarget.font.name : string.Empty;
  28. }
  29. public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
  30. {
  31. //--[ Localize Font Object ]----------
  32. {
  33. TMP_FontAsset newFont = cmp.GetSecondaryTranslatedObj<TMP_FontAsset>(ref mainTranslation, ref secondaryTranslation);
  34. if (newFont != null)
  35. {
  36. SetFont(mTarget, newFont);
  37. }
  38. else
  39. {
  40. //--[ Localize Font Material ]----------
  41. Material newMat = cmp.GetSecondaryTranslatedObj<Material>(ref mainTranslation, ref secondaryTranslation);
  42. if (newMat != null && mTarget.fontMaterial != newMat)
  43. {
  44. if (!newMat.name.StartsWith(mTarget.font.name, StringComparison.Ordinal))
  45. {
  46. newFont = GetTMPFontFromMaterial(cmp, secondaryTranslation.EndsWith(newMat.name, StringComparison.Ordinal) ? secondaryTranslation : newMat.name);
  47. if (newFont != null)
  48. SetFont(mTarget, newFont);
  49. }
  50. SetMaterial(mTarget, newMat);
  51. }
  52. }
  53. }
  54. if (mInitializeAlignment)
  55. {
  56. mInitializeAlignment = false;
  57. mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  58. InitAlignment_TMPro(mAlignmentWasRTL, mTarget.alignment, out mAlignment_LTR, out mAlignment_RTL);
  59. }
  60. else
  61. {
  62. TextAlignmentOptions alignRTL, alignLTR;
  63. InitAlignment_TMPro(mAlignmentWasRTL, mTarget.alignment, out alignLTR, out alignRTL);
  64. if (mAlignmentWasRTL && mAlignment_RTL != alignRTL ||
  65. !mAlignmentWasRTL && mAlignment_LTR != alignLTR)
  66. {
  67. mAlignment_LTR = alignLTR;
  68. mAlignment_RTL = alignRTL;
  69. }
  70. mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  71. }
  72. if (mainTranslation != null && mTarget.text != mainTranslation)
  73. {
  74. if (cmp.CorrectAlignmentForRTL)
  75. {
  76. mTarget.alignment = LocalizationManager.IsRight2Left ? mAlignment_RTL : mAlignment_LTR;
  77. }
  78. mTarget.isRightToLeftText = LocalizationManager.IsRight2Left;
  79. if (LocalizationManager.IsRight2Left) mainTranslation = I2Utils.ReverseText(mainTranslation);
  80. mTarget.text = mainTranslation;
  81. }
  82. }
  83. #region Tools
  84. internal static TMP_FontAsset GetTMPFontFromMaterial(Localize cmp, string matName)
  85. {
  86. string splitChars = " .\\/-[]()";
  87. for (int i = matName.Length - 1; i > 0;)
  88. {
  89. // Find first valid character
  90. while (i > 0 && splitChars.IndexOf(matName[i]) >= 0)
  91. i--;
  92. if (i <= 0) break;
  93. var fontName = matName.Substring(0, i + 1);
  94. var obj = cmp.GetObject<TMP_FontAsset>(fontName);
  95. if (obj != null)
  96. return obj;
  97. // skip this word
  98. while (i > 0 && splitChars.IndexOf(matName[i]) < 0)
  99. i--;
  100. }
  101. return null;
  102. }
  103. internal static void InitAlignment_TMPro(bool isRTL, TextAlignmentOptions alignment, out TextAlignmentOptions alignLTR, out TextAlignmentOptions alignRTL)
  104. {
  105. alignLTR = alignRTL = alignment;
  106. if (isRTL)
  107. {
  108. switch (alignment)
  109. {
  110. case TextAlignmentOptions.TopRight: alignLTR = TextAlignmentOptions.TopLeft; break;
  111. case TextAlignmentOptions.Right: alignLTR = TextAlignmentOptions.Left; break;
  112. case TextAlignmentOptions.BottomRight: alignLTR = TextAlignmentOptions.BottomLeft; break;
  113. case TextAlignmentOptions.BaselineRight: alignLTR = TextAlignmentOptions.BaselineLeft; break;
  114. case TextAlignmentOptions.MidlineRight: alignLTR = TextAlignmentOptions.MidlineLeft; break;
  115. case TextAlignmentOptions.CaplineRight: alignLTR = TextAlignmentOptions.CaplineLeft; break;
  116. case TextAlignmentOptions.TopLeft: alignLTR = TextAlignmentOptions.TopRight; break;
  117. case TextAlignmentOptions.Left: alignLTR = TextAlignmentOptions.Right; break;
  118. case TextAlignmentOptions.BottomLeft: alignLTR = TextAlignmentOptions.BottomRight; break;
  119. case TextAlignmentOptions.BaselineLeft: alignLTR = TextAlignmentOptions.BaselineRight; break;
  120. case TextAlignmentOptions.MidlineLeft: alignLTR = TextAlignmentOptions.MidlineRight; break;
  121. case TextAlignmentOptions.CaplineLeft: alignLTR = TextAlignmentOptions.CaplineRight; break;
  122. }
  123. }
  124. else
  125. {
  126. switch (alignment)
  127. {
  128. case TextAlignmentOptions.TopRight: alignRTL = TextAlignmentOptions.TopLeft; break;
  129. case TextAlignmentOptions.Right: alignRTL = TextAlignmentOptions.Left; break;
  130. case TextAlignmentOptions.BottomRight: alignRTL = TextAlignmentOptions.BottomLeft; break;
  131. case TextAlignmentOptions.BaselineRight: alignRTL = TextAlignmentOptions.BaselineLeft; break;
  132. case TextAlignmentOptions.MidlineRight: alignRTL = TextAlignmentOptions.MidlineLeft; break;
  133. case TextAlignmentOptions.CaplineRight: alignRTL = TextAlignmentOptions.CaplineLeft; break;
  134. case TextAlignmentOptions.TopLeft: alignRTL = TextAlignmentOptions.TopRight; break;
  135. case TextAlignmentOptions.Left: alignRTL = TextAlignmentOptions.Right; break;
  136. case TextAlignmentOptions.BottomLeft: alignRTL = TextAlignmentOptions.BottomRight; break;
  137. case TextAlignmentOptions.BaselineLeft: alignRTL = TextAlignmentOptions.BaselineRight; break;
  138. case TextAlignmentOptions.MidlineLeft: alignRTL = TextAlignmentOptions.MidlineRight; break;
  139. case TextAlignmentOptions.CaplineLeft: alignRTL = TextAlignmentOptions.CaplineRight; break;
  140. }
  141. }
  142. }
  143. internal static void SetFont(TMP_Text label, TMP_FontAsset newFont)
  144. {
  145. if (label.font != newFont)
  146. {
  147. label.font = newFont;
  148. }
  149. if (label.linkedTextComponent != null)
  150. {
  151. SetFont(label.linkedTextComponent, newFont);
  152. }
  153. }
  154. internal static void SetMaterial(TMP_Text label, Material newMat)
  155. {
  156. if (label.fontSharedMaterial != newMat)
  157. {
  158. label.fontSharedMaterial = newMat;
  159. }
  160. if (label.linkedTextComponent != null)
  161. {
  162. SetMaterial(label.linkedTextComponent, newMat);
  163. }
  164. }
  165. #endregion
  166. }
  167. }
  168. #endif