killapp/Assets/Editor/iOSLocalizedPermissionPostProcessor.cs
yuqucheng 9316f8f331 feat(iOS): 完善iOS平台WiFi配对与权限本地化适配
1. 新增iOSWiFiHelper类实现异步获取WiFi SSID,自动请求位置权限
2. 新增iOS权限描述本地化处理器,实现中英文权限文案自动切换
3. 更新所有iOS权限描述为更清晰的英文/中文说明
4. 优化iOS编译后处理脚本优先级,避免被其他插件覆盖
5. 在连接设备页面和WiFi设置页面自动填充当前WiFi名称
6. 更新应用版本号与构建号
2026-08-07 16:14:10 +08:00

91 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#if UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
using System.Text;
using UnityEngine;
/// <summary>
/// iOS 权限描述双语本地化:
/// - Info.plist 写入英文描述作为默认(所有非中文用户看到英文)
/// - 生成 zh-Hans.lproj/InfoPlist.strings中文系统用户自动看到中文
/// 最高优先级执行,确保覆盖其他插件写入的权限描述。
/// </summary>
public class iOSLocalizedPermissionPostProcessor
{
// 权限描述中英文对照
private static readonly (string key, string en, string zhHans)[] PERMISSIONS =
{
("NSCameraUsageDescription",
"Camera access is used to scan the QR code on your device for pairing. For example, scan the QR code on the device body to complete the connection.",
"需要相机权限来扫描设备上的二维码完成配对。例如:扫描设备机身上的二维码后完成连接。"),
("NSLocationWhenInUseUsageDescription",
"Location access is used to detect the current WiFi network so we can display the connected WiFi name.",
"需要位置权限来获取当前 WiFi 信息,用于展示已连接的 WiFi 名称。"),
("NSBluetoothAlwaysUsageDescription",
"Bluetooth is used to scan for and connect to your device. For example, after pairing with the device, we send control commands to it.",
"需要蓝牙权限来搜索并连接你的设备。例如:设备配对成功后向其发送控制指令。"),
("NSBluetoothPeripheralUsageDescription",
"Bluetooth is used to scan for and connect to your device. For example, after pairing with the device, we send control commands to it.",
"需要蓝牙权限来搜索并连接你的设备。例如:设备配对成功后向其发送控制指令。"),
("NSPhotoLibraryUsageDescription",
"Allows you to choose photos from your library to set as your profile picture or attach to feedback reports. For example, select a photo when you tap your avatar in the profile page.",
"允许你从相册选择照片设置头像或上传问题反馈截图。例如:在个人主页点击头像时选择照片进行更换。"),
("NSPhotoLibraryAddUsageDescription",
"Allows saving generated QR code images and downloaded videos to your photo library. For example, save a QR code to your library so you can share it with friends.",
"允许将生成的二维码图片和下载的视频保存到你的相册。例如:将二维码保存到相册后分享给好友。"),
};
[PostProcessBuild(2000)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;
// 1. Info.plist 写入英文描述(默认回退语言)
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
foreach (var p in PERMISSIONS)
{
plist.root.SetString(p.key, p.en);
}
File.WriteAllText(plistPath, plist.WriteToString());
// 2. 生成中文本地化文件 InfoPlist.strings
WriteChineseStringsFile(pathToBuiltProject);
Debug.Log("[iOSLocalizedPermissionPostProcessor] 权限描述已本地化 (默认英文 / 中文自动切换)");
}
private static void WriteChineseStringsFile(string buildPath)
{
string lprojDir = Path.Combine(buildPath, "zh-Hans.lproj");
Directory.CreateDirectory(lprojDir);
var sb = new StringBuilder();
foreach (var p in PERMISSIONS)
{
sb.AppendLine($"\"{p.key}\" = \"{p.zhHans}\";");
}
// 使用带 BOM 的 UTF-8 编码,兼容 Xcode
string filePath = Path.Combine(lprojDir, "InfoPlist.strings");
File.WriteAllText(filePath, sb.ToString(), new UTF8Encoding(true));
// 3. 将 InfoPlist.strings 添加到 Xcode 工程的 Resources 构建阶段
string pbxPath = PBXProject.GetPBXProjectPath(buildPath);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(pbxPath));
string targetGuid = proj.GetUnityMainTargetGuid();
string fileGuid = proj.AddFile(filePath, Path.Combine("zh-Hans.lproj", "InfoPlist.strings"), PBXSourceTree.Source);
proj.AddFileToBuild(targetGuid, fileGuid);
File.WriteAllText(pbxPath, proj.WriteToString());
Debug.Log($"[iOSLocalizedPermissionPostProcessor] 已添加中文本地化: {filePath}");
}
}
#endif