Unity realizes multilingual

In game development, it is very important to realize multilingual function. Here is a way to implement multilingual in Unity.

  1. Create language files

        First, create a folder called "Localization" in your project. Create a subfolder within this folder called "Languages". In the "Languages" folder, create a separate script for each language. For example, for English, create a script called "en_US.cs". In this script, all strings that need to be translated are stored in key-value pairs. Here is an example:

public static class en_US {
    public static string PLAY = "Play";
    public static string OPTIONS = "Options";
    // other strings
}

        In scripts in other languages, translate the corresponding strings to text in that language. For example, for Chinese, you can create a script called "zh_CN.cs" with the following content:                                                        

public static class zh_CN {
    public static string PLAY = "开始游戏";
    public static string OPTIONS = "选项";
    // other strings
}

        2. Create a localization manager class 

        Next, create a C# script called "LocalizationManager" in the project. This script is responsible for loading and switching between different languages, and provides methods to get strings in language files.

// LocalizationManager.cs

using System.Collections.Generic;
using UnityEngine;

public class LocalizationManager : MonoBehaviour {
    // 单例模式:确保只有一个实例存在,可以在整个游戏中被访问
    public static LocalizationManager Instance { get; private set; }

    // 字典:存储文本字符串和对应的键值对
    private Dictionary<string, string> localizedText;
    // 当前语言,默认为英文
    private string currentLanguage = "en_US";

    private void Awake() {
        // 单例模式:确保只有一个实例存在
        if (Instance == null) {
            Instance = this;
            DontDestroyOnLoad(gameObject);
            LoadLocalizedText(currentLanguage);
        }
        else {
            Destroy(gameObject);
        }
    }

    /// <summary>
    /// 加载指定语言的文本文件并解析为键值对,存储到字典中
    /// </summary>
    private void LoadLocalizedText(string language) {
        // 创建新的字典
        localizedText = new Dictionary<string, string>();

        // 加载文本文件
        TextAsset textAsset = Resources.Load<TextAsset>("Localization/Languages/" + language);
        // 分行解析文本内容,并添加到字典中
        string[] lines = textAsset.text.Split('\n');

        foreach (string line in lines) {
            string[] keyValue = line.Split('=');
            localizedText.Add(keyValue[0], keyValue[1]);
        }
    }

    /// <summary>
    /// 切换语言并重新加载文本文件
    /// </summary>
    public void SetLanguage(string language) {
        if (language != currentLanguage) {
            currentLanguage = language;
            LoadLocalizedText(currentLanguage);
        }
    }

    /// <summary>
    /// 获取指定键的本地化字符串
    /// </summary>
    public string GetLocalizedValue(string key) {
        // 如果存在该键,则返回对应的值;否则返回原始字符串
        string result = key;
        if (localizedText.ContainsKey(key)) {
            result = localizedText[key];
        }
        return result;
    }
}

        This class uses the singleton pattern to ensure that only one instance exists and can be accessed throughout the game. It uses Dictionary to store text strings and corresponding key-value pairs.

        The LoadLocalizedText method loads a text file in the current language and adds each key-value pair to a dictionary. The SetLanguage method switches the language and calls LoadLocalizedText to load a new text file. The GetLocalizedValue method gets the localized string for the specified key.

        3. Use multilingual text in the game

Finally, use the UI Text component to display multilingual strings. In the text field of the UI text component, use the following code to call the LocalizationManager to obtain the corresponding localized string:

// TextLocalization.cs

using UnityEngine;
using UnityEngine.UI;

public class TextLocalization : MonoBehaviour {
    // 需要翻译的字符串的键值
    public string key;

    private Text textComponent;

    private void Start() {
        // 查找UI文本组件并保存
        textComponent = GetComponent<Text>();
        // 更新文本
        UpdateText();
    }

    /// <summary>
    /// 获取对应的本地化字符串,并更新UI文本内容
    /// </summary>
    public void UpdateText() {
        textComponent.text = LocalizationManager.Instance.GetLocalizedValue(key);
    }
}

        This class is attached to the UI text component and sets the key field to the key value of the string to be translated. In the Start method, it finds the UI text component and saves it, then calls UpdateText to update the text. 

        The above is a simple way to achieve multi-language in Unity. By creating language files, creating localization manager classes, and using UI text components, you can easily make your game support multiple languages.

Guess you like

Origin blog.csdn.net/Asklyw/article/details/130363964