123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using XRTool.Util;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 资源的处理类
- /// 相同资源只会创建一个实例
- /// </summary>
- public class ArtHandler : ArtInstance, ArtPlay
- {
- //public ArtHandler() {
- // Debug.Log("ArtHandler:" + UnityUtil.CurTimeString);
- //}
- private ArtInstanceHandler instancer;
- private ArtPlay artDisPlay;
- private ArtInfo info;
- public ArtInstanceHandler Instancer { get => instancer; set => instancer = value; }
- public ArtPlay ArtDisPlay { get => artDisPlay; set => artDisPlay = value; }
- public float DownProcess { get => Instancer.DownProcess; set => Instancer.DownProcess = value; }
- public event Action<ArtPlay> OnCreateArt;
- private Texture artIcon;
- //private Action<float> loadProcess;
- //private Action<UnityEngine.Object> artLoaded;
- /// <summary>
- /// 初始化资源
- /// </summary>
- /// <param name="info"></param>
- public virtual void SetData(ArtInfo info, string containerName = "")
- {
- if (info != null)
- {
- this.info = info;
- if (info.InstaceType == InstaceType.ResourceLoad)
- {
- Instancer = new ResourcesHandler();
- }
- else if (info.InstaceType == InstaceType.UnityBase)
- {
- Instancer = new UnityBaseHandler();
- }
- else if (info.InstaceType == InstaceType.References)
- {
- Instancer = new ReferencesLoadHandler();
- }
- else if (info.InstaceType == InstaceType.AssetBundle)
- {
- Instancer = new ExtralLoadHandler();
- }
- else if (info.InstaceType == InstaceType.ExtralURLDownload)
- {
- Instancer = new ExtralLoadHandler();
- }
- else if (info.InstaceType == InstaceType.LocalPath)
- {
- Instancer = new ExtralLoadHandler();
- }
- if (Instancer != null)
- {
- Instancer.SetData(info);
- }
- if (info.ArtType == ArtType.Model)
- {
- ArtDisPlay = new ModelPlayer();
- }
- else if (info.ArtType == ArtType.WorldDlg)
- {
- ArtDisPlay = new WorldDlgPlayer();
- }
- else if (info.ArtType == ArtType.Image)
- {
- ArtDisPlay = new PictruePlayer();
- }
- else if (info.ArtType == ArtType.Movies)
- {
- ArtDisPlay = new ArtVideoPlayer();
- }
- else if (info.ArtType == ArtType.Prefab)
- {
- ArtDisPlay = new PrefabPlayer(containerName);
- }
- else if (info.ArtType == ArtType.Model2D)
- {
- ArtDisPlay = new Model2DPlayer();
- }
- if (ArtDisPlay != null)
- {
- ArtDisPlay.SetData(info);
- }
- }
- }
- /// <summary>
- /// 同步加载资源
- /// </summary>
- /// <returns></returns>
- public virtual UnityEngine.Object LoadArt()
- {
- if (Instancer != null && ArtDisPlay != null)
- {
- if (IsNeedLoad())
- {
- return Instancer.LoadArt();
- }
- }
- 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 (Instancer != null && ArtDisPlay != null)
- {
- if (IsNeedLoad())
- {
- Instancer.LoadArtAsyn(path, process, loaded);
- return;
- }
- process?.Invoke(1);
- loaded?.Invoke(null);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public virtual UnityEngine.Object InstanceArt()
- {
- if (Instancer != null && ArtDisPlay != null)
- {
- if (IsNeedInstance())
- {
- return Instancer.InstanceArt();
- }
- }
- return null;
- }
- /// <summary>
- /// 添加到容器中
- /// </summary>
- /// <param name="obj"></param>
- public void AddToNode(UnityEngine.Object obj)
- {
- if (ArtDisPlay != null)
- {
- ArtDisPlay.AddToNode(obj);
- }
- }
- /// <summary>
- /// 获取图标
- /// </summary>
- /// <returns></returns>
- public Texture GetIcon()
- {
- if (artIcon)
- {
- return artIcon;
- }
- if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
- info.InstaceType == InstaceType.UnityBase) && Instancer != null)
- {
- if (info.ArtType == ArtType.Image)
- {
- var tex = Instancer.LoadArt();
- return artIcon = (tex as Texture);
- }
- else if (info.ArtType == ArtType.Movies)
- {
- return artIcon = Instancer.GetIcon();
- }
- else
- {
- return artIcon = Instancer.GetIcon();
- }
- }
- return artIcon;
- }
- /// <summary>
- /// 获取缩略图
- /// </summary>
- /// <param name="loadTex"></param>
- public void GetIcon(Action<Texture> loadTex)
- {
- if (artIcon)
- {
- loadTex?.Invoke(artIcon);
- return;
- }
- Instancer.GetIcon(loadTex);
- loadTex += (tex) => { artIcon = tex; };
- }
- /// <summary>
- /// 创建对应的组件
- /// </summary>
- /// <returns></returns>
- public ArtComponent InstanceComponent()
- {
- if (Instancer != null)
- {
- return Instancer.InstanceComponent();
- }
- return null;
- }
- public bool IsNeedLoad()
- {
- if (ArtDisPlay != null)
- {
- return ArtDisPlay.IsNeedLoad();
- }
- return false;
- }
- public bool IsNeedInstance()
- {
- if (ArtDisPlay != null)
- {
- return ArtDisPlay.IsNeedInstance();
- }
- return false;
- }
- /// <summary>
- /// 创建容器
- /// </summary>
- /// <returns></returns>
- public ArtContainer CreateContainer()
- {
- if (ArtDisPlay != null)
- {
- return ArtDisPlay.CreateContainer();
- }
- return null;
- }
- /// <summary>
- /// 创建一个美术展示容器
- /// </summary>
- public virtual ArtContainer CreateArt()
- {
- ///创建容器
- var container = CreateContainer();
- if (container == null)
- {
- UnityLog.LogError(info.ArtId + "__" + info.ArtType + "容器不存在!");
- return null;
- }
- AddToNode(container.GetInstace());
- ///资源的加载走异步处理
- if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
- info.InstaceType == InstaceType.UnityBase))
- {
- UnityEngine.Object obj = null;
- if (IsNeedLoad())
- {
- obj = LoadArt();
- }
- if (IsNeedInstance())
- {
- obj = InstanceArt();
- }
- if (obj is GameObject)
- {
- if (!(obj as GameObject).activeSelf)
- {
- (obj as GameObject).SetActive(true);
- }
- }
- container.AddToContainer(obj, info);
- var artComponent = InstanceComponent();
- container.AddArtComponent(artComponent);
- if (artComponent != null)
- {
- artComponent.SetContainer(container as ArtContainerHandler, obj, info);
- }
- }
- else
- {
- if (IsNeedLoad())
- {
- LoadArtAsyn("", container.UpdateProcess, (obj) =>
- {
- if (IsNeedInstance())
- {
- obj = InstanceArt();
- }
- if (obj is GameObject)
- {
- if (!(obj as GameObject).activeSelf)
- {
- (obj as GameObject).SetActive(true);
- }
- }
- container.AddToContainer(obj, info);
- var artComponent = InstanceComponent();
- container.AddArtComponent(artComponent);
- if (artComponent != null)
- {
- artComponent.SetContainer(container as ArtContainerHandler, obj, info);
- }
- });
- }
- }
- container.SetArtHandler(this);
- OnCreateArt?.Invoke(ArtDisPlay);
- return container;
- }
- /// <summary>
- /// 异步创建容器
- /// </summary>
- /// <param name="createAction"></param>
- public virtual void CreateArt(Action<ArtContainer> createAction)
- {
- }
- /// <summary>
- /// 是否已下载
- /// </summary>
- /// <returns></returns>
- public bool IsDownLoad()
- {
- if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
- info.InstaceType == InstaceType.UnityBase))
- {
- Instancer.DownProcess = 1;
- return true;
- }
- return Instancer.IsDownLoad();
- }
- public void DownLoad(Action<float, float> downProcess, Action<string, byte[]> downComplete)
- {
- Instancer.DownLoad(downProcess, downComplete);
- }
- }
- }
|