緣由:因為公司2 3D 共用一套iOS底層框架 一直以來都用拷貝的方法 取所需要的代碼 導(dǎo)致一個問題 iOS底層拷貝過來的xxx.strings
本地化文件 一直無法被編譯 即使被編譯 也無法達到效果 導(dǎo)致一系列問題
現(xiàn)在只需要把語言文件放在unity項目中就好了 如 Assets/babyframework/localizationForiOS
直接拷貝效果
采用插件導(dǎo)出后效果
粘貼關(guān)鍵代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.iOS.Xcode;
public class NativeLocale
{
public static void AddLocalizedStringsIOS(string projectPath, string localizedDirectoryPath)
{
DirectoryInfo dir = new DirectoryInfo(localizedDirectoryPath);
if(!dir.Exists)
return;
List<string> locales = new List<string>();
var localeDirs = dir.GetDirectories("*.lproj", SearchOption.TopDirectoryOnly);
foreach(var sub in localeDirs)
locales.Add(Path.GetFileNameWithoutExtension(sub.Name));
AddLocalizedStringsIOS(projectPath, localizedDirectoryPath, locales);
}
public static void AddLocalizedStringsIOS(string projectPath, string localizedDirectoryPath, List<string> validLocales)
{
string projPath = projectPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);
foreach(var locale in validLocales)
{
// copy contents in the localization directory to project directory
string src = Path.Combine(localizedDirectoryPath, locale + ".lproj");
DirectoryCopy(src, Path.Combine(projectPath, "Unity-iPhone/" + locale + ".lproj"));
string fileRelatvePath = string.Format("Unity-iPhone/{0}.lproj/Localizable.strings", locale);
proj.AddLocalization("Localizable.strings", locale, fileRelatvePath);
}
proj.WriteToFile(projPath);
}
private static void DirectoryCopy(string sourceDirName, string destDirName)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
return;
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// skip unity meta files
if(file.FullName.EndsWith(".meta"))
continue;
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath);
}
}
}
調(diào)用
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using UnityEditor.iOS.Xcode;
public class PostProcess: MonoBehaviour
{
[PostProcessBuildAttribute(9999)]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
OnIOSBuild(target, path);
}
}
private static void OnIOSBuild(BuildTarget target, string path)
{
NativeLocale.AddLocalizedStringsIOS(path, Path.Combine(Application.dataPath, "BabyframeWork/localizationForiOS"));
}
}
文件路徑如下
效果
項目需要用到unity PBXProject插件 不知道的自行Google
轉(zhuǎn)
參考的同性交友網(wǎng)站地址
https://github.com/zeyangl/UnityAppNameLocalizationForIOS