UILocalizeTexture.cs 4.1 KB

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