You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS设备截图无法存储:PC正常代码在iOS失效且无权限提示

iOS端Unity截图失效无权限弹窗问题解决

核心原因分析

  • 权限配置缺失:iOS要求访问相册必须在Info.plist中添加权限描述,否则系统不会触发权限弹窗,即使代码逻辑正确也无法访问相册。
  • 存储路径误解:你的两段代码都是将截图写入沙盒目录(Application.persistentDataPath),这个路径属于App私有空间,不需要相册权限,但用户无法直接在系统相册看到该文件,容易误以为代码未生效;如果要让截图出现在系统相册,需要额外调用相册写入接口。

具体修复步骤

1. 补充iOS权限配置

打开Unity项目的Player Settings → iOS → Other Settings → Configuration,在Info.plist中添加以下权限描述(根据需求选择):

  • Privacy - Photo Library Additions Usage Description:填写App需要写入相册的原因,例如"保存游戏截图到您的相册"
  • Privacy - Photo Library Usage Description:若有读取相册需求,填写对应原因

2. 调整代码:将截图保存到系统相册

如果需要截图出现在iOS相册中,仅写入沙盒是不够的,需调用相册相关接口,以下提供两种方案:

方案一:使用NativeGallery插件(简易实现)

public IEnumerator CaptureAndSaveToAlbum()
{
    yield return new WaitForEndOfFrame();
    // 捕获屏幕截图
    Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    screenshot.Apply();

    byte[] pngBytes = screenshot.EncodeToPNG();
    Destroy(screenshot);

    // 保存到系统相册
    NativeGallery.SaveImageToGallery(pngBytes, "游戏截图", $"截图_{System.DateTime.Now:yyyyMMddHHmmss}.png", 
        (savePath) => Debug.Log("保存成功:" + savePath),
        (errorMsg) => Debug.LogError("保存失败:" + errorMsg)
    );
}

注:需在Unity Asset Store下载免费的NativeGallery插件,或自行实现原生桥接

方案二:原生iOS桥接(无需插件)

先编写C#调用代码:

using UnityEngine;
using System.Runtime.InteropServices;

public class ScreenshotHandler : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void SaveImageToAlbum(byte[] imageData, int dataLength);

    public void CaptureAndSave()
    {
        StartCoroutine(CaptureProcess());
    }

    private IEnumerator CaptureProcess()
    {
        yield return new WaitForEndOfFrame();
        Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        screenshot.Apply();
        byte[] pngBytes = screenshot.EncodeToPNG();
        Destroy(screenshot);

        #if UNITY_IOS && !UNITY_EDITOR
        SaveImageToAlbum(pngBytes, pngBytes.Length);
        #endif
    }
}

再创建iOS原生文件ScreenshotHandler.mm,放入Unity项目的Assets/Plugins/iOS目录:

#import <UIKit/UIKit.h>
#import <Photos/Photos.h>

extern "C" {
    void SaveImageToAlbum(unsigned char* imageData, int length) {
        NSData* pngData = [NSData dataWithBytes:imageData length:length];
        UIImage* screenshotImage = [UIImage imageWithData:pngData];

        dispatch_async(dispatch_get_main_queue(), ^{
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetCreationRequest *assetRequest = [PHAssetCreationRequest creationRequestForAsset];
                [assetRequest addResourceWithType:PHAssetResourceTypePhoto data:pngData options:nil];
            } completionHandler:^(BOOL success, NSError *error) {
                if (success) {
                    NSLog(@"截图已保存到相册");
                } else {
                    NSLog(@"保存失败:%@", error.localizedDescription);
                }
            }];
        });
    }
}

3. 验证沙盒文件是否存在

如果仅需写入沙盒无需同步到相册,可通过Xcode查看App沙盒确认文件:
连接iOS设备,打开Xcode → Window → Devices and Simulators → 选中你的App → 点击Download Container → 右键显示包内容 → 进入Documents目录(对应Application.persistentDataPath)查看截图文件

额外注意事项

  • Unity版本:建议使用LTS版本(如2020.3+),避免旧版本对iOS截图API的兼容问题
  • 构建配置:在Player Settings中设置Scripting Backend为IL2CPP(iOS推荐),确保开启Allow Fullscreen等必要选项

内容的提问来源于stack exchange,提问作者利维坦

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 05:36:18