首页 > 技术文章 > Unity3d语言表配置工具

Yellow0-0River 原文

游戏开发过程中,根据策划需求经常会有多语言功能,最简单的实现方法即为将游戏中所有的文字配置在一张数据表中,然后根据需求动态的加载配置表。在UI拼接过程中,经常会有一些文字是固定不变的,那么就需要将这些字放入数据表中,然后策划或他人根据此数据表将文字翻译为其它语言。如下图(需要将选中的文字进行配置):

本人开发过程中,习惯于每个固定不变的文字(Text或Label)上面挂载一脚本,在Awake或者Start方法中根据数据表内容设置文字信息。但是在UI拼接过程中,根据美术提供的UI每次手动的将对应的文字输入到UI位置(Text或Label),将文字对应的key输入到挂载脚本位置,然后再将文字以及key配置到Excel中,显得非常麻烦,所以做了一小工具如下图,配置文字key,点击InsertToExcel按钮,将key以及文字配置到配置表中。

UILocalization.cs脚本如下:

using System; 
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class UILocalization : MonoBehaviour
{
    [SerializeField]
    private string _localizationKey;
    void Start()
    {
        if (string.IsNullOrEmpty(_localizationKey))
        {
            throw new Exception(string.Format("Text:{0} key is null", gameObject.name));
        }
        Text text = GetComponent<Text>();
        //写入给text赋值代码即可
        //text.text = GameEntry.Localization.GetString(_localizationKey);
    }
}

UILocalizationInspector.cs脚本如下:

using System.Diagnostics;
using UnityEditor;
using UnityEngine;
using System.IO;
using UnityEngine.UI;

[CustomEditor(typeof(UILocalization))]
public class UILocalizationInspector : UnityEditor.Editor
{
    private SerializedObject _object;
    private SerializedProperty _languageKey;
    private Text _textLanguage;

    public void OnEnable()
    {
        _object = new SerializedObject(target);
        _languageKey = _object.FindProperty("_localizationKey");
        _textLanguage = ((_object.targetObject) as UILocalization).GetComponent<Text>();
    }

    public override void OnInspectorGUI()
    {
        _object.Update();
        EditorGUILayout.PropertyField(_languageKey);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Localization Value", GUILayout.MaxWidth(190));
        _textLanguage.text = EditorGUILayout.TextArea(_textLanguage.text);
        EditorGUILayout.EndHorizontal();
        if (GUILayout.Button("InsertToExcel"))
        {
            //配置Excel路径
            string languagePath = @"C:UsersAdministratorDesktopStarForce-masterExcelsLocalization.xlsx";
            if (string.IsNullOrEmpty(_languageKey.stringValue))
            {
                EditorUtility.DisplayDialog("插入信息失败", "key值不可为空,信息插入失败", "ok");
                return;
            }
            if (_isFileUse(languagePath))
            {
                EditorUtility.DisplayDialog("插入信息失败", string.Format("{0}文件被占用,信息插入失败", languagePath), "ok");
                return;
            }
            //key列索引
            string keyCol = "1";
            string keyInfo = _languageKey.stringValue;
            //value列索引
            string valueCol = "2";
            string valueInfo = _textLanguage.text;
            Process process = new Process();
            process.StartInfo.FileName = Application.dataPath + "/../Tools/InsertStringToExcel/WriteMsgToExcel.exe";
            process.StartInfo.Arguments = string.Format("{0} {1} {2} {3} {4}", languagePath, keyCol,
                keyInfo, valueCol, valueInfo);
            process.StartInfo.WorkingDirectory = Path.GetDirectoryName(process.StartInfo.FileName);
            process.Start();
        }
        _object.ApplyModifiedProperties();
    }

    private bool _isFileUse(string filePath)
    {
        bool isUse = true;
        StreamWriter sw = null;
        try
        {
            sw = new StreamWriter(filePath, true);
            isUse = false;
        }
        catch
        {
        }
        finally
        {
            if (sw != null)
            {
                sw.Close();
            }
        }
        return isUse;
    }
}

对应的demo在这里(修改excel路径,Editor下操作即可)

推荐阅读