#define _DEBUG_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
namespace XericLibrary.Runtime.MacroLibrary
{
///
/// 加载宏
///
public static class MacroResources
{
#region 字段属性
public static readonly Dictionary FilePathName = new ()
{
// 图片
{ "Sprite","Sprite" },
{ "Texture2D","Sprite" },
// 声音
{ "Audio","Audio"},
// 字体
{ "Font","Font"},
// 脚本
{ "MonoBehaviour","Script"},
// 脚本
{ "RuntimeAnimatorController","Animation"},
// 材质
{ "PhysicMaterial","Material"},
{ "Material","Material"},
{ "Shader","Material"},
// 模型
{ "Mesh","Mesh"},
// 预制体
{ "GameObject","Prefab"},
// 场景
{ "Scene","Scene"},
};
private static System.Type CacheType;
private static UnityEngine.Object[] ResourceCache;
#endregion
#region 加载方法
///
/// 获取一个类型应该存在的根路径
///
///
///
///
public static bool GetFileRootPath(System.Type type, out string path)
{
var name = type.Name;
if(FilePathName.TryGetValue(name, out path))
return true;
#if _DEBUG_
Debug.LogError($"在Resources中加载{name}时并未找到一个有效的根路径");
#endif
return false;
}
///
/// 获取一个类型应该存在的根路径
///
///
///
///
public static bool GetFileRootPath(out string path)
{
return GetFileRootPath(typeof(T), out path);
}
///
/// 加载一个给定名称的特定文件类型的文件
///
///
///
///
public static bool LoadFile(this string fileName, out T result)
where T : UnityEngine.Object
{
var type = typeof(T);
if(!GetFileRootPath(type, out var path))
{
result = null;
return false;
}
if(CacheType != type)
{
CacheType = type;
ResourceCache = Resources.LoadAll(path);
}
result = ResourceCache.FirstOrDefault(a => a.name == fileName) as T;
return result != null;
}
#endregion
}
}