Unity学习第三篇之实现自动化挂载脚本的Resources资源加载模块

首先先定义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*/
    }
发布了16 篇原创文章 · 获赞 12 · 访问量 195

猜你喜欢

转载自blog.csdn.net/lq1340817945/article/details/105246861
今日推荐