게임 엔진/Unity
[유니티/안드로이드 11] 범위 지정 저장소 / 시스템에서 제공하는 디렉터리 경로 얻어오는 방법
niamdank
2021. 10. 14. 16:11
※ 디렉터리 경로가 필요한 경우 다음 함수 사용
private static string GetAndroidExternalFilesDir()
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
object[] args = { null };
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[]>("getExternalFilesDirs", args);
AndroidJavaObject emulated = null;
AndroidJavaObject sdCard = null;
for (int i = 0; i < externalFilesDirectories.Length; i++)
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
{
// Check which one is the emulated and which the sdCard.
bool isRemovable = environment.CallStatic<bool ("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool> ("isExternalStorageEmulated", directory);
if (isEmulated)
emulated = directory;
else if (isRemovable && isEmulated == false)
sdCard = directory;
}
}
// Return the sdCard if available
if (sdCard != null)
return sdCard.Call<string>("getAbsolutePath");
else
return emulated.Call<string>("getAbsolutePath");
}
}
}
변경된 이유)
보안 관련 내용으로 안드로이드 11을 타겟팅하게 되면 기존 WRITE_EXTERNAL_STORAGE 및 WRITE_MEDIA_STORAGE를 더 이상 지원하지 않게 변경됨에 따라 이 권한을 사용하지 않는 앱에 제공되는 디렉터리를 사용해야 함.
추가로 확인된 사항)
이상의 함수로 받아오는 경로가 유니티의 Application.persistentDataPath와 동일한 것 확인, 유니티의 경우 Application.persistentDataPath를 사용하면 됨.