LocalizeTarget_UnityUI_Text.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace I2.Loc
  5. {
  6. public class LocalizeTarget_UnityUI_Text : LocalizeTarget<Text>
  7. {
  8. static LocalizeTarget_UnityUI_Text()
  9. {
  10. LocalizeTarget_UnityUI_Text.AutoRegister();
  11. }
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  13. private static void AutoRegister()
  14. {
  15. LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Text, LocalizeTarget_UnityUI_Text>
  16. {
  17. Name = "Text",
  18. Priority = 100
  19. });
  20. }
  21. public override eTermType GetPrimaryTermType(Localize cmp)
  22. {
  23. return eTermType.Text;
  24. }
  25. public override eTermType GetSecondaryTermType(Localize cmp)
  26. {
  27. return eTermType.Font;
  28. }
  29. public override bool CanUseSecondaryTerm()
  30. {
  31. return true;
  32. }
  33. public override bool AllowMainTermToBeRTL()
  34. {
  35. return true;
  36. }
  37. public override bool AllowSecondTermToBeRTL()
  38. {
  39. return false;
  40. }
  41. public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
  42. {
  43. primaryTerm = ((!this.mTarget) ? null : this.mTarget.text);
  44. secondaryTerm = ((!(this.mTarget.font != null)) ? string.Empty : this.mTarget.font.name);
  45. }
  46. public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
  47. {
  48. Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
  49. if (secondaryTranslatedObj != null && secondaryTranslatedObj != this.mTarget.font)
  50. {
  51. this.mTarget.font = secondaryTranslatedObj;
  52. }
  53. if (this.mInitializeAlignment)
  54. {
  55. this.mInitializeAlignment = false;
  56. this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  57. this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out this.mAlignment_LTR, out this.mAlignment_RTL);
  58. }
  59. else
  60. {
  61. TextAnchor textAnchor;
  62. TextAnchor textAnchor2;
  63. this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out textAnchor, out textAnchor2);
  64. if ((this.mAlignmentWasRTL && this.mAlignment_RTL != textAnchor2) || (!this.mAlignmentWasRTL && this.mAlignment_LTR != textAnchor))
  65. {
  66. this.mAlignment_LTR = textAnchor;
  67. this.mAlignment_RTL = textAnchor2;
  68. }
  69. this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  70. }
  71. if (mainTranslation != null && this.mTarget.text != mainTranslation)
  72. {
  73. if (cmp.CorrectAlignmentForRTL)
  74. {
  75. this.mTarget.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
  76. }
  77. this.mTarget.text = mainTranslation;
  78. this.mTarget.SetVerticesDirty();
  79. }
  80. }
  81. private void InitAlignment(bool isRTL, TextAnchor alignment, out TextAnchor alignLTR, out TextAnchor alignRTL)
  82. {
  83. alignRTL = alignment;
  84. alignLTR = alignment;
  85. if (isRTL)
  86. {
  87. switch (alignment)
  88. {
  89. case TextAnchor.UpperLeft:
  90. alignLTR = TextAnchor.UpperRight;
  91. break;
  92. case TextAnchor.UpperRight:
  93. alignLTR = TextAnchor.UpperLeft;
  94. break;
  95. case TextAnchor.MiddleLeft:
  96. alignLTR = TextAnchor.MiddleRight;
  97. break;
  98. case TextAnchor.MiddleRight:
  99. alignLTR = TextAnchor.MiddleLeft;
  100. break;
  101. case TextAnchor.LowerLeft:
  102. alignLTR = TextAnchor.LowerRight;
  103. break;
  104. case TextAnchor.LowerRight:
  105. alignLTR = TextAnchor.LowerLeft;
  106. break;
  107. }
  108. }
  109. else
  110. {
  111. switch (alignment)
  112. {
  113. case TextAnchor.UpperLeft:
  114. alignRTL = TextAnchor.UpperRight;
  115. break;
  116. case TextAnchor.UpperRight:
  117. alignRTL = TextAnchor.UpperLeft;
  118. break;
  119. case TextAnchor.MiddleLeft:
  120. alignRTL = TextAnchor.MiddleRight;
  121. break;
  122. case TextAnchor.MiddleRight:
  123. alignRTL = TextAnchor.MiddleLeft;
  124. break;
  125. case TextAnchor.LowerLeft:
  126. alignRTL = TextAnchor.LowerRight;
  127. break;
  128. case TextAnchor.LowerRight:
  129. alignRTL = TextAnchor.LowerLeft;
  130. break;
  131. }
  132. }
  133. }
  134. private TextAnchor mAlignment_RTL = TextAnchor.UpperRight;
  135. private TextAnchor mAlignment_LTR;
  136. private bool mAlignmentWasRTL;
  137. private bool mInitializeAlignment = true;
  138. }
  139. }