PostProcessBuild_ANDROID.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if UNITY_ANDROID
  2. using UnityEditor.Callbacks;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace I2.Loc
  8. {
  9. public class PostProcessBuild_Android
  10. {
  11. // 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
  12. [PostProcessScene]
  13. public static void OnPostProcessScene()
  14. {
  15. #if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
  16. bool isFirstScene = (EditorBuildSettings.scenes.Length>0 && EditorBuildSettings.scenes[0].path == EditorApplication.currentScene);
  17. #else
  18. bool isFirstScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex <= 0;
  19. #endif
  20. if (!EditorApplication.isPlayingOrWillChangePlaymode &&
  21. (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) &&
  22. isFirstScene)
  23. {
  24. string projPath = System.IO.Path.GetFullPath(Application.streamingAssetsPath + "/../../Temp/StagingArea");
  25. //string projPath = System.IO.Path.GetFullPath(Application.dataPath+ "/Plugins/Android");
  26. PostProcessAndroid(BuildTarget.Android, projPath);
  27. }
  28. }
  29. //[PostProcessBuild(10000)]
  30. public static void PostProcessAndroid(BuildTarget buildTarget, string pathToBuiltProject)
  31. {
  32. if (buildTarget!=BuildTarget.Android)
  33. return;
  34. if (LocalizationManager.Sources.Count <= 0)
  35. LocalizationManager.UpdateSources();
  36. // Get language with variants, but also add it without the variant to allow fallbacks (e.g. en-CA also adds en)
  37. var langCodes = LocalizationManager.GetAllLanguagesCode(false).Concat( LocalizationManager.GetAllLanguagesCode(true) ).Distinct().ToList();
  38. if (langCodes.Count <= 0)
  39. return;
  40. string stringXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
  41. "<resources>\n"+
  42. " <string name=\"app_name\">{0}</string>\n"+
  43. "</resources>";
  44. SetStringsFile( pathToBuiltProject+"/res/values", "strings.xml", stringXML, LocalizationManager.GetAppName(langCodes[0]) );
  45. var list = new List<string>();
  46. list.Add( pathToBuiltProject + "/res/values" );
  47. foreach (var code in langCodes)
  48. {
  49. // Android doesn't use zh-CN or zh-TW, instead it uses: zh-rCN, zh-rTW, zh
  50. string fixedCode = code;
  51. if (fixedCode.StartsWith("zh", System.StringComparison.OrdinalIgnoreCase))
  52. {
  53. string googleCode = GoogleLanguages.GetGoogleLanguageCode(fixedCode);
  54. if (googleCode==null) googleCode = fixedCode;
  55. fixedCode = (googleCode == "zh-CN") ? "zh-CN" : googleCode;
  56. }
  57. fixedCode = fixedCode.Replace("-", "-r");
  58. string dir = pathToBuiltProject + "/res/values-" + fixedCode;
  59. SetStringsFile( dir, "strings.xml", stringXML, LocalizationManager.GetAppName(code) );
  60. }
  61. }
  62. static void CreateFileIfNeeded ( string folder, string fileName, string text )
  63. {
  64. try
  65. {
  66. if (!System.IO.Directory.Exists( folder ))
  67. System.IO.Directory.CreateDirectory( folder );
  68. if (!System.IO.File.Exists( folder + "/"+fileName ))
  69. System.IO.File.WriteAllText( folder + "/"+fileName, text );
  70. }
  71. catch (System.Exception e)
  72. {
  73. Debug.Log( e );
  74. }
  75. }
  76. static void SetStringsFile(string folder, string fileName, string stringXML, string appName)
  77. {
  78. try
  79. {
  80. appName = appName.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "\\\"").Replace("'", "\\'");
  81. appName = appName.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty);
  82. if (!System.IO.Directory.Exists(folder))
  83. System.IO.Directory.CreateDirectory(folder);
  84. if (!System.IO.File.Exists(folder + "/" + fileName))
  85. {
  86. // create the string file if it doesn't exist
  87. stringXML = string.Format(stringXML, appName);
  88. }
  89. else
  90. {
  91. stringXML = System.IO.File.ReadAllText(folder + "/" + fileName);
  92. // find app_name
  93. var pattern = "\"app_name\">(.*)<\\/string>";
  94. var regexPattern = new System.Text.RegularExpressions.Regex(pattern);
  95. if (regexPattern.IsMatch(stringXML))
  96. {
  97. // Override the AppName if it was found
  98. stringXML = regexPattern.Replace(stringXML, string.Format("\"app_name\">{0}</string>", appName));
  99. }
  100. else
  101. {
  102. // insert the appName if it wasn't there
  103. int idx = stringXML.IndexOf("<resources>");
  104. if (idx > 0)
  105. stringXML = stringXML.Insert(idx + "</resources>".Length, string.Format("\n <string name=\"app_name\">{0}</string>\n", appName));
  106. }
  107. }
  108. System.IO.File.WriteAllText(folder + "/" + fileName, stringXML);
  109. }
  110. catch (System.Exception e)
  111. {
  112. Debug.Log(e);
  113. }
  114. }
  115. }
  116. }
  117. #endif