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)难度:不难 比较费劲的就是 ...
随机推荐
- LightOJ 1370- Bi-shoe and Phi-shoe (欧拉函数)
题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一个幸运数字, ...
- hdu 5912(迭代+gcd)
Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- wordpress后台加载速度异常缓慢排查记录(原创)
原因在于在function.php函数中加入了下面的代码导致了缓慢: //停用版本更新通知remove_action('load-update-core.php', 'wp_update_themes ...
- 解决wordpress无法发送邮件的问题|配置好WP-Mail-SMTP的前提
我的WordPress主机是万网的,配置WP-Mail-SMTP时一直无法发送邮件,导致设置失败.经过多次询问度娘才找到了解决wordpress无法发送邮件的方法,在这里把这个wordpress技巧分 ...
- es6 class 中 constructor 方法 和 super
首先,ES6 的 class 属于一种“语法糖”,所以只是写法更加优雅,更加像面对对象的编程,其思想和 ES5 是一致的. <1>constructor function Point(x, ...
- Codeforces Round #300 E - Demiurges Play Again
E - Demiurges Play Again 感觉这种类型的dp以前没遇到过... 不是很好想.. dp[u] 表示的是以u为子树进行游戏得到的值是第几大的. #include<bits/s ...
- qrcode 生成二维码
qrcode 生成二维码 Demo: https://www.hgnulb.cn/freedom/qrcode/qrcode.html qrcodeGithub 地址: https://github. ...
- java总结(方法与对象)
包 1.用于管理类 2.包名采用公司的域名倒序+项目名+模块名 3.包的引入 类 1.main方法用static 它调用的方法也要static 2.程序要运行必须要main方法 3.类是一种引用数据类 ...
- 【转】VueJS中学习使用Vuex详解
转自:https://segmentfault.com/a/1190000015782272 在SPA单页面组件的开发中 Vue的vuex和React的Redux 都统称为同一状态管理,个人的理解 ...
- Django Suit v2-dev 使用
转:链接:https://www.jianshu.com/p/84fa8219fb48 官方文档: 链接 Git: 链接 install Django Suit 为了适配 Django 有许多不同的版 ...