| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using System.Collections.Generic;
- using System.IO;
- using System.Text.RegularExpressions;
- using UnityEditor;
- using UnityEditor.Build;
- using UnityEngine;
- public class MenuChangeAppLanguage
- {
- enum LanguageType { CN = 0, EN = 1 }
- enum DeviceType { iPhone, iPad }
- static void RefreshMenuChecked()
- {
- int lang = CommonConfig.AppLanguage; //EditorPrefs.GetInt("AppLanguage", 0);
- string iosTarget = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
- Menu.SetChecked("APP语言版本切换/中文/iPhone", lang == 0 && iosTarget.Contains("BowArrow") && !iosTarget.Contains("Pad"));
- Menu.SetChecked("APP语言版本切换/中文/iPad", lang == 0 && iosTarget.Contains("PadBowArrow"));
- Menu.SetChecked("APP语言版本切换/海外/iPhone", lang == 1 && iosTarget.Contains("BowArrowEn") && !iosTarget.Contains("Pad"));
- Menu.SetChecked("APP语言版本切换/海外/iPad", lang == 1 && iosTarget.Contains("PadBowArrowEn"));
- }
- [MenuItem("APP语言版本切换/中文/iPhone")]
- static void CN_iPhone() => ApplySetting(LanguageType.CN, DeviceType.iPhone);
- [MenuItem("APP语言版本切换/中文/iPhone", true)]
- static bool CN_iPhone_Validate() { RefreshMenuChecked(); return true; }
- [MenuItem("APP语言版本切换/中文/iPad")]
- static void CN_iPad() => ApplySetting(LanguageType.CN, DeviceType.iPad);
- [MenuItem("APP语言版本切换/中文/iPad", true)]
- static bool CN_iPad_Validate() { RefreshMenuChecked(); return true; }
- [MenuItem("APP语言版本切换/海外/iPhone")]
- static void EN_iPhone() => ApplySetting(LanguageType.EN, DeviceType.iPhone);
- [MenuItem("APP语言版本切换/海外/iPhone", true)]
- static bool EN_iPhone_Validate() { RefreshMenuChecked(); return true; }
- [MenuItem("APP语言版本切换/海外/iPad")]
- static void EN_iPad() => ApplySetting(LanguageType.EN, DeviceType.iPad);
- [MenuItem("APP语言版本切换/海外/iPad", true)]
- static bool EN_iPad_Validate() { RefreshMenuChecked(); return true; }
- static void ApplySetting(LanguageType lang, DeviceType device)
- {
- int langId = (int)lang;
- // EditorPrefs.SetInt("AppLanguage", langId);
- if (CommonConfig.bInfraredApp)
- {
- PlayerSettings.productName = CommonConfig.StandaloneModeOrPlatformB
- ? CommonConfig.AppNames[2]
- : CommonConfig.AppNames[langId];
- }
- else
- {
- PlayerSettings.productName = CommonConfig.StandaloneModeOrPlatformB
- ? CommonConfig.AppNames[2]
- : CommonConfig.AppNames9Axis[langId];
- }
- #if UNITY_IOS
- string bundleId = lang switch
- {
- LanguageType.CN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrow" : "com.xmjssvr.PadBowArrow",
- LanguageType.EN => device == DeviceType.iPhone ? "com.xmjssvr.BowArrowEn" : "com.xmjssvr.PadBowArrowEn",
- _ => "com.xmjssvr.BowArrow"
- };
- PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, bundleId);
- //现在iphone模式需要上传ipad
- PlayerSettings.iOS.targetDevice = device == DeviceType.iPhone ? iOSTargetDevice.iPhoneAndiPad : iOSTargetDevice.iPadOnly;
- PlayerSettings.iOS.appleDeveloperTeamID = "WR7WD35237";
- PlayerSettings.iOS.appleEnableAutomaticSigning = true;
- #endif
- #if UNITY_ANDROID
- string androidId = lang switch
- {
- LanguageType.CN => CommonConfig.StandaloneModeOrPlatformB
- ? "com.xmjssvr.BowArrow.miBClient"
- : "com.xmjssvr.BowArrow.mi",
- LanguageType.EN => CommonConfig.StandaloneModeOrPlatformB
- ? "com.xmjssvr.BowArrowEn.BClient"
- : "com.xmjssvr.BowArrowEn",
- _ => "com.xmjssvr.BowArrow"
- };
- PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, androidId);
- #endif
- SetIosAppIcon(langId, device);
- }
- static void SetIosAppIcon(int langId, DeviceType device)
- {
- //string path = langId == 1
- // ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
- // : "Assets/BowArrow/Textures/Common/AppIcon2.png";
- string path;
- if (device == DeviceType.iPad)
- {
- // iPad版,选用 Pad 专用图标
- path = langId == 1
- ? "Assets/BowArrow/Textures/Common/AppIconPadEN.png"
- : "Assets/BowArrow/Textures/Common/AppIconPad.png";
- }
- else
- {
- // 普通版
- path = langId == 1
- ? "Assets/BowArrow/Textures/Common/AppIconEN.png"
- : "Assets/BowArrow/Textures/Common/AppIcon2.png";
- }
- Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
- if (texture == null)
- {
- Debug.LogWarning("图标文件未找到:" + path);
- return;
- }
- Texture2D[] icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
- for (int i = 0; i < icons.Length; i++) icons[i] = texture;
- PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Unknown, icons);
- AssetDatabase.SaveAssets();
- #if UNITY_IOS
- if (device == DeviceType.iPhone)
- ApplyAllIosIcons(langId);
- else
- ClearIosIcons();
- #endif
- }
- private static readonly string iconPathCN = "Assets/BowArrow/Textures/Common/AppIconsCN"; // iOS中文图标文件夹
- private static readonly string iconPathEN = "Assets/BowArrow/Textures/Common/AppIconsEN"; // iOS英文图标文件夹
- /// <summary>
- /// 批量导入 iOS 所有图标
- /// </summary>
- public static void ApplyAllIosIcons(int langId)
- {
- string iconPath = langId == 1 ? iconPathEN : iconPathCN;
- // ✅ 收集文件
- string[] files = Directory.GetFiles(iconPath, "*.png", SearchOption.AllDirectories);
- if (files.Length == 0)
- {
- Debug.LogError($"未找到图标文件: {iconPath}");
- return;
- }
- // ✅ 解析像素尺寸
- Dictionary<int, Texture2D> pixelMap = new();
- foreach (string file in files)
- {
- Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(file);
- if (tex == null) continue;
- if (TryParseIconPixel(file, out int pixels))
- {
- pixelMap[pixels] = tex;
- Debug.Log($"识别图标: {Path.GetFileName(file)} → {pixels}px");
- }
- }
- #if UNITY_2022_1_OR_NEWER
- var target = NamedBuildTarget.iOS;
- var iconKinds = PlayerSettings.GetSupportedIconKinds(target);
- foreach (var kind in iconKinds)
- {
- var icons = PlayerSettings.GetPlatformIcons(target, kind);
-
- // ✅ 清空旧图标
- for (int i = 0; i < icons.Length; i++)
- icons[i].SetTexture(null);
- for (int i = 0; i < icons.Length; i++)
- {
- int size = Mathf.RoundToInt(icons[i].width);
- if (pixelMap.TryGetValue(size, out var tex))
- {
- icons[i].SetTexture(tex);
- Debug.Log($"✅ [2022+] 设置 {kind} 图标 {size}px");
- }
- }
- PlayerSettings.SetPlatformIcons(target, kind, icons);
- }
- #else
- var group = BuildTargetGroup.iOS;
- var iconKinds = PlayerSettings.GetSupportedIconKindsForPlatform(group);
- foreach (var kind in iconKinds)
- {
- var icons = PlayerSettings.GetPlatformIcons(group, kind);
- // ✅ 清空旧图标
- for (int i = 0; i < icons.Length; i++)
- icons[i].SetTexture(null);
- for (int i = 0; i < icons.Length; i++)
- {
- int size = Mathf.RoundToInt(icons[i].width);
- if (pixelMap.TryGetValue(size, out var tex))
- {
- icons[i].SetTexture(tex);
- Debug.Log($"✅ [2021] 设置 {kind} 图标 {size}px");
- }
- }
- PlayerSettings.SetPlatformIcons(group, kind, icons);
- }
- #endif
- AssetDatabase.SaveAssets();
- Debug.Log("🎯 iOS App Icons 全部导入完成!");
- }
- public static void ClearIosIcons()
- {
- #if UNITY_2022_1_OR_NEWER
- var target = NamedBuildTarget.iOS;
- var iconKinds = PlayerSettings.GetSupportedIconKinds(target);
- foreach (var kind in iconKinds)
- {
- var icons = PlayerSettings.GetPlatformIcons(target, kind);
-
- // ✅ 清空旧图标
- for (int i = 0; i < icons.Length; i++)
- icons[i].SetTexture(null);
- PlayerSettings.SetPlatformIcons(target, kind, icons);
- }
- #else
- var group = BuildTargetGroup.iOS;
- var iconKinds = PlayerSettings.GetSupportedIconKindsForPlatform(group);
- foreach (var kind in iconKinds)
- {
- var icons = PlayerSettings.GetPlatformIcons(group, kind);
- // ✅ 清空旧图标
- for (int i = 0; i < icons.Length; i++)
- icons[i].SetTexture(null);
- PlayerSettings.SetPlatformIcons(group, kind, icons);
- }
- #endif
- AssetDatabase.SaveAssets();
- Debug.Log("🎯 iOS App Icons 全部清空!");
- }
- /// <summary>
- /// 支持多种命名方式的像素解析
- /// </summary>
- private static bool TryParseIconPixel(string file, out int pixels)
- {
- pixels = 0;
- string name = Path.GetFileNameWithoutExtension(file);
- // ① 匹配 iPhoneApp_60pt@3x → 180px
- Match m = Regex.Match(name, @"(\d+(?:\.\d+)?)pt@(\d+)x");
- if (m.Success)
- {
- float pt = float.Parse(m.Groups[1].Value);
- int scale = int.Parse(m.Groups[2].Value);
- pixels = Mathf.RoundToInt(pt * scale);
- return true;
- }
- // ② 匹配 store_1024pt 或 icon_60pt(无倍数,pt=px)
- m = Regex.Match(name, @"(\d+)pt");
- if (m.Success)
- {
- pixels = int.Parse(m.Groups[1].Value);
- return true;
- }
- // ③ 匹配直接像素命名 icon_180x180
- m = Regex.Match(name, @"(\d+)x\d+");
- if (m.Success)
- {
- pixels = int.Parse(m.Groups[1].Value);
- return true;
- }
- // ④ 特殊情况:AppStore_1024.png
- m = Regex.Match(name, @"1024");
- if (m.Success)
- {
- pixels = 1024;
- return true;
- }
- return false;
- }
- /// <summary>
- /// 从文件名中提取像素尺寸,如 "iPhoneApp_60pt@3x" → 180px
- /// </summary>
- //private static bool TryParseIconPixel(string file, out int pixels)
- //{
- // pixels = 0;
- // string name = Path.GetFileNameWithoutExtension(file);
- // Match m = Regex.Match(name, @"(\d+(?:\.\d+)?)pt@(\d+)x");
- // if (m.Success)
- // {
- // float pt = float.Parse(m.Groups[1].Value);
- // int scale = int.Parse(m.Groups[2].Value);
- // pixels = Mathf.RoundToInt(pt * scale);
- // return true;
- // }
- // // 匹配直接像素命名: Icon_120x120.png
- // m = Regex.Match(name, @"(\d+)x\d+");
- // if (m.Success)
- // {
- // pixels = int.Parse(m.Groups[1].Value);
- // return true;
- // }
- // return false;
- //}
- }
|