123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- using Newtonsoft.Json;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- using XRTool.Util;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 资源实例化抽象类
- /// 此类按照Resource方式进行简单实现
- /// </summary>
- public abstract class ArtInstanceHandler : ArtInstance
- {
- private ArtInfo info;
- public ArtInfo Info { get => info; set => info = value; }
- public UnityEngine.Object LoadCache { get => loadCache; set => loadCache = value; }
- private UnityEngine.Object loadCache;
- private string localPath;
- private Texture artIcon;
- private float process;
- /// <summary>
- /// 网络中下载到本地的文件路径
- /// </summary>
- public string localExtralPath
- {
- get
- {
- if (string.IsNullOrEmpty(localPath))
- {
- localPath = Path.Combine(BuildConfig.Instance.UserPath, Info.ArtType.ToString());
- }
- return localPath;
- }
- }
- public float DownProcess { get => process; set => process = value; }
- /// <summary>
- /// 获取缩略图
- /// </summary>
- /// <returns></returns>
- public virtual Texture GetIcon()
- {
- if (!artIcon)
- {
- if (Info != null && (Info.InstaceType == InstaceType.References || Info.InstaceType == InstaceType.ResourceLoad ||
- Info.InstaceType == InstaceType.UnityBase))
- {
- return artIcon = Resources.Load<Texture>(Info.Icon);
- }
- }
- return artIcon;
- }
- /// <summary>
- /// 实例化物体
- /// </summary>
- /// <returns></returns>
- public virtual UnityEngine.Object InstanceArt()
- {
- if (LoadCache)
- {
- return GameObject.Instantiate(LoadCache);
- }
- return null;
- }
- /// <summary>
- /// 同步加载资源
- /// </summary>
- /// <returns></returns>
- public virtual UnityEngine.Object LoadArt()
- {
- if (Info != null)
- {
- return LoadCache = Resources.Load(Info.Url);
- }
- return null;
- }
- /// <summary>
- /// 异步加载资源
- /// </summary>
- /// <param name="path"></param>
- /// <param name="process"></param>
- /// <param name="loaded"></param>
- public virtual void LoadArtAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
- {
- if (Info != null)
- {
- TimerMgr.Instance.StartCoroutine(LoadAsyn(path, process, loaded));
- }
- else
- {
- LoadCache = null;
- process?.Invoke(1);
- loaded?.Invoke(LoadCache);
- }
- }
- /// <summary>
- /// 异步加载
- /// </summary>
- /// <param name="path"></param>
- /// <param name="process"></param>
- /// <param name="loaded"></param>
- /// <returns></returns>
- public virtual IEnumerator LoadAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
- {
- var sync = Resources.LoadAsync(path);
- while (!sync.isDone)
- {
- process?.Invoke(sync.progress);
- yield return new WaitForFixedUpdate();
- }
- LoadCache = sync.asset;
- process?.Invoke(1);
- loaded?.Invoke(LoadCache);
- }
- /// <summary>
- /// 初始化数据
- /// </summary>
- /// <param name="info"></param>
- public virtual void SetData(ArtInfo info, string containerName = "")
- {
- this.Info = info;
- }
- /// <summary>
- /// 创建资源对应的组件
- /// </summary>
- /// <returns></returns>
- public virtual ArtComponent InstanceComponent()
- {
- if (Info != null && !string.IsNullOrEmpty(Info.Component))
- {
- return ArtInfoMgr.Instance.InstanceComponent(Info.Component);
- }
- return null;
- }
- /// <summary>
- /// 获取外部资源的缩略图
- /// 如果是模型,获取icon的图片资源
- /// 如果是图片,直接获取本身url作为缩略图
- /// 如果是视频,则自行获取对应的缩略图:使用VideoPlayer读取对应帧,此处不提供方法
- /// </summary>
- /// <param name="loadTex"></param>
- public virtual void GetIcon(Action<Texture> loadTex)
- {
- if (artIcon)
- {
- loadTex?.Invoke(artIcon);
- return;
- }
- if (Info.ArtType == ArtType.Model || Info.ArtType == ArtType.Image || Info.ArtType == ArtType.Movies)
- {
- string url = Info.Icon;
- if (string.IsNullOrEmpty(url) && Info.ArtType == ArtType.Image)
- {
- url = Info.Url;
- }
- string serverPath = BuildConfig.Instance.ServerUrl + "/" + url;// Path.Combine(ResourcesManager.ArtServerPath, url);
- string filePath = Path.Combine(localExtralPath, url);
- try
- {
- ///文件不存在,下载
- ///下载后,加载
- DownloadHandlerTexture texHander = new DownloadHandlerTexture(true);
- if (!File.Exists(filePath))
- {
- TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) =>
- {
- LoadTexture(loadTex, handler.downloadHandler);
- }, texHander));
- }
- else
- {
- TimerMgr.Instance.StartCoroutine(DataFileUtil.RequestDownData(@"file://" + filePath, (isComplete, handler) =>
- {
- LoadTexture(loadTex, handler.downloadHandler);
- }, texHander));
- }
- }
- catch (Exception ex)
- {
- UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon error" + ex.ToString());
- }
- }
- else if (Info.ArtType == ArtType.Movies)
- {
- loadTex?.Invoke(null);
- }
- }
- public virtual void LoadTexture(Action<Texture> loadIcon, DownloadHandler handler)
- {
- Texture texture = null;
- if (handler != null)
- {
- var texHandler = handler as DownloadHandlerTexture;
- if (texHandler != null)
- {
- texture = texHandler.texture;
- }
- else
- {
- UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon load error");
- }
- }
- else
- {
- UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon handler error");
- }
- if (texture == null)
- {
- UnityLog.LogError(JsonConvert.SerializeObject(Info) + "no iconread");
- }
- loadIcon?.Invoke(texture);
- artIcon = texture;
- }
- /// <summary>
- /// 判断是否已下载指定文件
- /// </summary>
- /// <returns></returns>
- public bool IsDownLoad()
- {
- string filePath = Path.Combine(localExtralPath, Info.Url);
- return File.Exists(filePath);
- }
- public string GetFilePath(string url)
- {
- string[] arr = url.Split('/');
- string path = url;
- if (arr.Length > 2)
- {
- path = "";
- for (int i = arr.Length - 2; i < arr.Length; i++)
- {
- if (i != arr.Length -1)
- {
- path += arr[i] + "/";
- }
- else
- {
- path += arr[i];
- }
- }
- }
- UnityLog.Log(path);
- return path;
- }
- public void DownLoad(Action<float, float> downProcess, Action<string, byte[]> downComplete)
- {
- string url = Info.Url;
- string serverPath = Path.Combine(BuildConfig.Instance.ServerUrl, url);
- string filePath = Path.Combine(localExtralPath, GetFilePath(url));
- TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (string path, UnityWebRequest handler) =>
- {
- downComplete?.Invoke(path, handler.downloadHandler.data);
- }
- ));
- }
- }
- }
|