using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XRTool.Util;
namespace ShadowStudio.Model
{
///
/// 资源的处理类
/// 相同资源只会创建一个实例
///
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 OnCreateArt;
private Texture artIcon;
//private Action loadProcess;
//private Action artLoaded;
///
/// 初始化资源
///
///
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);
}
}
}
///
/// 同步加载资源
///
///
public virtual UnityEngine.Object LoadArt()
{
if (Instancer != null && ArtDisPlay != null)
{
if (IsNeedLoad())
{
return Instancer.LoadArt();
}
}
return null;
}
///
/// 异步加载资源
///
///
///
///
public virtual void LoadArtAsyn(string path, Action process, Action loaded)
{
if (Instancer != null && ArtDisPlay != null)
{
if (IsNeedLoad())
{
Instancer.LoadArtAsyn(path, process, loaded);
return;
}
process?.Invoke(1);
loaded?.Invoke(null);
}
}
///
///
///
///
public virtual UnityEngine.Object InstanceArt()
{
if (Instancer != null && ArtDisPlay != null)
{
if (IsNeedInstance())
{
return Instancer.InstanceArt();
}
}
return null;
}
///
/// 添加到容器中
///
///
public void AddToNode(UnityEngine.Object obj)
{
if (ArtDisPlay != null)
{
ArtDisPlay.AddToNode(obj);
}
}
///
/// 获取图标
///
///
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;
}
///
/// 获取缩略图
///
///
public void GetIcon(Action loadTex)
{
if (artIcon)
{
loadTex?.Invoke(artIcon);
return;
}
Instancer.GetIcon(loadTex);
loadTex += (tex) => { artIcon = tex; };
}
///
/// 创建对应的组件
///
///
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;
}
///
/// 创建容器
///
///
public ArtContainer CreateContainer()
{
if (ArtDisPlay != null)
{
return ArtDisPlay.CreateContainer();
}
return null;
}
///
/// 创建一个美术展示容器
///
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;
}
///
/// 异步创建容器
///
///
public virtual void CreateArt(Action createAction)
{
}
///
/// 是否已下载
///
///
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 downProcess, Action downComplete)
{
Instancer.DownLoad(downProcess, downComplete);
}
}
}