using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; using TriLibCore; using TriLibCore.SFB; using UnityEngine; using UnityEngine.Networking; using UnityEngine.SceneManagement; namespace ZXL.Helper { public static class AssetHelper { private static string _rootPath; static string _assetsRootFolder; static string _sceneRootFolder; static string _cacheRootFolder; static AssetHelper() { _rootPath = $"{Application.dataPath}/{Application.productName}"; _assetsRootFolder = _rootPath + "/Assets"; _sceneRootFolder = _rootPath + "/Scenes"; _cacheRootFolder = _rootPath + "/Cache"; if (!Directory.Exists(_rootPath)) Directory.CreateDirectory(_rootPath); if (!Directory.Exists(_assetsRootFolder)) Directory.CreateDirectory(_assetsRootFolder); if (!Directory.Exists(_sceneRootFolder)) Directory.CreateDirectory(_sceneRootFolder); if (!Directory.Exists(_cacheRootFolder)) Directory.CreateDirectory(_cacheRootFolder); } #region xiazai public static async Task PushFileWithLoadToScene(GameObject parent) { var list = await OpenFilePushAsync(); if (list.Count > 0) { var fileInfo = new FileInfo(list[0].Name); Debug.Log("Selected file: " + list[0].Name); var directoryName = fileInfo.DirectoryName.Split("\\")[^1]; Debug.Log(directoryName); string savePathWithName = Path.Combine(_assetsRootFolder, directoryName, fileInfo.Name); var result = await DownloadModelAsync(fileInfo.FullName, savePathWithName); if (result) { await LoadInternalModelToSceneAsync(savePathWithName, null, null, parent); } } } /// /// 打开选择文件弹框 /// static async Task> OpenFilePushAsync() { var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false); return paths; } /// /// 下载模型内容并返回模型数据 /// /// /// /// static async Task DownloadModelAsync(string fileUrl, string savePathWithName) { UnityWebRequest request = UnityWebRequest.Get(fileUrl); request.SendWebRequest(); while (true) { if (request.isDone) { byte[] results = request.downloadHandler.data; // 通过模型数据写入本地 FileInfo file = new FileInfo(savePathWithName); if (file.Directory != null) file.Directory.Create(); Stream sw; sw = file.Create(); sw.Write(results, 0, results.Length); sw.Close(); sw.Dispose(); return true; } else if (request.isNetworkError) { Debug.Log("Download Error:" + request.error); return false; } else { } await Task.Delay(100); } } /// /// 加载外部模型到场景中 /// /// /// /// /// static async Task LoadExternalModelToSceneAsync(string title, Action onProgress, Action onBeginLoad, GameObject parent) { var defaultLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); var filePicker = AssetLoaderFilePicker.Create(); bool isLoadFinished = false; if (onProgress != null) { onProgress += OnProgress; filePicker.LoadModelFromFilePickerAsync(title, null, null, onProgress, onBeginLoad, null, parent, defaultLoaderOptions); // AssetLoader.LoadModelFromFile(path, null, null, onProgress, null, parent, defaultLoaderOptions); } else { filePicker.LoadModelFromFilePickerAsync(title, null, null, OnProgress, onBeginLoad, null, parent, defaultLoaderOptions); } void OnProgress(AssetLoaderContext assetLoaderContext, float progress) { if (progress >= 1) isLoadFinished = true; } while (!isLoadFinished) { await Task.Delay(100); } Debug.Log("LoadModelToScene Finish !!"); } /// /// 加载内部模型到场景中 /// /// 模型路径 /// 进度条更新 /// 开始加载 /// 模型实例化父节点 static async Task LoadInternalModelToSceneAsync(string path, Action onProgress, Action onBeginLoad, GameObject parent) { var defaultLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); bool isLoadFinished = false; if (onProgress != null) { onProgress += OnProgress; AssetLoader.LoadModelFromFile(path, null, null, onProgress, null, parent, defaultLoaderOptions); } else { AssetLoader.LoadModelFromFile(path, null, null, OnProgress, null, parent, defaultLoaderOptions); } void OnProgress(AssetLoaderContext assetLoaderContext, float progress) { if (progress >= 1) isLoadFinished = true; } while (!isLoadFinished) { await Task.Delay(100); } Debug.Log("LoadModelToScene Finish !!"); } #endregion } }