一、Player Settings 设置
Player Settings>Player>Other Settings> Android > Write Permission > External (SDCard).
二、代码
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Android;
using System;
using UnityEngine.UI;
public class VideoPlayManager : MonoBehaviour
{
private void Awake()
{
}
void Start()
{
SetAndroid();
}
private void SetAndroid()
{
#if UNITY_ANDROID
// 控制音频和视频的权限,在Android设备上需要申请权限
// Permission.RequestUserPermission(Permission.ExternalStorageRead);
// Permission.RequestUserPermission(Permission.ExternalStorageWrite);
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
{
Permission.RequestUserPermission(Permission.ExternalStorageRead);
}
//请求 写入外部存储(如 SD 卡)的权限
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
#endif
}
}
三、修改AndroidManifest
打开AndroidManifest
Player Settings>Player>Other Settings> Android >Publishing Settings> Build>Custom Main Manifest
在Assets\Plugins\Android\AndroidManifest打开AndroidManifest添加权限代码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />