UILocalizeTexture.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class UILocalizeTexture : MonoBehaviour
  7. {
  8. public string lgKey;
  9. public string key;
  10. private Image mImage;
  11. private long loadSeq;
  12. string oldname;
  13. LocalizedSpriteCfg spriteCfg;
  14. private void Awake()
  15. {
  16. oldname = "";
  17. loadSeq = 0;
  18. mImage = GetComponent<Image>();
  19. EventMgr.AddEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE,OnLanguageChange);
  20. }
  21. private void OnLanguageChange(CoreEvent<int> e)
  22. {
  23. Refresh();
  24. }
  25. // Start is called before the first frame update
  26. private void Start()
  27. {
  28. Refresh();
  29. }
  30. // Update is called once per frame
  31. private void Update()
  32. {
  33. }
  34. private void OnDestroy()
  35. {
  36. Dispose();
  37. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
  38. }
  39. public void Refresh()
  40. {
  41. if (string.IsNullOrEmpty(key) || mImage == null)
  42. return;
  43. if (string.IsNullOrEmpty(lgKey))
  44. {
  45. spriteCfg = LocalizedTextureCfgMgr.Instance.GetLocalizedSpriteCfg(key);
  46. }
  47. else
  48. {
  49. spriteCfg = LocalizedTextureCfgMgr.Instance.GetCfgByLgkey(lgKey,key);
  50. }
  51. if (spriteCfg == null || oldname == spriteCfg.Language)
  52. {
  53. return;
  54. }
  55. oldname = spriteCfg.Language;
  56. loadSeq = ResourceMgr.Instance.LoadAsset<Sprite>(OnLoadEnd, spriteCfg.AssetPath, spriteCfg.Language);
  57. }
  58. private void OnLoadEnd(Sprite sp, string assetPath, params string[] assetNames)
  59. {
  60. //mImage.sprite.name;
  61. mImage.sprite = sp;
  62. if (spriteCfg.GetSetNativeSize())
  63. {
  64. mImage.SetNativeSize();
  65. }
  66. SerizedOtherCfg();
  67. }
  68. public void SetKey(string _key)
  69. {
  70. key = _key;
  71. Refresh();
  72. }
  73. public void Dispose()
  74. {
  75. if (loadSeq > 0)
  76. {
  77. ResourceMgr.Instance.UnloadAssetBySeqId(loadSeq);
  78. loadSeq = 0;
  79. }
  80. }
  81. private void SerizedOtherCfg()
  82. {
  83. if (string.IsNullOrEmpty(spriteCfg.OtherSetting))
  84. {
  85. return;
  86. }
  87. string[] datas_1 = spriteCfg.OtherSetting.Split(";",StringSplitOptions.RemoveEmptyEntries);
  88. for (int i = 0; i < datas_1.Length; i++)
  89. {
  90. string[] datas_2 = datas_1[i].Split("_", StringSplitOptions.RemoveEmptyEntries);
  91. if (datas_2.Length < 2)
  92. {
  93. continue;
  94. }
  95. int type = -1;
  96. if (!int.TryParse(datas_2[0],out type))
  97. {
  98. continue;
  99. }
  100. string[] datas_3 = datas_2[1].Split(":", StringSplitOptions.RemoveEmptyEntries);
  101. if (datas_3.Length < 2)
  102. {
  103. continue;
  104. }
  105. int x;
  106. int y;
  107. int z;
  108. if (!int.TryParse(datas_3[0], out x))
  109. {
  110. continue;
  111. }
  112. if (!int.TryParse(datas_3[1], out y))
  113. {
  114. continue;
  115. }
  116. if (datas_3.Length < 3)
  117. {
  118. SetImage(type, new Vector3(x,y));
  119. }
  120. else if ( int.TryParse(datas_3[2], out z))
  121. {
  122. SetImage(type, new Vector3(x, y, z));
  123. }
  124. }
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. /// <param name="type">1 local pos ; 2 rotation ; 3 scale 4 size </param>
  130. /// <param name="data"></param>
  131. private void SetImage(int type, Vector3 data)
  132. {
  133. RectTransform rectTransform = transform as RectTransform;
  134. if (rectTransform == null )
  135. {
  136. return;
  137. }
  138. switch (type)
  139. {
  140. case 1:
  141. {
  142. Vector3 lp = rectTransform.localPosition;
  143. lp.x = data.x;
  144. lp.y = data.y;
  145. rectTransform.localPosition = lp;
  146. }
  147. break;
  148. case 2:
  149. {
  150. rectTransform.localRotation = Quaternion.Euler(data);
  151. }
  152. break;
  153. case 3:
  154. {
  155. rectTransform.localScale = data;
  156. }
  157. break;
  158. case 4:
  159. {
  160. rectTransform.sizeDelta = new Vector2(data.x, data.y);
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. }