You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.6 KiB
C#
84 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Articy.Unity;
|
|
using Articy.Unity.Interfaces;
|
|
|
|
namespace Game
|
|
{
|
|
public class ResourceManager : ManagerBase<ResourceManager>
|
|
{
|
|
private Dictionary<string, Object> _resCache = new Dictionary<string, Object>();
|
|
private Dictionary<string, Object> _resDraftCache = new Dictionary<string, Object>();
|
|
public override void OnCreate()
|
|
{
|
|
}
|
|
public override void Update()
|
|
{
|
|
|
|
}
|
|
public override void OnDestroy()
|
|
{
|
|
|
|
}
|
|
public T Load<T>(string resName) where T : Object
|
|
{
|
|
if (!_resCache.ContainsKey(resName))
|
|
{
|
|
_resCache[resName] = Resources.Load(resName, typeof(T));
|
|
}
|
|
if (_resCache[resName] == null)
|
|
{
|
|
Util.Print("Resources没有该资源:", resName);
|
|
return default(T);
|
|
}
|
|
if (!(_resCache[resName] is T))
|
|
{
|
|
Util.Print("Resources资源类型错误:", resName, typeof(T), _resCache[resName].GetType());
|
|
return default(T);
|
|
}
|
|
return _resCache[resName] as T;
|
|
}
|
|
public T LoadDraft<T>(ArticyObject resObj) where T : Object
|
|
{
|
|
if (resObj is null)
|
|
{
|
|
Util.Print("Draft资源为空");
|
|
return default(T);
|
|
}
|
|
var resName = resObj.TechnicalName;
|
|
if (!(resObj is IAsset))
|
|
{
|
|
Util.Print("Draft资源类型错误:", resName);
|
|
}
|
|
if (!_resDraftCache.ContainsKey(resName))
|
|
{
|
|
_resDraftCache[resName] = ((IAsset)resObj).LoadAsset<Object>();
|
|
}
|
|
if (_resDraftCache[resName] == null)
|
|
{
|
|
Util.Print("Draft没有该资源:", resName);
|
|
return default(T);
|
|
}
|
|
if (!(_resDraftCache[resName] is T))
|
|
{
|
|
Util.Print("Draft资源类型错误:", resName, typeof(T));
|
|
return default(T);
|
|
}
|
|
return _resDraftCache[resName] as T;
|
|
}
|
|
|
|
}
|
|
|
|
abstract partial class Util
|
|
{
|
|
public static T Load<T>(string resName) where T : Object
|
|
{
|
|
return ResourceManager.Instance.Load<T>(resName);
|
|
}
|
|
public static T LoadDraft<T>(ArticyObject resObj) where T : Object
|
|
{
|
|
return ResourceManager.Instance.LoadDraft<T>(resObj);
|
|
}
|
|
|
|
}
|
|
} |