很多游戏中都有语言设置选项,NGUI插件中自带了国际化脚本,但是灵活性较低,而且目前项目是UGUI,以下是修改后,以便记录。
Localization和NGUI中用法一样,挂在在一个不销毁的游戏物体上,并设置当前语言,及所有语言的陪标
ByteReader数据解析类,不多余解释
-
- using UnityEngine;
- using System.Text;
- using System.Collections.Generic;
-
-
- public class ByteReader
- {
- byte[] mBuffer;
- int mOffset = 0;
-
- public ByteReader(byte[] bytes) { mBuffer = bytes; }
- public ByteReader(TextAsset asset) { mBuffer = asset.bytes; }
-
-
-
-
-
- public bool canRead { get { return (mBuffer != null && mOffset < mBuffer.Length); } }
-
-
-
-
-
- static string ReadLine(byte[] buffer, int start, int count)
- {
- #if UNITY_FLASH
-
- StringBuilder sb = new StringBuilder();
-
- int max = start + count;
-
- for (int i = start; i < max; ++i)
- {
- byte byte0 = buffer[i];
-
- if ((byte0 & 128) == 0)
- {
-
- sb.Append((char)byte0);
- }
- else if ((byte0 & 224) == 192)
- {
-
- if (++i == count) break;
- byte byte1 = buffer[i];
- int ch = (byte0 & 31) << 6;
- ch |= (byte1 & 63);
- sb.Append((char)ch);
- }
- else if ((byte0 & 240) == 224)
- {
-
- if (++i == count) break;
- byte byte1 = buffer[i];
- if (++i == count) break;
- byte byte2 = buffer[i];
-
- if (byte0 == 0xEF && byte1 == 0xBB && byte2 == 0xBF)
- {
-
- }
- else
- {
- int ch = (byte0 & 15) << 12;
- ch |= (byte1 & 63) << 6;
- ch |= (byte2 & 63);
- sb.Append((char)ch);
- }
- }
- else if ((byte0 & 248) == 240)
- {
-
- if (++i == count) break;
- byte byte1 = buffer[i];
- if (++i == count) break;
- byte byte2 = buffer[i];
- if (++i == count) break;
- byte byte3 = buffer[i];
-
- int ch = (byte0 & 7) << 18;
- ch |= (byte1 & 63) << 12;
- ch |= (byte2 & 63) << 6;
- ch |= (byte3 & 63);
- sb.Append((char)ch);
- }
- }
- return sb.ToString();
- #else
- return Encoding.UTF8.GetString(buffer, start, count);
- #endif
- }
-
-
-
-
-
- public string ReadLine()
- {
- int max = mBuffer.Length;
-
-
- while (mOffset < max && mBuffer[mOffset] < 32) ++mOffset;
-
- int end = mOffset;
-
- if (end < max)
- {
- for (;;)
- {
- if (end < max)
- {
- int ch = mBuffer[end++];
- if (ch != ‘\n‘ && ch != ‘\r‘) continue;
- }
- else ++end;
-
- string line = ReadLine(mBuffer, mOffset, end - mOffset - 1);
- mOffset = end;
- return line;
- }
- }
- mOffset = max;
- return null;
- }
-
-
-
-
-
- public Dictionary<int, string> ReadDictionary()
- {
- Dictionary<int, string> dict = new Dictionary<int, string>();
- char[] separator = new char[] { ‘=‘ };
-
- while (canRead)
- {
- string line = ReadLine();
- if (line == null) break;
-
- #if UNITY_FLASH
- string[] split = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
- #else
- string[] split = line.Split(separator, 2, System.StringSplitOptions.RemoveEmptyEntries);
- #endif
-
- if (split.Length == 2)
- {
- int key = int.Parse(split[0].Trim());
- string val = split[1].Trim();
- dict[key] = val;
- }
- }
- return dict;
- }
- }
将UILocalize脚本挂在在Text上或者Image上,就是需要多语言切换的UI上
-
-
- using UnityEngine;
- using UnityEngine.UI;
-
- public class UILocalize : MonoBehaviour
- {
-
-
-
- public string Atlas;
- public string key;
-
- string mLanguage;
- bool mStarted = false;
-
-
-
-
-
- void OnLocalize(Localization loc) { if (mLanguage != loc.currentLanguage) Localize(); }
-
-
-
-
-
- void OnEnable() { if (mStarted && Localization.instance != null) Localize(); }
-
-
-
-
-
- void Start()
- {
- mStarted = true;
- if (Localization.instance != null) Localize();
- }
-
-
-
-
-
- public void Localize(string key = null)
- {
- if (key == null)
- key = this.key;
- Localization loc = Localization.instance;
- MaskableGraphic w = GetComponent<MaskableGraphic>();
- Text lbl = w as Text;
- Image sp = w as Image;
-
-
- if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null)
- key = lbl.text;
-
-
- string val = string.IsNullOrEmpty(key) ? w.name : loc.Get(int.Parse(key));
-
- if (val != null)
- {
- if (lbl != null)
- {
- lbl.text = val;
- }
- else if (sp != null)
- {
- if (Atlas == null)
- return;
- sp.sprite = DoRo.Manager.LoadManager.Instance.LoadSprite(Atlas, val);
- }
- }
- mLanguage = loc.currentLanguage;
- }
- }
Unity 国际化 多语言设置
原文:http://www.cnblogs.com/android-blogs/p/6269661.html