A game in the early stages of development need to consider the problem of multiple languages, or later in the multi-lingual version of the face of huge costs. In view of previous page tour experience, other colleagues designed the way to read Excel to manage all the text. But I found in the use of a very problematic problem, when many people edit an Excel, conflict is very troublesome, the cost of the solution is quite high.
Then I thought of some ways, such as building a Web site, all the string Key, value to save to the database, to avoid conflicts, easy to query. But the feeling is still too troublesome, the cost is slightly higher. Then think of a way, since reading an Excel easy conflict, then I will make multiple files, one person to edit an Excel, so there is no conflict. Then when adding a key, find out if the key exists, and if so, remind the person who added it.
So the problem becomes from reading a single file into a file that goes through a folder. Because Excel generates a temporary file and is occupied when it is opened, it cannot be manipulated (such as replication).
usingSystem;usingUnityeditor;usingUnityeditor.ui;usingUnityengine;usingUnityengine.ui;usingSystem.IO;usingOfficeopenxml;usingSystem.Collections.Generic;usingSystem.Text.RegularExpressions;namespacexxxx{[Initializeonload] Public classStringswatcher {Static stringStringspath; StaticDateTime LastModifyTime; Static stringStringsfolderpath; StaticStringswatcher () {Stringsfolderpath=Path.getdirectoryname (Application.datapath); Stringsfolderpath= Path.Combine (Stringsfolderpath,"Strings"); //Create a strings folder if(!directory.exists (Stringsfolderpath)) {directory.createdirectory (Stringsfolderpath); } //Stringspath = Path.GetFullPath (Path.Combine (RootPath, "strings.xlsx"));editorapplication.update+=Update; } Static voidUpdate () {if(Editorapplication.isplaying | | editorapplication.iscompiling)return; //if (! File.exists (Stringspath)) return; if(! Directory.Exists (Stringsfolderpath))return; DateTime Time=Directory.getlastwritetime (Stringsfolderpath); if(LastModifyTime = = time)return; LastModifyTime=Time ; Debug.Log ("Reloading"+ Stringsfolderpath +", Time:"+Time . ToString ()); DateTime StartTime=DateTime.Now; //Excelpackage package = new Excelpackage (new FileInfo (Tempfile));List<string> keys =Newlist<string>(); List<string> values =Newlist<string>(); //traverse the Excel file under the strings directoryDirectoryInfo folder =NewDirectoryInfo (Stringsfolderpath); foreach(FileInfo Fileiteminchfolder. GetFiles ()) {stringStrfiletype =fileitem.extension; //in the case of an Excel file and not a temporary file, a temporary file with the ~$ switch will report an error when reading the temporary file: invaliddataexception The file is a valid package file if(NewRegex (@"^[^~]+\.xlsx$"). IsMatch (FileItem.Name.ToLower ())) {stringTempfile =Path.Combine (Path.gettemppath (), Path.getrandomfilename ()); File.Copy (Fileitem.fullname, tempfile); //Debug.Log (Fileitem.name + "," + tempfile);Excelpackage Package=NewExcelpackage (NewFileInfo (tempfile)); Excelworksheet sheet= Package. workbook.worksheets[1]; introws =sheet. Dimension.rows; for(introw =2; Row <= rows; row++) { ObjectKeyobj = sheet. Cells[row,1]. Value; ObjectValueobj = sheet. Cells[row,2]. Value; //Valid Key and value is a null or not. if(Keyobj = =NULL) {Debug.logerror ("Find Key is null. FileName:"+ Fileitem.name +", RowIndex:"+row); return; } if(Valueobj = =NULL) {Debug.logerror ("Find Key is null. FileName:"+ Fileitem.name +", RowIndex:"+row); return; } //Find key is Exist or not if(Keys. Find (x = x = = keyobj.tostring ())! =NULL) {Debug.logerror ("Find report Key. FileName:"+ Fileitem.name +", RowIndex:"+row); return; } stringKey =keyobj.tostring (); stringValue =valueobj.tostring (); Keys. ADD (key); Values. ADD (value); } //approximately 0.1 seconds per deletion of a fileFile.delete (tempfile); } //Update memory data, Resave assetsassetdatabase.saveassets (); } DateTime endTime=DateTime.Now; Debug.Log ("Rebuild string.assets Time:"+ (Endtime-starttime). TotalSeconds +"s"); } }}
If you are reading Excel when you encounter invaliddataexception the file is a valid package file , the above code may help you.
In addition to the text, the game project also need to manage the UI picture with text, this also need to advance the Convention, to formulate the relevant specifications.
Unified management of text in unity projects