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
{
///
/// 资源实例化抽象类
/// 此类按照Resource方式进行简单实现
///
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;
///
/// 网络中下载到本地的文件路径
///
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; }
///
/// 获取缩略图
///
///
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(Info.Icon);
}
}
return artIcon;
}
///
/// 实例化物体
///
///
public virtual UnityEngine.Object InstanceArt()
{
if (LoadCache)
{
return GameObject.Instantiate(LoadCache);
}
return null;
}
///
/// 同步加载资源
///
///
public virtual UnityEngine.Object LoadArt()
{
if (Info != null)
{
return LoadCache = Resources.Load(Info.Url);
}
return null;
}
///
/// 异步加载资源
///
///
///
///
public virtual void LoadArtAsyn(string path, Action process, Action loaded)
{
if (Info != null)
{
TimerMgr.Instance.StartCoroutine(LoadAsyn(path, process, loaded));
}
else
{
LoadCache = null;
process?.Invoke(1);
loaded?.Invoke(LoadCache);
}
}
///
/// 异步加载
///
///
///
///
///
public virtual IEnumerator LoadAsyn(string path, Action process, Action 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);
}
///
/// 初始化数据
///
///
public virtual void SetData(ArtInfo info, string containerName = "")
{
this.Info = info;
}
///
/// 创建资源对应的组件
///
///
public virtual ArtComponent InstanceComponent()
{
if (Info != null && !string.IsNullOrEmpty(Info.Component))
{
return ArtInfoMgr.Instance.InstanceComponent(Info.Component);
}
return null;
}
///
/// 获取外部资源的缩略图
/// 如果是模型,获取icon的图片资源
/// 如果是图片,直接获取本身url作为缩略图
/// 如果是视频,则自行获取对应的缩略图:使用VideoPlayer读取对应帧,此处不提供方法
///
///
public virtual void GetIcon(Action 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 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;
}
///
/// 判断是否已下载指定文件
///
///
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 downProcess, Action 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);
}
));
}
}
}