首先先定义ILoader基类
using System;
using UnityEngine;
using Object = UnityEngine.Object;
public interface ILoader
{
GameObject LoadPrefab(string path);
GameObject LoadPrefabAndInstantiate(string path, Transform parent = null);
void LoadConfig(string path, Action<object> complete);
T Load<T>(string path) where T : Object;
T[] LoadAll<T>(string path) where T : Object;
}
继承自ILoader 的 Resources资源加载类
using System;
using System.Collections;
using UnityEngine;
using Object = UnityEngine.Object;
public class ResourceLoader : ILoader
{
public GameObject LoadPrefab(string path)
{
var prefab = Resources.Load<GameObject>(path);
return prefab;
}
public GameObject LoadPrefabAndInstantiate(string path, Transform parent = null)
{
var prefab = LoadPrefab(path);
var temp = Object.Instantiate(prefab, parent);
return temp;
}
public T Load<T>(string path) where T : Object
{
var sprite = Resources.Load<T>(path);
if (sprite == null)
{
Debug.LogError("未找到对应图片,路径:" + path);
return null;
}
return sprite;
}
public T[] LoadAll<T>(string path) where T : Object
{
var sprites = Resources.LoadAll<T>(path);
if (sprites == null || sprites.Length == 0)
{
Debug.LogError("当前路径下未找到对应资源,路径:" + path);
return null;
}
return sprites;
}
public void LoadConfig(string path, Action<object> complete)
{
CoroutineMgr.Single.ExecuteOnce(Config(path, complete));
}
private IEnumerator Config(string path, Action<object> complete)
{
if (Application.platform != RuntimePlatform.Android)
path = "file://" + path;
var www = new WWW(path);
yield return www;
if (www.error != null)
{
Debug.LogError("加载配置错误,路径为:" + path);
yield break;
}
complete(www.text);
Debug.Log("文件加载成功,路径为:" + path);
}
}
继承自ILoader 的AB包资源加载方式
using System;
using UnityEngine;
using Object = UnityEngine.Object;
public class ABLoader : ILoader
{
public GameObject LoadPrefab(string path)
{
throw new NotImplementedException();
}
public GameObject LoadPrefabAndInstantiate(string path, Transform parent = null)
{
throw new NotImplementedException();
}
public void LoadConfig(string path, Action<object> complete)
{
throw new NotImplementedException();
}
public T Load<T>(string path) where T : Object
{
throw new NotImplementedException();
}
public T[] LoadAll<T>(string path) where T : Object
{
throw new NotImplementedException();
}
}
LoadMgr 管理类,外部只需要调用管理类
using System;
using UnityEngine;
using Object = UnityEngine.Object;
public class LoadMgr : NormalSingleton<LoadMgr>, ILoader
{
[SerializeField] private readonly ILoader _loader;
public LoadMgr()
{
_loader = new ResourceLoader();
}
public GameObject LoadPrefab(string path)
{
return _loader.LoadPrefab(path);
}
public GameObject LoadPrefabAndInstantiate(string path, Transform parent = null)
{
return _loader.LoadPrefabAndInstantiate(path, parent);
}
public void LoadConfig(string path, Action<object> complete)
{
_loader.LoadConfig(path, complete);
}
public T Load<T>(string path) where T : Object
{
return _loader.Load<T>(path);
}
public T[] LoadAll<T>(string path) where T : Object
{
return _loader.LoadAll<T>(path);
}
}
调用示例
private void Init***()
{
LoadMgr.Single.LoadConfig(Application.streamingAssetsPath + "/Config/EnemyConfig.json", (value) =>
{
/*todo*/
Callback();
});
}
private void Callback()
{
/*todo*/
}