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*/
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lq1340817945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值