很多游戏中都有语言设置选项,NGUI插件中自带了国际化脚本,但是灵活性较低,而且目前项目是UGUI,以下是修改后,以便记录。

Localization和NGUI中用法一样,挂在在一个不销毁的游戏物体上,并设置当前语言,及所有语言的陪标

  1. //----------------------------------------------
  2. //----------------------------------------------
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. public class Localization : MonoBehaviour
  7. {
  8. static Localization mInst;
  9. static public Localization instance
  10. {
  11. get
  12. {
  13. if (mInst == null)
  14. {
  15. mInst = Object.FindObjectOfType(typeof(Localization)) as Localization;
  16. if (mInst == null)
  17. {
  18. GameObject go = new GameObject("_Localization");
  19. DontDestroyOnLoad(go);
  20. mInst = go.AddComponent<Localization>();
  21. }
  22. }
  23. return mInst;
  24. }
  25. }
  26. public string startingLanguage;
  27. public TextAsset[] languages;
  28. Dictionary<int, string> mDictionary = new Dictionary<int, string>();
  29. string mLanguage;
  30. public string currentLanguage
  31. {
  32. get
  33. {
  34. //if (string.IsNullOrEmpty(mLanguage))
  35. {
  36. currentLanguage = PlayerPrefs.GetString("Language");
  37. if (string.IsNullOrEmpty(mLanguage))
  38. {
  39. currentLanguage = startingLanguage;
  40. if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
  41. {
  42. currentLanguage = languages[0].name;
  43. }
  44. }
  45. }
  46. return mLanguage;
  47. }
  48. set
  49. {
  50. if (mLanguage != value)
  51. {
  52. startingLanguage = value;
  53. if (!string.IsNullOrEmpty(value))
  54. {
  55. if (languages != null)
  56. {
  57. for (int i = 0, imax = languages.Length; i < imax; ++i)
  58. {
  59. TextAsset asset = languages[i];
  60. if (asset != null && asset.name == value)
  61. {
  62. Load(asset);
  63. return;
  64. }
  65. }
  66. }
  67. TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;
  68. if (txt != null)
  69. {
  70. Load(txt);
  71. return;
  72. }
  73. }
  74. mDictionary.Clear();
  75. PlayerPrefs.DeleteKey("Language");
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Determine the starting language.
  81. /// </summary>
  82. void Awake() { if (mInst == null) { mInst = this; DontDestroyOnLoad(gameObject); } else Destroy(gameObject); }
  83. /// <summary>
  84. /// Start with the specified starting language.
  85. /// </summary>
  86. void Start() { if (!string.IsNullOrEmpty(startingLanguage)) currentLanguage = startingLanguage; }
  87. /// <summary>
  88. /// Oddly enough... sometimes if there is no OnEnable function in Localization, it can get the Awake call after UILocalize's OnEnable.
  89. /// </summary>
  90. void OnEnable() { if (mInst == null) mInst = this; }
  91. /// <summary>
  92. /// Remove the instance reference.
  93. /// </summary>
  94. void OnDestroy() { if (mInst == this) mInst = null; }
  95. /// <summary>
  96. /// Load the specified asset and activate the localization.
  97. /// </summary>
  98. void Load(TextAsset asset)
  99. {
  100. mLanguage = asset.name;
  101. PlayerPrefs.SetString("Language", mLanguage);
  102. ByteReader reader = new ByteReader(asset);
  103. mDictionary = reader.ReadDictionary();
  104. // Broadcast("OnLocalize");
  105. }
  106. public string Get(int key)
  107. {
  108. string val;
  109. return (mDictionary.TryGetValue(key, out val)) ? val : null;
  110. }
  111. }

ByteReader数据解析类,不多余解释

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Text;
  7. using System.Collections.Generic;
  8. /// <summary>
  9. /// MemoryStream.ReadLine has an interesting oddity: it doesn't always advance the stream's position by the correct amount:
  10. /// http://social.msdn.microsoft.com/Forums/en-AU/Vsexpressvcs/thread/b8f7837b-e396-494e-88e1-30547fcf385f
  11. /// Solution? Custom line reader with the added benefit of not having to use streams at all.
  12. /// </summary>
  13. public class ByteReader
  14. {
  15. byte[] mBuffer;
  16. int mOffset = 0;
  17. public ByteReader(byte[] bytes) { mBuffer = bytes; }
  18. public ByteReader(TextAsset asset) { mBuffer = asset.bytes; }
  19. /// <summary>
  20. /// Whether the buffer is readable.
  21. /// </summary>
  22. public bool canRead { get { return (mBuffer != null && mOffset < mBuffer.Length); } }
  23. /// <summary>
  24. /// Read a single line from the buffer.
  25. /// </summary>
  26. static string ReadLine(byte[] buffer, int start, int count)
  27. {
  28. #if UNITY_FLASH
  29. // Encoding.UTF8 is not supported in Flash :(
  30. StringBuilder sb = new StringBuilder();
  31. int max = start + count;
  32. for (int i = start; i < max; ++i)
  33. {
  34. byte byte0 = buffer[i];
  35. if ((byte0 & 128) == 0)
  36. {
  37. // If an UCS fits 7 bits, its coded as 0xxxxxxx. This makes ASCII character represented by themselves
  38. sb.Append((char)byte0);
  39. }
  40. else if ((byte0 & 224) == 192)
  41. {
  42. // If an UCS fits 11 bits, it is coded as 110xxxxx 10xxxxxx
  43. if (++i == count) break;
  44. byte byte1 = buffer[i];
  45. int ch = (byte0 & 31) << 6;
  46. ch |= (byte1 & 63);
  47. sb.Append((char)ch);
  48. }
  49. else if ((byte0 & 240) == 224)
  50. {
  51. // If an UCS fits 16 bits, it is coded as 1110xxxx 10xxxxxx 10xxxxxx
  52. if (++i == count) break;
  53. byte byte1 = buffer[i];
  54. if (++i == count) break;
  55. byte byte2 = buffer[i];
  56. if (byte0 == 0xEF && byte1 == 0xBB && byte2 == 0xBF)
  57. {
  58. // Byte Order Mark -- generally the first 3 bytes in a Windows-saved UTF-8 file. Skip it.
  59. }
  60. else
  61. {
  62. int ch = (byte0 & 15) << 12;
  63. ch |= (byte1 & 63) << 6;
  64. ch |= (byte2 & 63);
  65. sb.Append((char)ch);
  66. }
  67. }
  68. else if ((byte0 & 248) == 240)
  69. {
  70. // If an UCS fits 21 bits, it is coded as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  71. if (++i == count) break;
  72. byte byte1 = buffer[i];
  73. if (++i == count) break;
  74. byte byte2 = buffer[i];
  75. if (++i == count) break;
  76. byte byte3 = buffer[i];
  77. int ch = (byte0 & 7) << 18;
  78. ch |= (byte1 & 63) << 12;
  79. ch |= (byte2 & 63) << 6;
  80. ch |= (byte3 & 63);
  81. sb.Append((char)ch);
  82. }
  83. }
  84. return sb.ToString();
  85. #else
  86. return Encoding.UTF8.GetString(buffer, start, count);
  87. #endif
  88. }
  89. /// <summary>
  90. /// Read a single line from the buffer.
  91. /// </summary>
  92. public string ReadLine()
  93. {
  94. int max = mBuffer.Length;
  95. // Skip empty characters
  96. while (mOffset < max && mBuffer[mOffset] < 32) ++mOffset;
  97. int end = mOffset;
  98. if (end < max)
  99. {
  100. for (;;)
  101. {
  102. if (end < max)
  103. {
  104. int ch = mBuffer[end++];
  105. if (ch != '\n' && ch != '\r') continue;
  106. }
  107. else ++end;
  108. string line = ReadLine(mBuffer, mOffset, end - mOffset - 1);
  109. mOffset = end;
  110. return line;
  111. }
  112. }
  113. mOffset = max;
  114. return null;
  115. }
  116. /// <summary>
  117. /// Assume that the entire file is a collection of key/value pairs.
  118. /// </summary>
  119. public Dictionary<int, string> ReadDictionary()
  120. {
  121. Dictionary<int, string> dict = new Dictionary<int, string>();
  122. char[] separator = new char[] { '=' };
  123. while (canRead)
  124. {
  125. string line = ReadLine();
  126. if (line == null) break;
  127. #if UNITY_FLASH
  128. string[] split = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
  129. #else
  130. string[] split = line.Split(separator, 2, System.StringSplitOptions.RemoveEmptyEntries);
  131. #endif
  132. if (split.Length == 2)
  133. {
  134. int key = int.Parse(split[0].Trim());
  135. string val = split[1].Trim();
  136. dict[key] = val;
  137. }
  138. }
  139. return dict;
  140. }
  141. }

将UILocalize脚本挂在在Text上或者Image上,就是需要多语言切换的UI上

  1. //----------------------------------------------
  2. //----------------------------------------------
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class UILocalize : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// Localization key.
  9. /// </summary>
  10. public string Atlas;
  11. public string key;
  12. string mLanguage;
  13. bool mStarted = false;
  14. /// <summary>
  15. /// This function is called by the Localization manager via a broadcast SendMessage.
  16. /// </summary>
  17. void OnLocalize(Localization loc) { if (mLanguage != loc.currentLanguage) Localize(); }
  18. /// <summary>
  19. /// Localize the widget on enable, but only if it has been started already.
  20. /// </summary>
  21. void OnEnable() { if (mStarted && Localization.instance != null) Localize(); }
  22. /// <summary>
  23. /// Localize the widget on start.
  24. /// </summary>
  25. void Start()
  26. {
  27. mStarted = true;
  28. if (Localization.instance != null) Localize();
  29. }
  30. /// <summary>
  31. /// Force-localize the widget.
  32. /// </summary>
  33. public void Localize(string key = null)
  34. {
  35. if (key == null)
  36. key = this.key;
  37. Localization loc = Localization.instance;
  38. MaskableGraphic w = GetComponent<MaskableGraphic>();
  39. Text lbl = w as Text;
  40. Image sp = w as Image;
  41. // If no localization key has been specified, use the label's text as the key
  42. if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null)
  43. key = lbl.text;
  44. // If we still don't have a key, use the widget's name
  45. string val = string.IsNullOrEmpty(key) ? w.name : loc.Get(int.Parse(key));
  46. if (val != null)
  47. {
  48. if (lbl != null)
  49. {
  50. lbl.text = val;
  51. }
  52. else if (sp != null)
  53. {
  54. if (Atlas == null)
  55. return;
  56. sp.sprite = DoRo.Manager.LoadManager.Instance.LoadSprite(Atlas, val);
  57. }
  58. }
  59. mLanguage = loc.currentLanguage;
  60. }
  61. }

Unity 国际化 多语言设置的更多相关文章

  1. iOS 国际化多语言设置 xcode7

    iOS 国际化多语言设置 方式一: 1. 在storyboard中创建好UI,然后在 project 里面  Localizables 栏目里面,添加你需要的语言:默认是Englist; 比如这里我添 ...

  2. yii2 api接口 实现国际化多语言设置

    1) 在 /config/main.php 下添加如下代码: 'components' => [ 'language' => 'zh-CN', 'i18n' => [ 'transl ...

  3. iOS国际化多语言设置

    一.创建工程.添加语言

  4. unity音量设置(同时设置到多个物体上)——引伸语言设置

    在游戏中游戏设置是一个很重要的功能,但是比如语言设置和音量设置分散在很多个物体的组件上,如果每个对应的物体都放到一个链表里,会导致程序雍总难堪,使用事件调用是最好的方式 音量存储类 SoundMana ...

  5. WPF 实际国际化多语言界面

    前段时候写了一个WPF多语言界面处理,个人感觉还行,分享给大家.使用合并字典,静态绑定,动态绑定.样式等东西 效果图 定义一个实体类LanguageModel,实际INotifyPropertyCha ...

  6. [Spring]Spring Mvc实现国际化/多语言

    1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...

  7. iOS 学习笔记六 【APP中的文字和APP名字的国际化多语言处理】

    今天为新手解决下APP中的文字和APP名字的国际化多语言处理, 不多说了,直接上步骤: 1.打开你的项目,单机project名字,选中project,直接看图吧: 2.创建Localizable.st ...

  8. php gettext方式实现UTF-8国际化多语言(i18n)

    php gettext方式实现UTF-8国际化多语言(i18n) 一.总结 一句话总结: 二.php gettext方式实现UTF-8国际化多语言(i18n) 近 来随着i18n(国际化)的逐渐标准化 ...

  9. django国际化的简单设置

    设置国际化的具体步骤: 一.国际化 1)效果:针对不同的国家的人可以配置不同的语言(一般是英文和中文,  English  Chinese) 2)目的:增加项目的用户量 3)难度:不难 比较费劲的就是 ...

随机推荐

  1. 让MySql支持Emoji表情存储

    java后台报错,如下. aused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column ...

  2. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  3. 号外,号外 -几乎所有的binary search和mergesort都有错

    号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...

  4. gm(GraphicsMagick)图片中文水印乱码问题

    1.GraphicsMagick图片中文水印乱码问题处理方式 如出现乱码是由于服务器中缺少中文字库所致,为避免系统中存在多个中文字库冲突, 所以没有必要在安装GraphicsMagick时就将字库文件 ...

  5. GLASNICI 解题报告

    GLASNICI 解题报告 题目描述 有N个人在一直线上,第i个人的位置为Di,满足Di≤Di+1.最初只有第1个人(在最左边)知道消息. 在任意时刻,每个人可以以每秒1单位的速度向左或向右移动,或者 ...

  6. MVC – 15.路由机制

    15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...

  7. vs2013设置语言

    设置语言格式 [工具]-[选项]-[国际化]

  8. Web前端开发最佳实践(2):前端代码重构

    前言 代码重构是业内经常讨论的一个热门话题,重构指的是在不改变代码外部行为的情况下进行源代码修改,所以重构之前需要考虑的是重构后如何才能保证外部行为不改变.对于后端代码来说,可以通过大量的自动化测试来 ...

  9. 9 行 javascript 代码获取 QQ 群成员

    昨天看到一条微博:「22 行 JavaScript 代码实现 QQ 群成员提取器」. 本着好奇心点击进去,发现没有达到效果,一是 QQ 版本升级了,二是博客里面的代码也有些繁琐. 于是自己试着写了一个 ...

  10. ls 大全

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...