using System; using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; using XRTool.Util; using Newtonsoft.Json; using TriLibCore; using TriLibCore.Mappers; using TriLibCore.Utils; namespace ShadowStudio.Model { public enum ArtState { UnLoad = -1, Loading = 0, Loaded = 1 } /// /// 外部资源加载 /// 可能是模型 zip文件 /// 可能是图片 /// 可能是视频,视频可在线播放,无需下载 /// public class ExtralLoadHandler : ArtInstanceHandler { private ArtState artState = ArtState.UnLoad; private Action loadPorcess; private Action artLoaded; public override UnityEngine.Object InstanceArt() { return LoadCache; } /// /// 异步加载资源 /// 此函数仅加载资源,不负责下载和解压 /// 与异步获取icon不同,异步获取icon,当不存在文件时,会先下载资源再加载 /// /// /// /// /// public override IEnumerator LoadAsyn(string path, Action process, Action loaded) { if (Info.ArtType == ArtType.Movies) { process?.Invoke(1); loaded(null); } else if (Info.ArtType == ArtType.Image) { Texture icon = GetIcon(); yield return icon; if (icon) { LoadCache = icon; process?.Invoke(1); loaded?.Invoke(LoadCache); } else { GetIcon((tex) => { LoadCache = tex; process?.Invoke(1); loaded?.Invoke(LoadCache); }); } } else if (Info.ArtType == ArtType.Model) { if (LoadCache) { string cloneName = LoadCache.name; LoadCache = GameObject.Instantiate(LoadCache); LoadCache.name = cloneName; loaded?.Invoke(LoadCache); yield break; } if (artState == ArtState.Loading) { loadPorcess += (p) => process?.Invoke(p); artLoaded += (obj) => { string cloneName = LoadCache.name; LoadCache = GameObject.Instantiate(LoadCache); LoadCache.name = cloneName; loaded?.Invoke(LoadCache); artLoaded = null; loadPorcess = null; }; yield break; } if (artState == ArtState.UnLoad) { artState = ArtState.Loading; } string url = Info.Url; string serverPath = Path.Combine(BuildConfig.Instance.ServerUrl, url); string filePath = Path.Combine(localExtralPath, GetFilePath(url)); try { ///文件不存在,下载 ///模型的图例?是否下载呢?是个问题 if (!File.Exists(filePath)) { TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadDataAsync(serverPath, filePath, (isComplete, handler) => { if (handler != null) { LoadInternal(filePath, loaded); } else { UnityLog.LogError(JsonConvert.SerializeObject(Info) + "frist load error!"); } }, null, (all, cur) => { DownProcess = cur; process?.Invoke(cur); loadPorcess?.Invoke(cur); })); //TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) => // { // if (handler != null) // { // LoadInternal(filePath, loaded, handler.downloadHandler.data); // } // else // { // UnityLog.LogError(JsonConvert.SerializeObject(Info) + "frist load error!"); // } // }, null, (all, cur) => // { // DownProcess = cur; // process?.Invoke(cur); // loadPorcess?.Invoke(cur); // })); } else { //TimerMgr.Instance.StartCoroutine(DataFileUtil.RequestDownData(filePath, (tmppath, handler) => //{ // LoadInternal(filePath, loaded, handler.downloadHandler != null ? handler.downloadHandler.data : null); //}, null, (all, cur) => // { // DownProcess = cur; // process?.Invoke(cur); // loadPorcess?.Invoke(cur); // })); LoadInternal(filePath, loaded); } } catch { loaded?.Invoke(null); UnityLog.LogError(Info.ArtId + "load exception!"); } } yield return null; } /// /// 加载模型 /// /// /// private void LoadModel(string filePath, Action loaded) { //using (var assetLoader = new AssetLoaderAsync()) //{ // try // { // var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); // assetLoaderOptions.RotationAngles = new Vector3(90f, 180f, 0f); // assetLoaderOptions.AutoPlayAnimations = true; // assetLoader.LoadFromFile(filePath, assetLoaderOptions, null, (loadedGameObject) => // { // LoadCache = loadedGameObject; // loaded?.Invoke(loadedGameObject); // }); // } // catch (Exception e) // { // Debug.LogError(e.ToString()); // loaded?.Invoke(null); // } //} } private AssetLoaderOptions GetAssetLoaderOptions() { //var assetLoaderOptions = AssetLoaderOptions.CreateInstance(); //assetLoaderOptions.DontLoadCameras = false; //assetLoaderOptions.DontLoadLights = false; //assetLoaderOptions.UseCutoutMaterials = false; //assetLoaderOptions.AddAssetUnloader = true; //return assetLoaderOptions; return null; } private void OnLoad(AssetLoaderContext assetLoaderContext) { //if (_loadedGameObject != null) //{ // Destroy(_loadedGameObject); //} //_loadedGameObject = assetLoaderContext.RootGameObject; //if (_loadedGameObject != null) //{ // Camera.main.FitToBounds(assetLoaderContext.RootGameObject, 2f); // Debug.Log("Model loaded. Loading materials."); //} //else //{ // Debug.Log("Model materials could not be loaded."); //} //_loadModelButton.interactable = true; } /// /// 更改层级 /// /// public void ChangeLayerRecursively(Transform parentTransform) { // 修改父物体的层级 parentTransform.gameObject.layer = LayerMask.NameToLayer("Arrow"); // 遍历所有子物体,并递归调用ChangeLayerRecursively方法 for (int i = 0; i < parentTransform.childCount; i++) { Transform childTransform = parentTransform.GetChild(i); ChangeLayerRecursively(childTransform); } } private void LoadInternal(string filename, Action loaded) { TriLibModelLoad.Load(filename, (AssetLoaderContext ac) => { Debug.Log("模型加载完成"); }, (AssetLoaderContext ac) => { Debug.Log("载材质加完成"); ChangeLayerRecursively(ac.RootGameObject.transform); LoadCache = ac.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; }, (AssetLoaderContext ac, float f) => { Debug.Log("加载中==》" + f); }, (IContextualizedError error) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.LogError(Info.ArtId + "资源加载失败"); }); return; //var modelFileWithStream = FindModelFile(); var modelFilename = Path.GetFileName(filename); var modelStream = File.OpenRead(filename); var _assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); _assetLoaderOptions.TextureMapper = ScriptableObject.CreateInstance(); _assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance(); _assetLoaderOptions.FixedAllocations.Add(_assetLoaderOptions.ExternalDataMapper); _assetLoaderOptions.FixedAllocations.Add(_assetLoaderOptions.TextureMapper); var _modelExtension = modelFilename != null ? FileUtils.GetFileExtension(modelFilename, false) : null; if (_modelExtension == "zip") { if (modelStream != null) { //AssetLoaderZip.LoadModelFromZipStream(modelStream, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items, null); AssetLoaderZip.LoadModelFromZipStream(modelStream, (t1) => { LoadCache = t1.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; }, null, null, (t2) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.LogError(Info.ArtId + "资源加载失败"); }, null, _assetLoaderOptions, null, null); } else { //AssetLoaderZip.LoadModelFromZipFile(modelFilename, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items, null); AssetLoaderZip.LoadModelFromZipFile(modelFilename, (t1) => { LoadCache = t1.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; }, null, null, (t2) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.LogError(Info.ArtId + "资源加载失败"); }, null, _assetLoaderOptions, null, null); } } else { if (modelStream != null) { //AssetLoader.LoadModelFromStream(modelStream, modelFilename, _modelExtension, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items); AssetLoader.LoadModelFromStream(modelStream, modelFilename, _modelExtension, (t2) => { LoadCache = t2.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; }, null, null, (t2) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.LogError(Info.ArtId + "资源加载失败"); }, null, _assetLoaderOptions, null); } else { AssetLoader.LoadModelFromFile(modelFilename, (t2) => { LoadCache = t2.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; }, null, null, (t2) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.LogError(Info.ArtId + "资源加载失败"); }, null, _assetLoaderOptions, null); } } //var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); //var assetLoaderFilePicker = AssetLoaderFilePicker.Create(); //assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a Model file", (assetLoaderContext) => //{ // LoadCache = assetLoaderContext.RootGameObject; // loaded?.Invoke(LoadCache); // artLoaded?.Invoke(LoadCache); // artState = ArtState.Loaded; //}, OnMaterialsLoad, (t1, process) => //{ //}, (isfrist) => { }, (error) => { }, null, assetLoaderOptions); //AssetLoaderZip.LoadModelFromZipFile(modelFilename, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items, null); //_loadingTimer.Reset(); //_loadingTimer.Start(); //PreLoadSetup(); //var assetLoaderOptions = GetAssetLoaderOptions(); //using (var assetLoader = new AssetLoaderAsync()) //{ // //assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed; // try // { // if (fileBytes != null && fileBytes.Length > 0) // { // var extension = FileUtils.GetFileExtension(filename); // assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, null, delegate (GameObject loadedGameObject) // { // LoadCache = loadedGameObject; // loaded?.Invoke(LoadCache); // artLoaded?.Invoke(LoadCache); // artState = ArtState.Loaded; // }); // } // else if (!string.IsNullOrEmpty(filename)) // { // assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions, null, delegate (GameObject loadedGameObject) // { // LoadCache = loadedGameObject; // loaded?.Invoke(LoadCache); // artLoaded?.Invoke(LoadCache); // artState = ArtState.Loaded; // }); // } // else // { // loaded?.Invoke(null); // artLoaded?.Invoke(null); // artState = ArtState.UnLoad; // throw new Exception("File not selected"); // } // } // catch // { // loaded?.Invoke(null); // artLoaded?.Invoke(null); // artState = ArtState.UnLoad; // UnityLog.LogError(Info.ArtId + "资源加载失败"); // } //} } private void OnMaterialsLoad(AssetLoaderContext obj) { //throw new NotImplementedException(); } } }