using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using Cysharp.Threading.Tasks;
///
/// 加载文件辅助类
///
public class LoadFileHelper : MonoBehaviour
{
public static LoadFileHelper Instance = null;
void Awake()
{
DontDestroyOnLoad(this);
Instance = this;
}
///
/// 从本地外部目录或本地StreamingAssets目录异步加载文件,得到文件string
///
///
///
public void LoadFileStringAsync(string path, Action complete)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
StartCoroutine(GetFileString(path, complete));
}
private IEnumerator GetFileString(string path, Action complete)
{
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(path))
{
yield return unityWebRequest.SendWebRequest();
string fileString = unityWebRequest.downloadHandler.text;
if (complete != null)
complete(fileString);
}
}
///
/// 从本地外部目录或本地StreamingAssets目录异步加载文件,得到文件string
///
///
///
public async UniTask LoadFileStringAsync(string path)
{
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(path))
{
await unityWebRequest.SendWebRequest();
string fileString = unityWebRequest.downloadHandler.text;
return fileString;
}
}
///
/// 从本地外部目录或本地StreamingAssets目录异步加载文件,得到文件byte数组
///
///
///
public void LoadFileBytesAsync(string path, Action complete)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
StartCoroutine(GetFileBytes(path, complete));
}
private IEnumerator GetFileBytes(string path, Action complete)
{
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(path))
{
yield return unityWebRequest.SendWebRequest();
byte[] bytes = unityWebRequest.downloadHandler.data;
if (complete != null)
complete(bytes);
}
}
///
/// 从本地外部目录或本地StreamingAssets目录异步加载多张或一张图片
///
///
///
public void LoadPicturesAsync(string[] paths, Action> complete)
{
if (paths != null)
{
StartCoroutine(LoadTexture2Ds(paths, complete));
}
}
private IEnumerator LoadTexture2Ds(string[] paths, Action> complete)
{
List textures = new List();
foreach (var path in paths)
{
using (UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(path))
{
yield return unityWebRequest.SendWebRequest();
Texture2D texture = DownloadHandlerTexture.GetContent(unityWebRequest);
textures.Add(texture);
}
}
if (complete != null)
{
complete(textures);
}
}
///
/// 从本地外部目录或本地StreamingAssets目录异步加载多个或一个AB
///
///
///
public void LoadAssetBundlesAsync(string[] paths, Action> complete)
{
if (paths != null)
{
StartCoroutine(LoadAssetBundles(paths, complete));
}
}
private IEnumerator LoadAssetBundles(string[] paths, Action> complete)
{
List assetBundles = new List();
foreach (var path in paths)
{
using (UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(path))
{
yield return unityWebRequest.SendWebRequest();
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
assetBundles.Add(assetBundle);
}
}
if (complete != null)
{
complete(assetBundles);
}
}
///
/// 从本地外部目录或本地StreamingAssets目录同步加载文件,得到文件string
///
///
///
public void LoadFileStringSync(string path, Action complete)
{
if (!File.Exists(path))
return;
StreamReader sr = new StreamReader(path);
if (sr == null)
return;
string fileString = sr.ReadToEnd();
sr.Close();
sr.Dispose();
if (complete != null)
complete(fileString);
}
}