G 示例:本地化模块一般会用到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<Dictionaries>
<Dictionary Language="ChineseSimplified">
<String Key="Game.Name" Value="星际力量" />
<String Key="Game.Description" Value="Game Framework 演示程序" />
<String Key="Game.Website" Value="https://GameFramework.cn/" />
<String Key="Game.Summary" Value="这是一个使用 Game Framework 游戏框架制作的游戏演示项目,目的是对此游戏框架的使用方法做一些示范,供使用者参考。" />
<String Key="Game.Introduction" Value="Game Framework 是基于 Unity 引擎的游戏框架,主要对游戏开发过程中常用模块进行了封装,很大程度地规范开发过程、加快开发速度并保证产品质量。" />
<String Key="Menu.StartButton" Value="开始" />
<String Key="Menu.SettingButton" Value="设置" />
<String Key="Menu.AboutButton" Value="关于" />
<String Key="Menu.QuitButton" Value="退出" />
<String Key="Dialog.ConfirmButton" Value="确定" />
<String Key="Dialog.CancelButton" Value="取消" />
<String Key="Dialog.OtherButton" Value="" />
<String Key="ForceUpdate.Title" Value="强制更新" />
<String Key="ForceUpdate.Message" Value="当前游戏版本过旧,是否前往更新?" />
<String Key="ForceUpdate.UpdateButton" Value="更新" />
<String Key="ForceUpdate.QuitButton" Value="退出" />
<String Key="AskQuitGame.Title" Value="确认退出" />
<String Key="AskQuitGame.Message" Value="是否确认退出游戏?" />
<String Key="Setting.Title" Value="设置" />
<String Key="Setting.Music" Value="音乐" />
<String Key="Setting.Sound" Value="音效" />
<String Key="Setting.UISound" Value="界面音效" />
<String Key="Setting.Language" Value="语言" />
<String Key="Setting.LanguageTips" Value="更改语言需要重启游戏" />
</Dictionary>
</Dictionaries>

# 代码:

继承 DefaultLocalizationHelper 类,然后重写 ParseData 方法即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using GameFramework.Localization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace FurryGF
{
/// <summary>
/// XML格式本地化辅助器
/// </summary>
public class XmlLocalizationHelper : DefaultLocalizationHelper
{
/// <summary>
/// 解析字典。
/// </summary>
/// <param name="dictionaryString">要解析的字典字符串。</param>
/// <param name="userData">用户自定义数据。</param>
/// <returns>是否解析字典成功。</returns>
public override bool ParseData(ILocalizationManager localizationManager, string dictionaryString, object userData)
{
try
{
string currentLaunguage = GameEntry.Localization.Language.ToString();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(dictionaryString);
XmlNode xmlRoot = xmlDocument.SelectSingleNode("Dictionaries");
XmlNodeList xmlNodedictionaryList = xmlRoot.ChildNodes;
for (int i = 0; i < xmlNodedictionaryList.Count; ++i)
{
XmlNode xmlNodeDictionary = xmlNodedictionaryList.Item(i);
if (xmlNodeDictionary.Name != "Dictionary")
{
continue;
}
string language = xmlNodeDictionary.Attributes.GetNamedItem("Language").Value;
if (language != currentLaunguage)
{
continue;
}
XmlNodeList xmlNodeStringList = xmlNodeDictionary.ChildNodes;
for (int j = 0; j < xmlNodeStringList.Count; ++j)
{
XmlNode xmlNodeString = xmlNodeStringList.Item(j);
if (xmlNodeString.Name != "String")
{
continue;
}
string key = xmlNodeString.Attributes.GetNamedItem("Key").Value;
string value = xmlNodeString.Attributes.GetNamedItem("Value").Value;
if (!localizationManager.AddRawString(key, value))
{
Log.Warning("本地化中,加入key失败 '{0}' 键值可能是重复了.", key);
return false;
}
}
}
return true;
}
catch (Exception exception)
{
Log.Warning("Can not parse dictionary data with exception '{0}'.", exception.ToString());
return false;
}
}
}
}

更新于

请我喝[茶]~( ̄▽ ̄)~*

Solvarg 微信支付

微信支付

Solvarg 支付宝

支付宝

Solvarg 贝宝

贝宝