using System; using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; using XRTool.Util; using Newtonsoft.Json; using TriLibCore; 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); // }); //} if (LoadCache) { process?.Invoke(1); loaded?.Invoke(LoadCache); yield break; } else { string url = Info.Url; if (string.IsNullOrEmpty(url)) { url = Info.Icon; } string serverPath = ResourcesManager.ArtServerPath + "/" + url;// Path.Combine(ResourcesManager.ArtServerPath, url); string filePath = Path.Combine(localExtralPath, url); try { ///文件不存在,下载 ///下载后,加载 DownloadHandlerTexture texHander = new DownloadHandlerTexture(true); if (!File.Exists(filePath)) { GameSession.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) => { LoadTexture((tex) => { LoadCache = tex; process?.Invoke(1); loaded?.Invoke(LoadCache); }, handler.downloadHandler); }, texHander)); } else { GameSession.Instance.StartCoroutine(DataFileUtil.RequestDownData(@"file://" + filePath, (isComplete, handler) => { LoadTexture((tex) => { LoadCache = tex; process?.Invoke(1); loaded?.Invoke(LoadCache); }, handler.downloadHandler); }, texHander)); } } catch (Exception ex) { UnityLog.Instance.LogError(JsonConvert.SerializeObject(Info) + "icon error" + ex.ToString()); } } } 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(ResourcesManager.ArtServerPath, url); string filePath = Path.Combine(localExtralPath, url); try { ///文件不存在,下载 ///模型的图例?是否下载呢?是个问题 if (!File.Exists(filePath)) { GameSession.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) => { if (handler != null) { LoadInternal(filePath, loaded, handler.downloadHandler.data); } else { UnityLog.Instance.LogError(JsonConvert.SerializeObject(Info) + "frist load error!"); } }, null, (all, cur) => { DownProcess = cur; process?.Invoke(cur); loadPorcess?.Invoke(cur); })); } else { GameSession.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); })); } } catch { loaded?.Invoke(null); UnityLog.Instance.LogError(Info.ArtId + "load exception!"); } } yield return null; } /// /// 加载模型 /// /// /// private void LoadModel(string filePath, Action loaded) { TriLibModelLoad.Load(filePath, (AssetLoaderContext ac) => { Debug.Log("模型加载完成"); }, (AssetLoaderContext ac) => { LoadCache = ac.RootGameObject; loaded?.Invoke(ac.RootGameObject); Debug.Log("载材质加完成"); }, (AssetLoaderContext ac, float f) => { Debug.Log("加载中==》" + f); }, (IContextualizedError error) => { Debug.LogError(error); loaded?.Invoke(null); }); } private void LoadInternal(string filename, Action loaded, byte[] fileBytes = null) { TriLibModelLoad.Load(filename, (AssetLoaderContext ac) => { Debug.Log("模型加载完成"); }, (AssetLoaderContext ac) => { LoadCache = ac.RootGameObject; loaded?.Invoke(LoadCache); artLoaded?.Invoke(LoadCache); artState = ArtState.Loaded; Debug.Log("载材质加完成"); }, (AssetLoaderContext ac, float f) => { Debug.Log("加载中==》" + f); }, (IContextualizedError error) => { loaded?.Invoke(null); artLoaded?.Invoke(null); artState = ArtState.UnLoad; UnityLog.Instance.LogError(Info.ArtId + "资源加载失败"); }); } } }