首先先定义ILoader基类
1 2 3 4 5 6 7 8 9 10 11 12 | 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资源加载类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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包资源加载方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 管理类,外部只需要调用管理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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); } } |
调用示例
1 2 3 4 5 6 7 8 9 10 11 12 | private void Init***() { LoadMgr.Single.LoadConfig(Application.streamingAssetsPath + "/Config/EnemyConfig.json", (value) => { /*todo*/ Callback(); }); } private void Callback() { /*todo*/ } |