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)难度:不难 比较费劲的就是 ...
随机推荐
- 让MySql支持Emoji表情存储
java后台报错,如下. aused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- 号外,号外 -几乎所有的binary search和mergesort都有错
号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...
- gm(GraphicsMagick)图片中文水印乱码问题
1.GraphicsMagick图片中文水印乱码问题处理方式 如出现乱码是由于服务器中缺少中文字库所致,为避免系统中存在多个中文字库冲突, 所以没有必要在安装GraphicsMagick时就将字库文件 ...
- GLASNICI 解题报告
GLASNICI 解题报告 题目描述 有N个人在一直线上,第i个人的位置为Di,满足Di≤Di+1.最初只有第1个人(在最左边)知道消息. 在任意时刻,每个人可以以每秒1单位的速度向左或向右移动,或者 ...
- MVC – 15.路由机制
15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...
- vs2013设置语言
设置语言格式 [工具]-[选项]-[国际化]
- Web前端开发最佳实践(2):前端代码重构
前言 代码重构是业内经常讨论的一个热门话题,重构指的是在不改变代码外部行为的情况下进行源代码修改,所以重构之前需要考虑的是重构后如何才能保证外部行为不改变.对于后端代码来说,可以通过大量的自动化测试来 ...
- 9 行 javascript 代码获取 QQ 群成员
昨天看到一条微博:「22 行 JavaScript 代码实现 QQ 群成员提取器」. 本着好奇心点击进去,发现没有达到效果,一是 QQ 版本升级了,二是博客里面的代码也有些繁琐. 于是自己试着写了一个 ...
- ls 大全
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...