Unity3d Study of multi-language in Chinese to do key

Source: Internet
Author: User



Multi-language time to do key in Chinese is definitely 100% of the advantages, assuming that the code in English to the text assigned to the place are all English. Write more code after the maintenance of no one can read, or look very laborious.

To Poolmanager not understand can look under the Poolmanager plug-in is very suitable for beginners to learn.

Talk about the principle of key in Chinese: There are two places in unity where text is assigned, one is pre-fabricated on the UI prefab. The other is the text written in the code. Then in the development phase we write in the prefab and the code directly in Chinese, and so on the project later through the tool to all the Chinese place all extracted. Then the extracted Chinese handed over to the planning. Let's plan them to translate, so that the Chinese we wrote earlier is a multi-language key. Finally, the text displayed on the interface is read with this Chinese key reading table.

Ngui all the text is in the Uilabel, but we want to do picture and text mixed, is generally in the Uilabel on the expansion of a script, with this script in the generation of the corresponding Uilabel and uisprite. In this article I will first explain the principle by Uilabel.

1. Traverse all Uiprefab to find out the components including Uilabe (or the one you wrote yourself) and extract the text.

2. Traverse the entire CS code. Put all the Strutil.gettext (" I want to change the line of successful wisdom net "); Both the Chinese character and the characters in the middle of the double argument are extracted.

Directly on the idea code.

Using Unityengine;
Using System.Collections;
Using Unityeditor;
Using System.Collections.Generic;
Using System.IO;
Using System.Text;
Using System.Text.RegularExpressions;

public class Teststart:editor
{
Uiprefab Folder folder
private static string Uiprefabpath = Application.datapath + "/ui";
Folder folder for scripts
private static string scriptpath = Application.datapath + "/scripts";
Exported Chinese key Path
private static string Outpath = Application.datapath + "/out.txt";

private static list<string>localization = null;
private static string staticwritetext = "";
[MenuItem ("tools/Export multiple Languages")]
static void Exportchinese ()
{
Localization = new list<string> ();
Staticwritetext = "";

Extract the Chinese on the prefab
Staticwritetext + = "----------------Prefab----------------------\ n";
Loaddiectoryprefab (New DirectoryInfo (Uiprefabpath));

Extract the Chinese from CS
Staticwritetext + = "----------------Script----------------------\ n";
Loaddiectorycs (New DirectoryInfo (ScriptPath));


Finally, the extracted Chinese produced
string textpath = Outpath;
if (System.IO.File.Exists (Textpath))
{
File.delete (Textpath);
}
using (StreamWriter writer = new StreamWriter (Textpath, False, Encoding.UTF8))
{
Writer. Write (Staticwritetext);
}
Assetdatabase.refresh ();
}

Recursive all UI Prefab
static public void Loaddiectoryprefab (DirectoryInfo dictoryinfo)
{
if (!dictoryinfo.exists) return;
DirectoryInfo []chinddictoryinfos = Dictoryinfo.getdirectories ();
foreach (DirectoryInfo chinddictoryinfo in Chinddictoryinfos)
{
Loaddiectoryprefab (Chinddictoryinfo);
}
fileinfo[] Fileinfos = Dictoryinfo.getfiles ("*.prefab", searchoption.alldirectories);
foreach (FileInfo files in Fileinfos)
{
string path = files. FullName;
String Assetpath = path. Substring (path. IndexOf ("assets/"));
Gameobject prefab = Assetdatabase.loadassetatpath (Assetpath, typeof (Gameobject)) as Gameobject;
Gameobject instance = gameobject.instantiate (prefab) as Gameobject;
Searchprefabstring (Instance.transform);
Gameobject.destroyimmediate (instance);
}
}

Recursive all C # code
static public void Loaddiectorycs (DirectoryInfo dictoryinfo)
{

if (!dictoryinfo.exists) return;
DirectoryInfo []chinddictoryinfos = Dictoryinfo.getdirectories ();
foreach (DirectoryInfo chinddictoryinfo in Chinddictoryinfos)
{
Loaddiectoryprefab (Chinddictoryinfo);
}
fileinfo[] Fileinfos = Dictoryinfo.getfiles ("*.cs", searchoption.alldirectories);
foreach (FileInfo files in Fileinfos)
{
string path = files. FullName;
String Assetpath = path. Substring (path. IndexOf ("assets/"));
Textasset Textasset = Assetdatabase.loadassetatpath (Assetpath, typeof (Textasset)) as Textasset;
string text = Textasset.text;
Use the regular form to extract the string between two strings in the code.
Regex reg = new Regex ("strutil.gettext\\ (\". *?\ "");
MatchCollection mc = Reg. Matches (text);
foreach (Match m in MC)
{
string format = M.value;
format = format. Replace ("Strutil.gettext (\" "," ");
format = format. Replace ("\" "," ");
if (! Localization.contains (format) &&!string. IsNullOrEmpty (format)) {
Localization.add (format);
Staticwritetext+=format+ "\ n";
}
}
}
}

   //extract Chinese on prefab
 static public void searchprefabstring (Transform root)
 {
  foreach (Transform chind in root)
  {
   //Because this is a writing example, I'm using Uilabel
   //here should be the script you use for text-to-picture blending.
   uilabel label = Chind. Getcomponent<uilabel> ();
   if (Label! = NULL)
   {
    string text = label.text;
    if (! Localization.contains (text) &&!string. IsNullOrEmpty (text)) {
     localization.add (text);
     text = text. Replace ("\ n", @ "\ n");
     staticwritetext+=text+ "\ n";
    }
   }
   if (chind.childcount >0)
    searchprefabstring (chind);
  }
 }
}

For example, this is an assignment code on a simple interface. Use Strutil.gettext () to pick up Chinese. The Strutil class was written by ourselves.

Using Unityengine;
Using System.Collections;
public class Uimain:monobehaviour
{
Private UILabel mname = null;
void Awake ()
{
Mname = transform. Find ("name"). Getcomponent<uilabel> ();
Mname.text = Strutil.gettext ("Rain pine momo\n I want to Change line");
Mname.text = Strutil.gettext ("I am {0} my net name {1}", "Xuanhusung", "Rain pine Momo");
}
}

The Strutil class handles the key from the Local data table to replace the text displayed in the corresponding multi-language.

Using Unityengine;
Using System.Collections;

public class Strutil
{
static public string GetText (string text)
{
Go to the data table by passing in the Chinese key to read the corresponding replacement of the multilingual text
return text;
}

static public string GetText (string Text,params object[] args)
{
Go to the data table by passing in the Chinese key to read the corresponding replacement of the multilingual text
return string. Format (Text,args);
}
}

Using the tool code extraction, finally, all the multilingual Chinese place in the txt.

Finally, let the plan hold the generated Chinese key in the Excel table. Give the corresponding translated text.

Another important point of knowledge is the line-wrapping problem, which may be a newline operation on prefab, but \ n is not a string. So we're going to go to the "\ n" string and write it in.

Text. Replace ("\ n", @ "\ n");

In turn, when reading a table, you still need to convert the "\ n" string to \ n line break

Text. Replace (@ "\ n", "\ n");

That's fine. Planning can also be directly in the data table to fill in \ n to line up.

The final thought

1. The development process may have to change the code or to add a new function to delete the function, so we have to extract the difference in the Chinese key, that is, the newly added key or the newly deleted key listed.

Because there is no change, there is no need to plan another translation.

2. It is best to help plan and generate Excel files directly. Very easy on Windows. But Mac can't. I know how to read Excel files on my Mac. But I don't know how to generate Excel on a MAC who knows what the big God is. Please let me know.

To be able to generate the kind of. xlsx. Thank you.

3. Because I want to do the picture and text mix, so uilabel I have not directly used, but also wrote a class to manage uilable and uisprite, in fact, is based on XML or JSON A class of descriptive descriptors to dynamically generate uilable and uisprite to help it in the dynamic calculation of the coordinates, the calculation of the spacing class.

Because your Chinese key needs to be a reference to the "I'm {0} I'm {1} this year," a type of string. So I'm still writing a method.

Finally, the paper

p=1014, in fact, this article mainly provides a train of thought. Let's say you have a better idea of multiple languages, or a way to do it. Welcome to the following message to me, thank you.


Unity3d Study of multi-language in Chinese to do key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.