Fixed the FileSystem of Android

Fixed the FileSystem of Android
This commit is contained in:
ALEXTANG
2022-05-25 00:35:16 +08:00
parent 8c7024953f
commit 6a9cac0337
2 changed files with 90 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace TEngine
{
@@ -137,7 +138,19 @@ namespace TEngine
if (string.IsNullOrEmpty(filePath))
return null;
#if UNITY_ANDROID && !UNITY_EDITOR
//外部目录
if (filePath.StartsWith(Application.persistentDataPath))
{
return ReadAllBytesFromOutFolder(filePath);
}
else //内部目录
{
return ReadAllBytesFromInnerFolder(filePath);
}
#else
return ReadAllBytesFromOutFolder(filePath);
#endif
}
private static byte[] ReadAllBytesFromOutFolder(string filePath)
@@ -149,5 +162,37 @@ namespace TEngine
}
return File.ReadAllBytes(filePath);
}
private static byte[] ReadAllBytesFromInnerFolder(string filePath)
{
#if !UNITY_ANDROID || UNITY_EDITOR
filePath = $"file://{filePath}";
#endif
UnityWebRequest www = UnityWebRequest.Get(filePath);
UnityWebRequestAsyncOperation request = www.SendWebRequest();
while (!request.isDone) ;
byte[] data = www.downloadHandler.data;
www.downloadHandler.Dispose();
www.Dispose();
www = null;
return data;
}
/// <summary>
/// 读取内部目录
/// </summary>
/// <param name="filePath">文件完整路径</param>
/// <returns></returns>
private static string ReadTextFromInnerFolder(string filePath)
{
#if !UNITY_ANDROID || UNITY_EDITOR
filePath = $"file://{filePath}";
#endif
UnityWebRequest www = UnityWebRequest.Get(filePath);
UnityWebRequestAsyncOperation request = www.SendWebRequest();
while (!request.isDone) ;
return www.downloadHandler.text;
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace TEngineCore
{
@@ -137,7 +138,19 @@ namespace TEngineCore
if (string.IsNullOrEmpty(filePath))
return null;
#if UNITY_ANDROID && !UNITY_EDITOR
//外部目录
if (filePath.StartsWith(Application.persistentDataPath))
{
return ReadAllBytesFromOutFolder(filePath);
}
else //内部目录
{
return ReadAllBytesFromInnerFolder(filePath);
}
#else
return ReadAllBytesFromOutFolder(filePath);
#endif
}
private static byte[] ReadAllBytesFromOutFolder(string filePath)
@@ -149,5 +162,37 @@ namespace TEngineCore
}
return File.ReadAllBytes(filePath);
}
private static byte[] ReadAllBytesFromInnerFolder(string filePath)
{
#if !UNITY_ANDROID || UNITY_EDITOR
filePath = $"file://{filePath}";
#endif
UnityWebRequest www = UnityWebRequest.Get(filePath);
UnityWebRequestAsyncOperation request = www.SendWebRequest();
while (!request.isDone) ;
byte[] data = www.downloadHandler.data;
www.downloadHandler.Dispose();
www.Dispose();
www = null;
return data;
}
/// <summary>
/// 读取内部目录
/// </summary>
/// <param name="filePath">文件完整路径</param>
/// <returns></returns>
private static string ReadTextFromInnerFolder(string filePath)
{
#if !UNITY_ANDROID || UNITY_EDITOR
filePath = $"file://{filePath}";
#endif
UnityWebRequest www = UnityWebRequest.Get(filePath);
UnityWebRequestAsyncOperation request = www.SendWebRequest();
while (!request.isDone) ;
return www.downloadHandler.text;
}
}
}