| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class UILocalizeTexture : MonoBehaviour
- {
- public string lgKey;
- public string key;
- private Image mImage;
- private long loadSeq;
- string oldname;
- LocalizedSpriteCfg spriteCfg;
- private void Awake()
- {
- oldname = "";
- loadSeq = 0;
- mImage = GetComponent<Image>();
- EventMgr.AddEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE,OnLanguageChange);
- }
- private void OnLanguageChange(CoreEvent<int> e)
- {
- Refresh();
- }
- // Start is called before the first frame update
- private void Start()
- {
- Refresh();
- }
- // Update is called once per frame
- private void Update()
- {
- }
- private void OnDestroy()
- {
- Dispose();
- EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
- }
- public void Refresh()
- {
- if (string.IsNullOrEmpty(key) || mImage == null)
- return;
- if (string.IsNullOrEmpty(lgKey))
- {
- spriteCfg = LocalizedTextureCfgMgr.Instance.GetLocalizedSpriteCfg(key);
- }
- else
- {
- spriteCfg = LocalizedTextureCfgMgr.Instance.GetCfgByLgkey(lgKey,key);
- }
- if (spriteCfg == null || oldname == spriteCfg.Language)
- {
- return;
- }
- oldname = spriteCfg.Language;
- loadSeq = ResourceMgr.Instance.LoadAsset<Sprite>(OnLoadEnd, spriteCfg.AssetPath, spriteCfg.Language);
- }
- private void OnLoadEnd(Sprite sp, string assetPath, params string[] assetNames)
- {
- //mImage.sprite.name;
- mImage.sprite = sp;
- if (spriteCfg.GetSetNativeSize())
- {
- mImage.SetNativeSize();
- }
- SerizedOtherCfg();
- }
- public void SetKey(string _key)
- {
- key = _key;
- Refresh();
- }
- public void Dispose()
- {
- if (loadSeq > 0)
- {
- ResourceMgr.Instance.UnloadAssetBySeqId(loadSeq);
- loadSeq = 0;
- }
- }
- private void SerizedOtherCfg()
- {
- if (string.IsNullOrEmpty(spriteCfg.OtherSetting))
- {
- return;
- }
- string[] datas_1 = spriteCfg.OtherSetting.Split(";",StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < datas_1.Length; i++)
- {
- string[] datas_2 = datas_1[i].Split("_", StringSplitOptions.RemoveEmptyEntries);
- if (datas_2.Length < 2)
- {
- continue;
- }
- int type = -1;
- if (!int.TryParse(datas_2[0],out type))
- {
- continue;
- }
- string[] datas_3 = datas_2[1].Split(":", StringSplitOptions.RemoveEmptyEntries);
- if (datas_3.Length < 2)
- {
- continue;
- }
- int x;
- int y;
- int z;
- if (!int.TryParse(datas_3[0], out x))
- {
- continue;
- }
- if (!int.TryParse(datas_3[1], out y))
- {
- continue;
- }
- if (datas_3.Length < 3)
- {
- SetImage(type, new Vector3(x,y));
- }
- else if ( int.TryParse(datas_3[2], out z))
- {
- SetImage(type, new Vector3(x, y, z));
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="type">1 local pos ; 2 rotation ; 3 scale 4 size </param>
- /// <param name="data"></param>
- private void SetImage(int type, Vector3 data)
- {
- RectTransform rectTransform = transform as RectTransform;
- if (rectTransform == null )
- {
- return;
- }
- switch (type)
- {
- case 1:
- {
- Vector3 lp = rectTransform.localPosition;
- lp.x = data.x;
- lp.y = data.y;
- rectTransform.localPosition = lp;
- }
- break;
- case 2:
- {
- rectTransform.localRotation = Quaternion.Euler(data);
- }
- break;
- case 3:
- {
- rectTransform.localScale = data;
- }
- break;
- case 4:
- {
- rectTransform.sizeDelta = new Vector2(data.x, data.y);
- }
- break;
- default:
- break;
- }
- }
- }
|