NGUILabelLocalizeSupport.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using I2.Loc;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. [DisallowMultipleComponent]
  6. [RequireComponent(typeof(Localize))]
  7. [RequireComponent(typeof(UILabel))]
  8. public class NGUILabelLocalizeSupport : MonoBehaviour
  9. {
  10. public NGUILabelLocalizeSupport()
  11. {
  12. NGUILabelLocalizeSupport.OverRideProperty overRideProperty = default(NGUILabelLocalizeSupport.OverRideProperty);
  13. overRideProperty.overFlow.enabled = true;
  14. overRideProperty.overFlow.value = UILabel.Overflow.ShrinkContent;
  15. overRideProperty.SpacingX.enabled = true;
  16. overRideProperty.SpacingY.enabled = true;
  17. this.overRidePropertys = overRideProperty;
  18. base..ctor();
  19. }
  20. private void Awake()
  21. {
  22. if (this.callAwake)
  23. {
  24. return;
  25. }
  26. this.callAwake = true;
  27. this.localize = base.GetComponent<Localize>();
  28. this.label = base.GetComponent<UILabel>();
  29. if (this.localize != null && this.label != null)
  30. {
  31. this.localize.LocalizeEvent.AddListener(new UnityAction(this.OnLocalize));
  32. this.holdOverflow = this.label.overflowMethod;
  33. this.holdAlignment = this.label.alignment;
  34. this.holdFontSize = this.label.fontSize;
  35. if (this.label.useFloatSpacing)
  36. {
  37. this.holdSpacingX = this.label.floatSpacingX;
  38. this.holdSpacingY = this.label.floatSpacingY;
  39. }
  40. else
  41. {
  42. this.holdSpacingX = (float)this.label.spacingX;
  43. this.holdSpacingY = (float)this.label.spacingY;
  44. }
  45. this.holdWidgetSizeX = this.label.width;
  46. this.holdWidgetSizeY = this.label.height;
  47. this.holdMaxLines = this.label.maxLineCount;
  48. this.updateReq = true;
  49. this.OnLocalize();
  50. }
  51. }
  52. public void OnLocalize()
  53. {
  54. if (!this.callAwake)
  55. {
  56. this.Awake();
  57. }
  58. if (!this.updateReq)
  59. {
  60. return;
  61. }
  62. if (LocalizationManager.CurrentLanguageCode == "ja")
  63. {
  64. if (this.holdOverflow != UILabel.Overflow.ResizeFreely)
  65. {
  66. this.label.width = this.holdWidgetSizeX;
  67. }
  68. if (this.holdOverflow != UILabel.Overflow.ResizeHeight)
  69. {
  70. this.label.height = this.holdWidgetSizeY;
  71. }
  72. this.label.fontSize = this.holdFontSize;
  73. if (this.label.useFloatSpacing)
  74. {
  75. this.label.floatSpacingX = this.holdSpacingX;
  76. this.label.floatSpacingY = this.holdSpacingY;
  77. }
  78. else
  79. {
  80. this.label.spacingX = (int)this.holdSpacingX;
  81. this.label.spacingY = (int)this.holdSpacingY;
  82. }
  83. this.label.maxLineCount = this.holdMaxLines;
  84. this.label.alignment = this.holdAlignment;
  85. this.label.overflowMethod = this.holdOverflow;
  86. }
  87. else
  88. {
  89. this.Apply(this.overRidePropertys);
  90. if (this.languageOverRidePropertys != null)
  91. {
  92. foreach (NGUILabelLocalizeSupport.LanguageOverRideProperty languageOverRideProperty in this.languageOverRidePropertys)
  93. {
  94. if (languageOverRideProperty.LanguageCode == LocalizationManager.CurrentLanguageCode)
  95. {
  96. this.Apply(languageOverRideProperty.Property);
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. public void Apply(NGUILabelLocalizeSupport.OverRideProperty property)
  104. {
  105. UILabel.Overflow overflow = (!property.overFlow.enabled) ? this.holdOverflow : property.overFlow.value;
  106. if (overflow != UILabel.Overflow.ResizeFreely)
  107. {
  108. this.label.width = ((!property.widgetSizeX.enabled) ? this.holdWidgetSizeX : property.widgetSizeX.value);
  109. }
  110. if (overflow != UILabel.Overflow.ResizeHeight)
  111. {
  112. this.label.height = ((!property.widgetSizeY.enabled) ? this.holdWidgetSizeY : property.widgetSizeY.value);
  113. }
  114. this.label.fontSize = ((!property.fontSize.enabled) ? this.holdFontSize : property.fontSize.value);
  115. if (property.SpacingX.enabled)
  116. {
  117. if (this.label.useFloatSpacing)
  118. {
  119. this.label.floatSpacingX = (float)property.SpacingX.value;
  120. }
  121. else
  122. {
  123. this.label.spacingX = property.SpacingX.value;
  124. }
  125. }
  126. else if (this.label.useFloatSpacing)
  127. {
  128. this.label.floatSpacingX = this.holdSpacingX;
  129. }
  130. else
  131. {
  132. this.label.spacingX = (int)this.holdSpacingX;
  133. }
  134. if (property.SpacingY.enabled)
  135. {
  136. if (this.label.useFloatSpacing)
  137. {
  138. this.label.floatSpacingY = (float)property.SpacingY.value;
  139. }
  140. else
  141. {
  142. this.label.spacingY = property.SpacingY.value;
  143. }
  144. }
  145. else if (this.label.useFloatSpacing)
  146. {
  147. this.label.floatSpacingY = this.holdSpacingY;
  148. }
  149. else
  150. {
  151. this.label.spacingY = (int)this.holdSpacingY;
  152. }
  153. this.label.maxLineCount = ((!property.maxLines.enabled) ? this.holdMaxLines : property.maxLines.value);
  154. this.label.alignment = ((!property.alignment.enabled) ? this.holdAlignment : property.alignment.value);
  155. this.label.overflowMethod = overflow;
  156. }
  157. [SerializeField]
  158. public NGUILabelLocalizeSupport.OverRideProperty overRidePropertys;
  159. [SerializeField]
  160. public NGUILabelLocalizeSupport.LanguageOverRideProperty[] languageOverRidePropertys;
  161. private UILabel.Overflow holdOverflow;
  162. private NGUIText.Alignment holdAlignment;
  163. private int holdFontSize;
  164. private float holdSpacingX;
  165. private float holdSpacingY;
  166. private int holdWidgetSizeX;
  167. private int holdWidgetSizeY;
  168. private int holdMaxLines;
  169. private bool updateReq;
  170. private bool callAwake;
  171. private Localize localize;
  172. private UILabel label;
  173. [Serializable]
  174. public struct OverRideProperty
  175. {
  176. public NGUILabelLocalizeSupport.OverRideProperty.OverflowValue overFlow;
  177. public NGUILabelLocalizeSupport.OverRideProperty.AlignmentValue alignment;
  178. public NGUILabelLocalizeSupport.OverRideProperty.IntValue fontSize;
  179. public NGUILabelLocalizeSupport.OverRideProperty.IntValue SpacingX;
  180. public NGUILabelLocalizeSupport.OverRideProperty.IntValue SpacingY;
  181. public NGUILabelLocalizeSupport.OverRideProperty.IntValue widgetSizeX;
  182. public NGUILabelLocalizeSupport.OverRideProperty.IntValue widgetSizeY;
  183. public NGUILabelLocalizeSupport.OverRideProperty.IntValue maxLines;
  184. [Serializable]
  185. public struct IntValue
  186. {
  187. public bool enabled;
  188. public int value;
  189. }
  190. [Serializable]
  191. public struct OverflowValue
  192. {
  193. public bool enabled;
  194. public UILabel.Overflow value;
  195. }
  196. [Serializable]
  197. public struct AlignmentValue
  198. {
  199. public bool enabled;
  200. public NGUIText.Alignment value;
  201. }
  202. }
  203. [Serializable]
  204. public struct LanguageOverRideProperty
  205. {
  206. public string LanguageCode;
  207. public NGUILabelLocalizeSupport.OverRideProperty Property;
  208. }
  209. }