Unity版本:2019.4.5f1兆衅,本文實現(xiàn)的多語言為簡體中文羡亩,繁體中文
前言:
如下圖所示,在PlayerSettings里面只能設(shè)置基礎(chǔ)的應(yīng)用名;
下面記錄我在Unity打包成android時雷袋,實現(xiàn)應(yīng)用名的本地化的過程:
方式一(未成功):在Assets/Plugins/Android/目錄下添加res
首先要了解片排,我們配置的默認(rèn)應(yīng)用名在導(dǎo)成android時會在src→main→res→values目錄下生成strings.xml文件
strings.xml文件內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My Game</string>
</resources>
因此率寡,我們只需要依照android多語言的方式倚搬,在src→main→res目錄下添加:values-zh-rCN,values-zh-rTW(我這里只添加繁體和簡體中文捅僵,更多語言請自行查找android的對應(yīng)語言代碼),并分別創(chuàng)建strings.xml文件上荡,內(nèi)容如下:
- values-zh-rCN
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">我的游戲</string>
</resources>
- values-zh-rTW
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">我的遊戲</string>
</resources>
在unity工程中馒闷,我們只需要Plugins→Android目錄下創(chuàng)建res目錄纳账,其里面內(nèi)容如下:
如此之后打包確并沒有成功疏虫,并有如下警告信息:
OBSOLETE - Providing Android resources in Assets/Plugins/Android/res is deprecated, please move your resources to an AAR or an Android Library.
See "AAR plug-ins and Android Libraries" section of the Manual for more details.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun() (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPlayerWindow.cs:136)
由該警告可知,直接添加 Assets/Plugins/Android/res的方式已經(jīng)被放棄了呢袱,需要打包成aar翅敌,由此引出方式二哼御。
參考鏈接:Unity Android應(yīng)用名稱的國際化
方式二:打包res到aar
這一步需要了解較多android知識,這里簡要描述下:
1.創(chuàng)建android工程看靠,添加module
2.在module的創(chuàng)建values-zh-rCN液肌,values-zh-rTW目錄和語言文件
3.將module打包成aar
4.將aar拷貝到Assets/Plugins/Android/目錄下
我在項目中使用了I2 Localization實現(xiàn)多語言嗦哆,當(dāng)時沒有了解I2 Localization機(jī)制,和其產(chǎn)生了沖突粥喜,導(dǎo)致部分語言未生效
方式三:在Assets/Plugins/Android/目錄下添加res橘券,同時在 build.gradle使用task
1.添加res目錄(過程同方法一)
2.在Bulid Settings → Player Settings → Android → Publishing Settings 中勾選Custom Launcher Gradle Templ:
3.在生成的launcherTemplate.gradle文件末尾添加如下代碼(使用task在編譯時將前面添加的res目錄復(fù)制到正確的地址):
task localizeAppName(type: Copy) {
from("${project.rootDir}/unityLibrary/unity-android-resources/res/") {
include "**"
}
into "${project.projectDir}/src/main/res/"
}
preBuild.dependsOn(localizeAppName)
同樣由于使用了I2 Localization锋华,導(dǎo)致部分語言未生效,這里可以通過類似如下的代碼刪除I2 Localization生成的strings.xml文件:
注意: 1.我這里只針對自己項目移除了簡體中文和繁體中文
?????????2.如果strings.xml未能成功移除衍腥,在需要將工程導(dǎo)出成Android項目纳猫,找到正確的文件路徑進(jìn)行移除
/* 刪除自動生成的多余values-zh-rCN */
task appNameDeleteCN(type: Delete) {
delete "${project.projectDir}/src/main/res/values-zh-rCN/strings.xml"
delete "${project.projectDir}/src/main/res/values-zh-rTW/strings.xml"
}
preBuild.dependsOn(appNameDeleteCN)
參考鏈接:Unity2018 設(shè)置 Android 程序名稱的多語言
方式四:參考I2 Localization续担,在unity中動態(tài)生成strings.xml
這里不多贅述了活孩,直接貼出其PostProcessBuild_Android類
#if UNITY_ANDROID
using UnityEditor.Callbacks;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace I2.Loc
{
public class PostProcessBuild_Android
{
// Post Process Scene is a hack, because using PostProcessBuild will be called after the APK is generated, and so, I didn't find a way to copy the new files
[PostProcessScene]
public static void OnPostProcessScene()
{
#if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
bool isFirstScene = (EditorBuildSettings.scenes.Length>0 && EditorBuildSettings.scenes[0].path == EditorApplication.currentScene);
#else
bool isFirstScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex <= 0;
#endif
if (!EditorApplication.isPlayingOrWillChangePlaymode &&
(EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) &&
isFirstScene)
{
string projPath = System.IO.Path.GetFullPath(Application.streamingAssetsPath + "/../../Temp/StagingArea");
//string projPath = System.IO.Path.GetFullPath(Application.dataPath+ "/Plugins/Android");
PostProcessAndroid(BuildTarget.Android, projPath);
}
}
//[PostProcessBuild(10000)]
public static void PostProcessAndroid(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget!=BuildTarget.Android)
return;
if (LocalizationManager.Sources.Count <= 0)
LocalizationManager.UpdateSources();
// Get language with variants, but also add it without the variant to allow fallbacks (e.g. en-CA also adds en)
var langCodes = LocalizationManager.GetAllLanguagesCode(false).Concat( LocalizationManager.GetAllLanguagesCode(true) ).Distinct().ToList();
if (langCodes.Count <= 0)
return;
string stringXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<resources>\n"+
" <string name=\"app_name\">{0}</string>\n"+
"</resources>";
SetStringsFile( pathToBuiltProject+"/res/values", "strings.xml", stringXML, LocalizationManager.GetAppName(langCodes[0]) );
var list = new List<string>();
list.Add( pathToBuiltProject + "/res/values" );
foreach (var code in langCodes)
{
// Android doesn't use zh-CN or zh-TW, instead it uses: zh-rCN, zh-rTW, zh
string fixedCode = code;
if (fixedCode.StartsWith("zh", System.StringComparison.OrdinalIgnoreCase))
{
string googleCode = GoogleLanguages.GetGoogleLanguageCode(fixedCode);
if (googleCode==null) googleCode = fixedCode;
fixedCode = (googleCode == "zh-CN") ? "zh-CN" : googleCode;
}
fixedCode = fixedCode.Replace("-", "-r");
string dir = pathToBuiltProject + "/res/values-" + fixedCode;
SetStringsFile( dir, "strings.xml", stringXML, LocalizationManager.GetAppName(code) );
}
}
static void CreateFileIfNeeded ( string folder, string fileName, string text )
{
try
{
if (!System.IO.Directory.Exists( folder ))
System.IO.Directory.CreateDirectory( folder );
if (!System.IO.File.Exists( folder + "/"+fileName ))
System.IO.File.WriteAllText( folder + "/"+fileName, text );
}
catch (System.Exception e)
{
Debug.Log( e );
}
}
static void SetStringsFile(string folder, string fileName, string stringXML, string appName)
{
try
{
appName = appName.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", "\\\"").Replace("'", "\\'");
appName = appName.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty);
if (!System.IO.Directory.Exists(folder))
System.IO.Directory.CreateDirectory(folder);
if (!System.IO.File.Exists(folder + "/" + fileName))
{
// create the string file if it doesn't exist
stringXML = string.Format(stringXML, appName);
}
else
{
stringXML = System.IO.File.ReadAllText(folder + "/" + fileName);
// find app_name
var pattern = "\"app_name\">(.*)<\\/string>";
var regexPattern = new System.Text.RegularExpressions.Regex(pattern);
if (regexPattern.IsMatch(stringXML))
{
// Override the AppName if it was found
stringXML = regexPattern.Replace(stringXML, string.Format("\"app_name\">{0}</string>", appName));
}
else
{
// insert the appName if it wasn't there
int idx = stringXML.IndexOf("<resources>");
if (idx > 0)
stringXML = stringXML.Insert(idx + "</resources>".Length, string.Format("\n <string name=\"app_name\">{0}</string>\n", appName));
}
}
System.IO.File.WriteAllText(folder + "/" + fileName, stringXML);
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
}
}
#endif