123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- 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
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
- 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);
- });
- }
- }
- 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);
- }));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- else
- {
-
-
-
-
-
-
-
-
-
- LoadInternal(filePath, loaded);
- }
- }
- catch
- {
- loaded?.Invoke(null);
- UnityLog.LogError(Info.ArtId + "load exception!");
- }
- }
- yield return null;
- }
-
-
-
-
-
- private void LoadModel(string filePath, Action<UnityEngine.Object> loaded)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- private AssetLoaderOptions GetAssetLoaderOptions()
- {
-
-
-
-
-
-
- return null;
- }
- private void OnLoad(AssetLoaderContext assetLoaderContext)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- private void LoadInternal(string filename, Action<UnityEngine.Object> loaded)
- {
-
- var modelFilename = Path.GetFileName(filename);
- var modelStream = File.OpenRead(filename);
- var _assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
- _assetLoaderOptions.TextureMapper = ScriptableObject.CreateInstance<FilePickerTextureMapper>();
- _assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance<FilePickerExternalDataMapper>();
- _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, (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, (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, (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);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- private void OnMaterialsLoad(AssetLoaderContext obj)
- {
-
- }
- }
- }
|