一款游戏在研发初期就需要考虑多语言的问题,否则后期在进行多国语言版本时就面临着巨大的成本。鉴于之前页游的经验,其它同事设计出读取Excel的方式来管理所有的文字。但是我在使用中发现很致使的一个问题,当多人编辑一个Excel时,冲突了就很麻烦,解决起来的成本还蛮高的。

之后我想了一些办法,例如搭建一个web站点,将所有的字符串 Key、Value保存到数据库中,避免冲突,方便去查询。但感觉还是太过麻烦,成本略高。然后就想到一个办法,既然读取一个Excel容易冲突,那我就弄多个文件,一个人编辑一个Excel,这样总不会冲突了吧。然后添加 Key 的时候,先查找 Key是否存在,如果存在就提醒添加者。

这样问题就变成从读取单个文件变成遍历一个文件夹下的文件。因为Excel在打开时,会生成一个临时文件并被占用,所以不可以对它进行操作(如复制)。

  1. using System;
  2. using UnityEditor;
  3. using UnityEditor.UI;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.IO;
  7. using OfficeOpenXml;
  8. using System.Collections.Generic;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace xxxx
  12. {
  13. [InitializeOnLoad]
  14. public class StringsWatcher
  15. {
  16. static string stringsPath;
  17. static DateTime lastModifyTime;
  18. static string stringsFolderPath;
  19.  
  20. static StringsWatcher()
  21. {
  22. stringsFolderPath = Path.GetDirectoryName(Application.dataPath);
  23. stringsFolderPath = Path.Combine(stringsFolderPath, "strings");
  24.  
  25. // 创建Strings文件夹
  26. if (!Directory.Exists(stringsFolderPath))
  27. {
  28. Directory.CreateDirectory(stringsFolderPath);
  29. }
  30.  
  31. // stringsPath = Path.GetFullPath(Path.Combine(rootPath, "Strings.xlsx"));
  32.  
  33. EditorApplication.update += Update;
  34. }
  35.  
  36. static void Update()
  37. {
  38. if (EditorApplication.isPlaying || EditorApplication.isCompiling) return;
  39.  
  40. //if (!File.Exists(stringsPath)) return;
  41. if (!Directory.Exists(stringsFolderPath)) return;
  42.  
  43. DateTime time = Directory.GetLastWriteTime(stringsFolderPath);
  44.  
  45. if (lastModifyTime == time) return;
  46.  
  47. lastModifyTime = time;
  48.  
  49. Debug.Log("Reloading " + stringsFolderPath + ", Time : " + time.ToString());
  50.  
  51. DateTime startTime = DateTime.Now;
  52.  
  53. // ExcelPackage package = new ExcelPackage(new FileInfo(tempFile));
  54.  
  55. List<string> keys = new List<string>();
  56. List<string> values = new List<string>();
  57.  
  58. // 遍历 strings 目录下的excel文件
  59. DirectoryInfo folder = new DirectoryInfo(stringsFolderPath);
  60. foreach (FileInfo fileItem in folder.GetFiles())
  61. {
  62. string strFileType = fileItem.Extension;
  63.  
  64. // 如果是 excel 文件且不是临时文件, 临时文件以~$开关,读取临时文件会报错误:Invaliddataexception the file is not an valid package file
  65. if (new Regex(@"^[^~]+\.xlsx$").IsMatch(fileItem.Name.ToLower()))
  66. {
  67. string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
  68. File.Copy(fileItem.FullName, tempFile);
  69.  
  70. //Debug.Log(fileItem.Name + ", " + tempFile);
  71.  
  72. ExcelPackage package = new ExcelPackage(new FileInfo(tempFile));
  73. ExcelWorksheet sheet = package.Workbook.Worksheets[1];
  74.  
  75. int rows = sheet.Dimension.Rows;
  76. for (int row = 2; row <= rows; row++)
  77. {
  78. object keyObj = sheet.Cells[row, 1].Value;
  79. object valueObj = sheet.Cells[row, 2].Value;
  80.  
  81. // Valid key and value is null or not.
  82. if (keyObj == null)
  83. {
  84. Debug.LogError("Find Key is null. fileName : " + fileItem.Name + ", rowIndex : " + row);
  85. return;
  86. }
  87.  
  88. if (valueObj == null)
  89. {
  90. Debug.LogError("Find Key is null. fileName : " + fileItem.Name + ", rowIndex : " + row);
  91. return;
  92. }
  93.  
  94. // Find key is Exist or not
  95. if (keys.Find(x => x == keyObj.ToString()) != null)
  96. {
  97. Debug.LogError("Find Report Key. fileName : " + fileItem.Name + ", rowIndex : " + row);
  98. return;
  99. }
  100.  
  101. string key = keyObj.ToString();
  102. string value = valueObj.ToString();
  103.  
  104. keys.Add(key);
  105. values.Add(value);
  106. }
  107.  
  108. // 每删除一个文件大约多0.1秒
  109. File.Delete(tempFile);
  110. }
  111.  
  112. // 更新内存的数据,重新保存assets
  113. AssetDatabase.SaveAssets();
  114. }
  115.  
  116. DateTime endTime = DateTime.Now;
  117. Debug.Log("Rebuild string.assets time : " + (endTime - startTime).TotalSeconds + "s");
  118. }
  119. }
  120. }

如果你读取Excel时遇到了 Invaliddataexception the file is not an valid package file ,上面的代码或许对你有所帮助。

除了文字外,游戏项目中还需要管理的就是带有文字的UI图片,这个也需要提前进行约定,制定相关的规范。

Unity项目中文字的统一管理的更多相关文章

  1. 关于Unity项目中创建项目遇到的一些问题

    1.Unity调用Android的方法默认不是在UI线程执行,所以在Android上写一些页面的重绘的方法,让Unity去调用时,注意要在Android中添加对应的runOnUiThread才可以: ...

  2. 关于 Unity 项目中的 Mono 堆内存泄露

    关于 Unity 项目中的 Mono 堆内存泄露 题记:这是补一篇应该在将近一年前就应该写的记录,今天终于补上. 内存泄露是一个老话题了,之前我专门写过一篇 排查 Lua 虚拟机内存泄露 的文章,并且 ...

  3. Javascript中怎样获取统一管理的Java提示语

    项目开发中,各个页面.各个业务操作都会使用提示语.面对这么多message,更好的方式是统一管理这些消息. 这样在做国际化的时候进行统一处理也变的方便. 推荐方案使用数据库来管理全部提示语,在项目启动 ...

  4. Android中关于项目中对Thread的管理(不是线程池)

    背景 项目中对于一些并不复杂的耗时操作,比如计算,不频繁操作数据库等,因为没必要使用线程池,所以之前项目会直接使用new Thread的方式,时间一长,回头再看,原来new Thread之处已经很多了 ...

  5. 在Unity项目中使用Git

    (搬运自我在SegmentFault的博客) 本文参考了Unity官网的Mastering Unity Project Folder Structure - Version Control Syste ...

  6. 怎么在项目中使用前端包管理器bower和构建工具gulp

    下面以WeUI(微信官方网页开发样式库)介绍一下,怎么把WeUi引入到自己的项目中,我的开发环境Visual Studio 2012,当然了Visual Studio 2015对此已有了更好的支持(h ...

  7. React项目中使用Mobx状态管理(二)

    并上一节使用的是普通的数据状态管理,不过官方推荐使用装饰器模式,而在默认的react项目中是不支持装饰器的,需要手动启用. 官方参考 一.添加配置 官方提供了四种方法, 方法一.使用TypeScrip ...

  8. Unity项目中的资源管理

    这是我在2017金山技术开放日分享的部分内容.从贴图资源格式配置的介绍开始,引申出资源配置工具,最后再谈谈一整套项目资源管理方案.在GitHub上可以获取到资源配置工具的代码,是基于下面理念的一份简单 ...

  9. android 项目中使用对话框统一封装

    近期在做拼车项目中使用到了一些对话框,而且在非常多地方都使用到了,既然非常多地方使用到,那么肯定要封装一下,

随机推荐

  1. eclipse启动web应用

    在建好web项目的基础上: (1)配置tomcat服务器 点击window---->Preference----->Server---->Runtime Environment--- ...

  2. 系统环境变量(就是不需要切换目录,敲击“python”就可以进入编码器)

    1.右击我的电脑,选择属性,选择“高级系统设置” 2.选择高级,选择环境变量 3.在系统变量中找到path,点击编辑.然后新建,将python的路径复制进去,点击确定.

  3. 51Nod1367 完美森林 贪心

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1367.html 题目传送门 - 51Nod1367 题意 有一棵N个点的树,树中节点标号依次为0,1 ...

  4. 扩展中国剩余定理 (exCRT) 的证明与练习

    原文链接https://www.cnblogs.com/zhouzhendong/p/exCRT.html 扩展中国剩余定理 (exCRT) 的证明与练习 问题模型 给定同余方程组 $$\begin{ ...

  5. 给linux服务器添加一块新的磁盘

    http://www.linuxidc.com/Linux/2011-02/31868.htm 把硬盘装好后,我们用 fdisk -l 查看下: 图中可以看出 /dev/sdb 是500G,新加的硬盘 ...

  6. P1026 统计单词个数 区间dp

    题目描述 给出一个长度不超过200200的由小写英文字母组成的字母串(约定;该字串以每行2020个字母的方式输入,且保证每行一定为2020个).要求将此字母串分成kk份(1<k \le 401& ...

  7. TensorFlow池化层-函数

    池化层的作用如下-引用<TensorFlow实践>: 池化层的作用是减少过拟合,并通过减小输入的尺寸来提高性能.他们可以用来对输入进行降采样,但会为后续层保留重要的信息.只使用tf.nn. ...

  8. DT:DT实现根据乳腺肿瘤特征向量高精度预测肿瘤的是恶性还是良性—Jason niu

    %DT:DT实现根据乳腺肿瘤特征向量高精度预测肿瘤的是恶性还是良性 load data.mat a = randperm(569); Train = data(a(1:500),:); Test = ...

  9. 【Spring Boot】构造、访问Restful Webservice与定时任务

    Spring Boot Guides Examples(1~3) 参考网址:https://spring.io/guides 创建一个RESTful Web Service 使用Eclipse 创建一 ...

  10. CDN拾遗

    作为前端er,辛辛苦苦搬完砖,好不容易上线之后,正准备告一声万事大吉,回家吃鸡.忽然qa/pm/老板问,为什么我这里还是没有更新?只能是弱弱的回一声,清个缓存看看?或者还有那么一天,发现大部分区域都是 ...