UILocalizeTexture.cs 4.3 KB

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