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)难度:不难 比较费劲的就是 ...
随机推荐
- java基础7 封装
面向对象的三大特征: 1.封装 (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承 ...
- kubeadm高可用master节点部署文档
kubeadm的标准部署里,etcd和master都是单节点的. 但上生产,至少得高可用. etcd的高可用,用kubeadm微微扩散一下就可以. 但master却官方没有提及. 于是搜索了几篇文档, ...
- 关闭webstorm自动保存,并显示文件未保存标识
1.取消自动保存 2.显示编辑状态设置:
- Python全栈开发之10、网络编程
网络编程就是如何在程序中实现两台计算机的通信,而两台计算机间的通信其实就是两个进程间的通信,进程间的通信主要通过socket(套接字)来描述ip地址(主机)和端口(进程)来实现的,因此我们学习网络编程 ...
- Gitlab在centos7上手工安装
本节内容: 安装包 安装Gitlab 配置Gitlab URL 配置防火墙 执行Gitlab的一些基本设置 环境及条件: 系统:CentOS Linux release 7.5.1804 (Core) ...
- 【WPF】设置Listview样式
<ListView Grid.Row="0" ItemsSource="{Binding Lst_bind}"> <ListView.Item ...
- Java Script 基础
一. JS的简介 JavaScript是一种网页编程技术,经常用于创建动态交互网页 JavaScript是一种基于对象和事件驱动的解释性脚本语言,类似C语言和Java的语法 事先不编译:逐行执行:无需 ...
- linux网络管理----网络命令
1 Linux网络命令之网络环境查看命令 ifconfig 2 Linux网络命令之网络测试命令 telnet 基本已经被ssh替代了,telnet是明文传递,不安全
- linux网络管理----Linux网络配置
1.1 linux配置IP地址 1️⃣ ifconfig命令临时配置IP地址 能不能自动获取IP地址和DNS地址,要看有没有DHCP服务器 win10上,输入ipconfig /all 会看到是否有D ...
- java 数组操作方法
数组操作方法: 实现数组拷贝: 语法:System.arraycopy(源数组名称,源数组拷贝开始索引,目标数组名称,目标数组拷贝数组索引,长度) 数组A:1 . 2 . 3 . 4 . 5 . 6 ...