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

121 lines
4.3 KiB
C#
Raw Normal View History

2024-12-17 23:11:00 +08:00
using System;
using System.Collections;
using System.IO;
using TriLibCore;
using TriLibCore.SFB;
using UnityEngine;
using UnityEngine.Networking;
namespace ZXL.Helper
{
public static class ZAssetHelper
{
private static string _rootPath;
static string _assetsFolder;
static string _sceneFolder;
static string _cacheFolder;
static ZAssetHelper()
{
_rootPath = $"{Application.dataPath}/{Application.productName}";
_assetsFolder = _rootPath + "/Assets";
_sceneFolder = _rootPath + "/Scenes";
_cacheFolder = _rootPath + "/Cache";
if (!Directory.Exists(_rootPath))
Directory.CreateDirectory(_rootPath);
if (!Directory.Exists(_assetsFolder))
Directory.CreateDirectory(_assetsFolder);
if (!Directory.Exists(_sceneFolder))
Directory.CreateDirectory(_sceneFolder);
if (!Directory.Exists(_cacheFolder))
Directory.CreateDirectory(_cacheFolder);
}
public static IEnumerator LoadAssets(string assetsName, GameObject parent, Action<GameObject> callback)
{
var folderName = assetsName.Split(".")[0];
string assetsPath = $"{_assetsFolder}/{folderName}/{assetsName}";
var loadModelFromFile = AssetLoader.LoadModelFromFile(assetsPath, null, null, OnProgress, null, parent);
bool isTrue = false;
while (!isTrue)
{
Debug.Log(loadModelFromFile.LoadingProgress);
yield return null;
}
callback(loadModelFromFile.WrapperGameObject);
void OnProgress(AssetLoaderContext arg1, float arg2)
{
if (arg2.Equals(1f))
{
isTrue = true;
}
}
}
public static IEnumerator PushModel(string title, GameObject wrapperGameObject, Action callback = null,
Action<AssetLoaderContext, float> onProgress = null, Action<bool> onBeginLoad = null,
Action<IContextualizedError> onError = null)
{
string exten = "";
var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", exten, false);
if (paths.Count == 0)
{
Debug.Log("Selected file: " + paths[0].Name);
yield break;
}
else
{
var strings = paths[0].Name.Split("/");
var name = strings[strings.Length - 1];
var split = name.Split(".")[0];
string savePathWithName = $"{_assetsFolder}/{split}/{name}";
UnityWebRequest request = UnityWebRequest.Get(paths[0].Name);
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();
break;
}
else if (request.isNetworkError)
{
Debug.Log("Download Error:" + request.error);
break;
}
else
{
}
yield return new WaitForSeconds(Time.deltaTime);
}
AssetLoader.LoadModelFromFile(savePathWithName, null, null, OnProgress, null, wrapperGameObject);
}
void OnProgress(AssetLoaderContext arg1, float arg2)
{
Debug.Log("OnProgress");
var strings = arg1.Filename.Split("/");
string fileName = strings[strings.Length - 1];
if (arg2.Equals(1f))
{
string url = $"{_assetsFolder}/{fileName}";
Debug.Log("LoadModel");
}
}
}
}
}