123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections;
- using System.IO;
- using TriLib;
- using UnityEngine;
- using UnityEngine.Networking;
- using XRTool.Util;
- using Newtonsoft.Json;
- namespace ShadowStudio.Model
- {
- public enum ArtState
- {
- UnLoad = -1,
- Loading = 0,
- Loaded = 1
- }
- /// <summary>
- /// 外部资源加载
- /// 可能是模型 zip文件
- /// 可能是图片
- /// 可能是视频,视频可在线播放,无需下载
- /// </summary>
- public class ExtralLoadHandler : ArtInstanceHandler
- {
- private ArtState artState = ArtState.UnLoad;
- private Action<float> loadPorcess;
- private Action<UnityEngine.Object> artLoaded;
- public override UnityEngine.Object InstanceArt()
- {
- return LoadCache;
- }
- /// <summary>
- /// 异步加载资源
- /// 此函数仅加载资源,不负责下载和解压
- /// 与异步获取icon不同,异步获取icon,当不存在文件时,会先下载资源再加载
- /// </summary>
- /// <param name="path"></param>
- /// <param name="process"></param>
- /// <param name="loaded"></param>
- /// <returns></returns>
- public override IEnumerator LoadAsyn(string path, Action<float> process, Action<UnityEngine.Object> 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;
- }
- /// <summary>
- /// 加载模型
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="loaded"></param>
- private void LoadModel(string filePath, Action<UnityEngine.Object> 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;
- }
- private void LoadInternal(string filename, Action<UnityEngine.Object> loaded, byte[] fileBytes = 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.Instance.LogError(Info.ArtId + "资源加载失败");
- }
- }
- }
- }
- }
|