EditorTool3D/Assets/ZXL/Scripts/Test/Test.cs

221 lines
8.0 KiB
C#
Raw Normal View History

2024-12-17 23:11:00 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using TriLibCore;
using TriLibCore.SFB;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using ZXL.Helper;
using ZXL.Scripts.UI;
using ZXLA;
namespace ZXL
{
public class Test : MonoBehaviour
{
private FileSystem _fileSystem;
private void Awake()
{
_fileSystem = this.gameObject.GetComponent<FileSystem>();
}
private void Update()
{
// if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.LeftShift))
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
_fileSystem.CreateDirectory("/Root", -1);
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
_fileSystem.CreateDirectory("/Root/Test1",0);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
_fileSystem.CreateFile("/Root/Test1/a.txt",1);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
_fileSystem.CreateFile("/Root/b.txt",0);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
_fileSystem.DeleteDirectory("/Root/Test1");
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
_fileSystem.DeleteDirectory("/Root");
}
}
if (Input.GetKeyDown(KeyCode.A))
{
// var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
// var assetLoaderContext = AssetLoader.LoadModelFromFile(path, null, OnMaterialsLoad, OnProgress, null,
// null, assetLoaderOptions);
// var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
// var filePicker = AssetLoaderFilePicker.Create();
// filePicker.LoadModelFromFilePickerAsync("Select a File",
// (a) => { Debug.Log("OnLoad"); },
// (a) => { Debug.Log("OnMaterialsIsLoad"); },
// OnProgress,
// (a) => {Debug.Log("OnBeginLoad"); },
// (a) => {Debug.Log("OnErrorLoad"); },
// gameObject,
// assetLoaderOptions);
// StartCoroutine(LoadModel(@$"C:\Users\Administrator\Documents\Rail.FBX", "Rail.FBX"));
// AssetHelper.PushModel("xxx", null, null, null, null, gameObject);
// Task.Run(async () => { await AssetHelper.PushFileWithLoadToScene(gameObject); });
// AssetHelper.PushFileWithLoadToScene(gameObject);
// FileHelper.DeleteFolder("D:\\zxl\\UnityProject_Build\\TTR");
// SceneHelper.CreateSceneData("woccc");
// StartCoroutine(ZAssetHelper.PushModel("Test", gameObject, () => { Debug.Log("完成了"); }));
// StartCoroutine(ZAssetHelper.LoadAssets($"连接处.fbx", gameObject, (go) => { Debug.Log("完成了"); }));
// StartCoroutine(LoadModel());
// string src=$"F:\\SceneEditor3D\\Assets\\ZXL\\config.json";
// string neNam = "ccc";
// FileHelper.RenameFile(src,neNam);
// var fileInfo = new FileInfo($"F:\\SceneEditor3D\\Assets\\ZXL\\config.json");
// string src=$"F:\\SceneEditor3D\\Assets\\ZXL\\config.json";
// FileHelper.CopyFile(src);
UIManager.Instance.Open(UIType.ConctrolUI);
}
}
IEnumerator LoadModel()
{
var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false);
if (paths.Count >= 0)
{
var context = AssetLoader.LoadModelFromFile(paths[0].Name, null, null, OnProgress);
while (true)
{
if (context.LoadingProgress >= 1)
{
break;
}
Debug.Log($"加载中 {context.LoadingProgress}");
yield return null;
}
Debug.Log("完成");
}
void OnProgress(AssetLoaderContext arg1, float arg2)
{
Debug.Log($"load {arg1.Filename} {arg1.BasePath} {arg2}%");
}
}
// IEnumerator LoadModel(string modelFullPath)
// {
// UnityWebRequest request = UnityWebRequest.Get(modelFullPath);
// yield return request.SendWebRequest();
// if (request.result == UnityWebRequest.Result.Success)
// {
// // 将获取到的数据转换为Asset
// AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(request.downloadHandler.data);
// yield return createRequest;
// AssetBundle assetBundle = createRequest.assetBundle;
// GameObject modelPrefab = assetBundle.LoadAsset<GameObject>("your_model");
// if (modelPrefab!= null)
// {
// GameObject instantiatedModel = Instantiate(modelPrefab);
// // 对模型进行位置、旋转、缩放等设置
// instantiatedModel.transform.position = new Vector3(0, 0, 0);
// instantiatedModel.transform.rotation = Quaternion.identity;
// instantiatedModel.transform.localScale = new Vector3(1, 1, 1);
// }
// else
// {
// Debug.LogError("未能从AssetBundle中获取到模型资源");
// }
// assetBundle.Unload(false);
// }
// else
// {
// Debug.LogError("加载模型文件失败: " + request.error);
// }
// }
IEnumerator LoadModel(string url, string resName)
{
UnityWebRequest request = UnityWebRequest.Get(url);
request.SendWebRequest();
while (true)
{
if (request.isDone)
{
byte[] results = request.downloadHandler.data;
string pathUrl = Application.streamingAssetsPath;
SaveFile(results, pathUrl, resName, () => { Debug.Log("完成"); });
LoadToScene(resName);
yield break;
}
else if (request.isNetworkError)
{
Debug.Log("Download Error:" + request.error);
yield break;
}
else
{
}
yield return null;
}
}
private static void SaveFile(byte[] res, string savePath, string fileUrl, Action action)
{
// if (!File.Exists(savePath))
// {
// Directory.CreateDirectory(savePath);
// }
string path = savePath + "/" + fileUrl;
FileInfo file = new FileInfo(path);
Stream sw;
sw = file.Create();
sw.Write(res, 0, res.Length);
sw.Close();
sw.Dispose();
action();
}
static void LoadToScene(string fileName)
{
var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
string path = $"{Application.streamingAssetsPath}/{fileName}";
var assetLoaderContext = AssetLoader.LoadModelFromFile(path);
UnityEngine.Debug.Log(assetLoaderContext.RootGameObject.name);
}
private void OnMaterialsLoad(AssetLoaderContext obj)
{
Debug.Log($"OnMaterialsLoad");
}
private void OnProgress(AssetLoaderContext arg1, float arg2)
{
Debug.Log($"load {arg1.Filename} {arg1.BasePath} {arg2}%");
}
}
}