MenuChangeAppLanguage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using UnityEditor;
  2. using UnityEngine;
  3. public class MenuChangeAppLanguage
  4. {
  5. enum LanguageType { CN = 0, EN = 1 }
  6. enum DeviceType { iPhone, iPad }
  7. static void RefreshMenuChecked()
  8. {
  9. int lang = CommonConfig.AppLanguage; //EditorPrefs.GetInt("AppLanguage", 0);
  10. string iosTarget = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
  11. Menu.SetChecked("APP语言版本切换/中文/iPhone", lang == 0 && iosTarget.Contains("BowArrow") && !iosTarget.Contains("Pad"));
  12. Menu.SetChecked("APP语言版本切换/中文/iPad", lang == 0 && iosTarget.Contains("PadBowArrow"));
  13. Menu.SetChecked("APP语言版本切换/英文/iPhone", lang == 1 && iosTarget.Contains("BowArrowEn") && !iosTarget.Contains("Pad"));
  14. Menu.SetChecked("APP语言版本切换/英文/iPad", lang == 1 && iosTarget.Contains("PadBowArrowEn"));
  15. }
  16. [MenuItem("APP语言版本切换/中文/iPhone")]
  17. static void CN_iPhone() => ApplySetting(LanguageType.CN, DeviceType.iPhone);
  18. [MenuItem("APP语言版本切换/中文/iPhone", true)]
  19. static bool CN_iPhone_Validate() { RefreshMenuChecked(); return true; }
  20. [MenuItem("APP语言版本切换/中文/iPad")]
  21. static void CN_iPad() => ApplySetting(LanguageType.CN, DeviceType.iPad);
  22. [MenuItem("APP语言版本切换/中文/iPad", true)]
  23. static bool CN_iPad_Validate() { RefreshMenuChecked(); return true; }
  24. [MenuItem("APP语言版本切换/英文/iPhone")]
  25. static void EN_iPhone() => ApplySetting(LanguageType.EN, DeviceType.iPhone);
  26. [MenuItem("APP语言版本切换/英文/iPhone", true)]
  27. static bool EN_iPhone_Validate() { RefreshMenuChecked(); return true; }
  28. [MenuItem("APP语言版本切换/英文/iPad")]
  29. static void EN_iPad() => ApplySetting(LanguageType.EN, DeviceType.iPad);
  30. [MenuItem("APP语言版本切换/英文/iPad", true)]
  31. static bool EN_iPad_Validate() { RefreshMenuChecked(); return true; }
  32. static void ApplySetting(LanguageType lang, DeviceType device)
  33. {
  34. int langId = (int)lang;
  35. // EditorPrefs.SetInt("AppLanguage", langId);
  36. if (CommonConfig.bInfraredApp)
  37. {
  38. PlayerSettings.productName = CommonConfig.StandaloneModeOrPlatformB
  39. ? CommonConfig.AppNames[2]
  40. : CommonConfig.AppNames[langId];
  41. }
  42. else
  43. {
  44. PlayerSettings.productName = CommonConfig.StandaloneModeOrPlatformB
  45. ? CommonConfig.AppNames[2]
  46. : CommonConfig.AppNames9Axis[langId];
  47. }
  48. #if UNITY_IOS
  49. string bundleId = lang switch
  50. {
  51. LanguageType.CN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrow" : "com.xmjssvr.PadBowArrow",
  52. LanguageType.EN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrowEn" : "com.xmjssvr.PadBowArrowEn",
  53. _ => "com.xmjssvr.BowArrow"
  54. };
  55. PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, bundleId);
  56. PlayerSettings.iOS.targetDevice = device == DeviceType.iPhone ? iOSTargetDevice.iPhoneOnly : iOSTargetDevice.iPadOnly;
  57. PlayerSettings.iOS.appleDeveloperTeamID = "WR7WD35237";
  58. PlayerSettings.iOS.appleEnableAutomaticSigning = true;
  59. #endif
  60. #if UNITY_ANDROID
  61. string androidId = lang switch
  62. {
  63. LanguageType.CN => CommonConfig.StandaloneModeOrPlatformB
  64. ? "com.xmjssvr.BowArrow.miBClient"
  65. : "com.xmjssvr.BowArrow.mi",
  66. LanguageType.EN => CommonConfig.StandaloneModeOrPlatformB
  67. ? "com.xmjssvr.BowArrowEn.BClient"
  68. : "com.xmjssvr.BowArrowEn",
  69. _ => "com.xmjssvr.BowArrow"
  70. };
  71. PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, androidId);
  72. #endif
  73. SetIosAppIcon(langId, device);
  74. }
  75. static void SetIosAppIcon(int langId, DeviceType device)
  76. {
  77. //string path = langId == 1
  78. // ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
  79. // : "Assets/BowArrow/Textures/Common/AppIcon2.png";
  80. string path;
  81. if (device == DeviceType.iPad)
  82. {
  83. // iPad版,选用 Pad 专用图标
  84. path = langId == 1
  85. ? "Assets/BowArrow/Textures/Common/AppIconPadEN.png"
  86. : "Assets/BowArrow/Textures/Common/AppIconPad.png";
  87. }
  88. else
  89. {
  90. // 普通版
  91. path = langId == 1
  92. ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
  93. : "Assets/BowArrow/Textures/Common/AppIcon2.png";
  94. }
  95. Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  96. if (texture == null)
  97. {
  98. Debug.LogWarning("图标文件未找到:" + path);
  99. return;
  100. }
  101. Texture2D[] icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
  102. for (int i = 0; i < icons.Length; i++) icons[i] = texture;
  103. PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Unknown, icons);
  104. AssetDatabase.SaveAssets();
  105. }
  106. }