Unity实现本地文本多语言化

1. Introduction

One of the key requirements for any successful app or game that wants to target a global audience is the ability to support multiple languages. Unity, being a popular game development engine, provides built-in features to support localization and make it easier to create a multi-language app. In this article, we will explore how to implement local text multi-language support in Unity.

2. Creating Language Files

In order to support multiple languages, we need to create language files that contain the translations for each language. These files can be in various formats such as CSV, XML, or JSON. For simplicity, we will use a JSON format.

Here is an example of how a language file may look like for English:

{

"greeting": "Hello",

"message": "Welcome to our app!"

}

In this example, we have two keys, "greeting" and "message", and their corresponding translations for English. We can create similar files for other languages.

{

"greeting": "Bonjour",

"message": "Bienvenue dans notre application!"

}

The above example shows the French translations for the same keys.

3. Loading Language Files

Once we have our language files ready, we need to load them into Unity so that we can use the translations in our app. Unity provides the TextAsset class to load text files, so we can use it to load our language files.

Here is an example of how we can load a language file using the TextAsset class:

[SerializeField] private TextAsset languageFile;

private Dictionary<string, string> translations;

private void Awake()

{

translations = JsonUtility.FromJson<Dictionary<string, string>>(languageFile.text);

}

In the above example, we serialize a variable of type TextAsset and load the JSON file into it. Then, we use the JsonUtility class provided by Unity to convert the JSON data into a dictionary of key-value pairs.

3.1 Adding Language Switching

To support switching between languages, we can create a dropdown menu or a set of buttons that allow the user to select their preferred language. When the user selects a language, we can load the corresponding language file and update the translations.

Here is an example of how we can implement language switching:

public void SwitchLanguage(string language)

{

// Load language file based on selected language

TextAsset selectedLanguageFile = Resources.Load<TextAsset>("Languages/" + language);

// Update translations

translations = JsonUtility.FromJson<Dictionary<string, string>>(selectedLanguageFile.text);

}

In the above example, we load the selected language file from the "Languages" folder in the Unity project's resources. We then update the translations with the new language file.

4. Localizing Text

Now that we have our language files loaded and translations available, we can start localizing the text in our app. Unity provides the Text component, which we can use to display localized text.

Here is an example of how we can localize a text component:

[SerializeField] private Text greetingText;

private void Start()

{

greetingText.text = translations["greeting"];

}

In the above example, we assign the translated value of the "greeting" key to the Text component's text property. This will display the localized greeting text.

4.1 Dynamically Updating Text

In some cases, we may need to update the text dynamically during runtime. For example, when the player achieves a certain milestone, we may want to display a congratulatory message.

We can achieve this by creating a script that updates the text based on the current language:

[SerializeField] private Text messageText;

private void UpdateMessage()

{

if (translations.ContainsKey("message"))

{

messageText.text = translations["message"];

}

else

{

Debug.LogError("Translation not found for key: message");

}

}

In the above example, we check if the translations dictionary contains the "message" key, and if so, we update the Text component's text property with the translated value. If the key is not found, we display an error message.

5. Conclusion

In this article, we have explored how to implement local text multi-language support in Unity. We have seen how to create language files, load them into Unity, switch between languages, and localize text components. By following these steps, you can easily make your app or game accessible to a global audience and provide a better user experience for non-English speakers.

后端开发标签