Unity 国际化 多语言设置
很多游戏中都有语言设置选项,NGUI插件中自带了国际化脚本,但是灵活性较低,而且目前项目是UGUI,以下是修改后,以便记录。
Localization和NGUI中用法一样,挂在在一个不销毁的游戏物体上,并设置当前语言,及所有语言的陪标
- //----------------------------------------------
- //----------------------------------------------
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.UI;
- public class Localization : MonoBehaviour
- {
- static Localization mInst;
- static public Localization instance
- {
- get
- {
- if (mInst == null)
- {
- mInst = Object.FindObjectOfType(typeof(Localization)) as Localization;
- if (mInst == null)
- {
- GameObject go = new GameObject("_Localization");
- DontDestroyOnLoad(go);
- mInst = go.AddComponent<Localization>();
- }
- }
- return mInst;
- }
- }
- public string startingLanguage;
- public TextAsset[] languages;
- Dictionary<int, string> mDictionary = new Dictionary<int, string>();
- string mLanguage;
- public string currentLanguage
- {
- get
- {
- //if (string.IsNullOrEmpty(mLanguage))
- {
- currentLanguage = PlayerPrefs.GetString("Language");
- if (string.IsNullOrEmpty(mLanguage))
- {
- currentLanguage = startingLanguage;
- if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
- {
- currentLanguage = languages[0].name;
- }
- }
- }
- return mLanguage;
- }
- set
- {
- if (mLanguage != value)
- {
- startingLanguage = value;
- if (!string.IsNullOrEmpty(value))
- {
- if (languages != null)
- {
- for (int i = 0, imax = languages.Length; i < imax; ++i)
- {
- TextAsset asset = languages[i];
- if (asset != null && asset.name == value)
- {
- Load(asset);
- return;
- }
- }
- }
- TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;
- if (txt != null)
- {
- Load(txt);
- return;
- }
- }
- mDictionary.Clear();
- PlayerPrefs.DeleteKey("Language");
- }
- }
- }
- /// <summary>
- /// Determine the starting language.
- /// </summary>
- void Awake() { if (mInst == null) { mInst = this; DontDestroyOnLoad(gameObject); } else Destroy(gameObject); }
- /// <summary>
- /// Start with the specified starting language.
- /// </summary>
- void Start() { if (!string.IsNullOrEmpty(startingLanguage)) currentLanguage = startingLanguage; }
- /// <summary>
- /// Oddly enough... sometimes if there is no OnEnable function in Localization, it can get the Awake call after UILocalize's OnEnable.
- /// </summary>
- void OnEnable() { if (mInst == null) mInst = this; }
- /// <summary>
- /// Remove the instance reference.
- /// </summary>
- void OnDestroy() { if (mInst == this) mInst = null; }
- /// <summary>
- /// Load the specified asset and activate the localization.
- /// </summary>
- void Load(TextAsset asset)
- {
- mLanguage = asset.name;
- PlayerPrefs.SetString("Language", mLanguage);
- ByteReader reader = new ByteReader(asset);
- mDictionary = reader.ReadDictionary();
- // Broadcast("OnLocalize");
- }
- public string Get(int key)
- {
- string val;
- return (mDictionary.TryGetValue(key, out val)) ? val : null;
- }
- }
ByteReader数据解析类,不多余解释
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright © 2011-2012 Tasharen Entertainment
- //----------------------------------------------
- using UnityEngine;
- using System.Text;
- using System.Collections.Generic;
- /// <summary>
- /// MemoryStream.ReadLine has an interesting oddity: it doesn't always advance the stream's position by the correct amount:
- /// http://social.msdn.microsoft.com/Forums/en-AU/Vsexpressvcs/thread/b8f7837b-e396-494e-88e1-30547fcf385f
- /// Solution? Custom line reader with the added benefit of not having to use streams at all.
- /// </summary>
- public class ByteReader
- {
- byte[] mBuffer;
- int mOffset = 0;
- public ByteReader(byte[] bytes) { mBuffer = bytes; }
- public ByteReader(TextAsset asset) { mBuffer = asset.bytes; }
- /// <summary>
- /// Whether the buffer is readable.
- /// </summary>
- public bool canRead { get { return (mBuffer != null && mOffset < mBuffer.Length); } }
- /// <summary>
- /// Read a single line from the buffer.
- /// </summary>
- static string ReadLine(byte[] buffer, int start, int count)
- {
- #if UNITY_FLASH
- // Encoding.UTF8 is not supported in Flash :(
- StringBuilder sb = new StringBuilder();
- int max = start + count;
- for (int i = start; i < max; ++i)
- {
- byte byte0 = buffer[i];
- if ((byte0 & 128) == 0)
- {
- // If an UCS fits 7 bits, its coded as 0xxxxxxx. This makes ASCII character represented by themselves
- sb.Append((char)byte0);
- }
- else if ((byte0 & 224) == 192)
- {
- // If an UCS fits 11 bits, it is coded as 110xxxxx 10xxxxxx
- 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 an UCS fits 16 bits, it is coded as 1110xxxx 10xxxxxx 10xxxxxx
- if (++i == count) break;
- byte byte1 = buffer[i];
- if (++i == count) break;
- byte byte2 = buffer[i];
- if (byte0 == 0xEF && byte1 == 0xBB && byte2 == 0xBF)
- {
- // Byte Order Mark -- generally the first 3 bytes in a Windows-saved UTF-8 file. Skip it.
- }
- else
- {
- int ch = (byte0 & 15) << 12;
- ch |= (byte1 & 63) << 6;
- ch |= (byte2 & 63);
- sb.Append((char)ch);
- }
- }
- else if ((byte0 & 248) == 240)
- {
- // If an UCS fits 21 bits, it is coded as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- 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
- }
- /// <summary>
- /// Read a single line from the buffer.
- /// </summary>
- public string ReadLine()
- {
- int max = mBuffer.Length;
- // Skip empty characters
- 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;
- }
- /// <summary>
- /// Assume that the entire file is a collection of key/value pairs.
- /// </summary>
- 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
- {
- /// <summary>
- /// Localization key.
- /// </summary>
- public string Atlas;
- public string key;
- string mLanguage;
- bool mStarted = false;
- /// <summary>
- /// This function is called by the Localization manager via a broadcast SendMessage.
- /// </summary>
- void OnLocalize(Localization loc) { if (mLanguage != loc.currentLanguage) Localize(); }
- /// <summary>
- /// Localize the widget on enable, but only if it has been started already.
- /// </summary>
- void OnEnable() { if (mStarted && Localization.instance != null) Localize(); }
- /// <summary>
- /// Localize the widget on start.
- /// </summary>
- void Start()
- {
- mStarted = true;
- if (Localization.instance != null) Localize();
- }
- /// <summary>
- /// Force-localize the widget.
- /// </summary>
- 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 no localization key has been specified, use the label's text as the key
- if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null)
- key = lbl.text;
- // If we still don't have a key, use the widget's name
- 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 国际化 多语言设置的更多相关文章
- iOS 国际化多语言设置 xcode7
iOS 国际化多语言设置 方式一: 1. 在storyboard中创建好UI,然后在 project 里面 Localizables 栏目里面,添加你需要的语言:默认是Englist; 比如这里我添 ...
- yii2 api接口 实现国际化多语言设置
1) 在 /config/main.php 下添加如下代码: 'components' => [ 'language' => 'zh-CN', 'i18n' => [ 'transl ...
- iOS国际化多语言设置
一.创建工程.添加语言
- unity音量设置(同时设置到多个物体上)——引伸语言设置
在游戏中游戏设置是一个很重要的功能,但是比如语言设置和音量设置分散在很多个物体的组件上,如果每个对应的物体都放到一个链表里,会导致程序雍总难堪,使用事件调用是最好的方式 音量存储类 SoundMana ...
- WPF 实际国际化多语言界面
前段时候写了一个WPF多语言界面处理,个人感觉还行,分享给大家.使用合并字典,静态绑定,动态绑定.样式等东西 效果图 定义一个实体类LanguageModel,实际INotifyPropertyCha ...
- [Spring]Spring Mvc实现国际化/多语言
1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...
- iOS 学习笔记六 【APP中的文字和APP名字的国际化多语言处理】
今天为新手解决下APP中的文字和APP名字的国际化多语言处理, 不多说了,直接上步骤: 1.打开你的项目,单机project名字,选中project,直接看图吧: 2.创建Localizable.st ...
- php gettext方式实现UTF-8国际化多语言(i18n)
php gettext方式实现UTF-8国际化多语言(i18n) 一.总结 一句话总结: 二.php gettext方式实现UTF-8国际化多语言(i18n) 近 来随着i18n(国际化)的逐渐标准化 ...
- django国际化的简单设置
设置国际化的具体步骤: 一.国际化 1)效果:针对不同的国家的人可以配置不同的语言(一般是英文和中文, English Chinese) 2)目的:增加项目的用户量 3)难度:不难 比较费劲的就是 ...
随机推荐
- Django之进阶相关操作
一.QuerySet的特点 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. 1 >>> Entry.objec ...
- 什么是VC、PE、LP、GP?
天使基金主要关注原创项目构思和小型初创项目,投资规模大多在300万元以下:风险投资关注初创时期企业长期投资,规模在1000万元以下:私募股权投资主要关注3年内可以上市的成熟型企业. VC即ventur ...
- 在ubuntu下安装kaldi基本步骤
注:最近在学习kaldi语音识别工具,在安装过程中遇到了许多问题,在此记录,以备后需. 在一开始,我看了这篇博客(http://blog.topspeedsnail.com/archives/1001 ...
- ElasticSearch部署文档(Ubuntu 14.04)
ElasticSearch部署文档(Ubuntu 14.04) 参考链接 https://www.elastic.co/guide/en/elasticsearch/guide/current/hea ...
- Django学习笔记-2018.11.16
知识储备: 1 Python基础 2 数据库SQL 3 HTTP协议 4 HTML&&CSS 5 正则表达式 Django启动 django-admin startproject pr ...
- 在内网环境使用WPAD/PAC和JS攻击win10
转:https://mp.weixin.qq.com/s/qoEZE8lBbFZikKzRTwgdsw 在内网环境使用WPAD/PAC和JS攻击win10 2018-03-01 wangrin 看雪学 ...
- 洛谷P3444 [POI2006]ORK-Ploughing [枚举,贪心]
题目传送门 ork 格式难调,题面就不放了. 分析: 一道偏难的贪心和枚举题.考试的时候是弃疗了...yyb巨佬已经讲的很详细了,推荐他的博客.这里小蒟蒻就只放代码了. Code: #include& ...
- js代码小优化
今天真坑,老大请了两天假,来了之后指指点点,不过人家说的倒是很是到位 好不容易把嵌套小窗口登陆注册功能,做完了,直接调之前写好的登陆注册功能,也就是页面跳转 并不是ajax异步登陆 说让改成ajax ...
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- hdu 2433 Travel(还不会)
Problem Description One day, Tom traveled to a country named BGM. BGM is a small country, but ...