using System.IO;
using System.Collections.Generic;
using UnityEngine;
public class AtlasManager
{
#region 私有变量
private static AtlasManager _instance;
private List<UIAtlas> _atlasList;
#endregion
#region 属性
//实例化
public static AtlasManager Instance
{
get
{
if (_instance == null) { _instance = new AtlasManager(); }
return _instance;
}
}
//获取图集数组
public UIAtlas[] AtlasArray
{
get
{
return _atlasList.ToArray();
}
set
{
_atlasList.CopyTo(value);
}
}
#endregion
//私有化构造函数
private AtlasManager()
{
_atlasList = new List<UIAtlas>();//List需要新建对象才能用
//获取图集List
DirectoryInfo direction = new DirectoryInfo("Assets/Resources/Atlas/");
FileInfo[] files = direction.GetFiles("*.prefab.meta", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string filePath = files[i].FullName;
filePath = filePath.Substring(filePath.LastIndexOf("Atlas")).Replace('\\', '/');//处理地址字符串
filePath = filePath.Substring(0, filePath.IndexOf('.'));//去掉后缀
try
{
_atlasList.Add(Resources.Load<UIAtlas>(filePath));//保存图集
}
catch
{
Debug.LogError("load atlas [" + filePath + "] error");
}
}
}
//获取图集
public UIAtlas GetAtlas(string atlasName)
{
foreach (UIAtlas atlas in _atlasList)
{
if (atlas.name == atlasName) return atlas;
}
return null;
}
}
AtlasManager.cs
最新推荐文章于 2022-11-23 14:20:13 发布