MenuChangeAppLanguage.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. PlayerSettings.productName = CommonConfig.StandaloneModeOrPlatformB
  37. ? CommonConfig.AppNames[2]
  38. : CommonConfig.AppNames[langId];
  39. #if UNITY_IOS
  40. string bundleId = lang switch
  41. {
  42. LanguageType.CN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrow" : "com.xmjssvr.PadBowArrow",
  43. LanguageType.EN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrowEn" : "com.xmjssvr.PadBowArrowEn",
  44. _ => "com.xmjssvr.BowArrow"
  45. };
  46. PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, bundleId);
  47. PlayerSettings.iOS.targetDevice = device == DeviceType.iPhone ? iOSTargetDevice.iPhoneOnly : iOSTargetDevice.iPadOnly;
  48. PlayerSettings.iOS.appleDeveloperTeamID = "WR7WD35237";
  49. PlayerSettings.iOS.appleEnableAutomaticSigning = true;
  50. #endif
  51. #if UNITY_ANDROID
  52. string androidId = lang switch
  53. {
  54. LanguageType.CN => CommonConfig.StandaloneModeOrPlatformB
  55. ? "com.xmjssvr.BowArrow.miBClient"
  56. : "com.xmjssvr.BowArrow.mi",
  57. LanguageType.EN => CommonConfig.StandaloneModeOrPlatformB
  58. ? "com.xmjssvr.BowArrowEn.BClient"
  59. : "com.xmjssvr.BowArrowEn",
  60. _ => "com.xmjssvr.BowArrow"
  61. };
  62. PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, androidId);
  63. #endif
  64. SetIosAppIcon(langId, device);
  65. }
  66. static void SetIosAppIcon(int langId, DeviceType device)
  67. {
  68. //string path = langId == 1
  69. // ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
  70. // : "Assets/BowArrow/Textures/Common/AppIcon2.png";
  71. string path;
  72. if (device == DeviceType.iPad)
  73. {
  74. // iPad版,选用 Pad 专用图标
  75. path = langId == 1
  76. ? "Assets/BowArrow/Textures/Common/AppIconPadEN.png"
  77. : "Assets/BowArrow/Textures/Common/AppIconPad.png";
  78. }
  79. else
  80. {
  81. // 普通版
  82. path = langId == 1
  83. ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
  84. : "Assets/BowArrow/Textures/Common/AppIcon2.png";
  85. }
  86. Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  87. if (texture == null)
  88. {
  89. Debug.LogWarning("图标文件未找到:" + path);
  90. return;
  91. }
  92. Texture2D[] icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
  93. for (int i = 0; i < icons.Length; i++) icons[i] = texture;
  94. PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Unknown, icons);
  95. AssetDatabase.SaveAssets();
  96. }
  97. }