EditorTool3D/Assets/ZXL/Scripts/Helper/AssetHelper.cs

188 lines
6.5 KiB
C#

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);
}
}
}
/// <summary>
/// 打开选择文件弹框
/// </summary>
static async Task<IList<ItemWithStream>> OpenFilePushAsync()
{
var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false);
return paths;
}
/// <summary>
/// 下载模型内容并返回模型数据
/// </summary>
/// <param name="fileUrl"></param>
/// <param name="savePathWithName"></param>
/// <returns></returns>
static async Task<bool> 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);
}
}
/// <summary>
/// 加载外部模型到场景中
/// </summary>
/// <param name="title"></param>
/// <param name="onProgress"></param>
/// <param name="onBeginLoad"></param>
/// <param name="parent"></param>
static async Task LoadExternalModelToSceneAsync(string title, Action<AssetLoaderContext, float> onProgress,
Action<bool> 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 !!");
}
/// <summary>
/// 加载内部模型到场景中
/// </summary>
/// <param name="path">模型路径</param>
/// <param name="onProgress">进度条更新</param>
/// <param name="onBeginLoad">开始加载</param>
/// <param name="parent">模型实例化父节点</param>
static async Task LoadInternalModelToSceneAsync(string path, Action<AssetLoaderContext, float> onProgress,
Action<bool> 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
}
}