Unity3D加载项目资源的方法归纳

本文详细介绍了Unity中五种不同的资源加载方法,包括同步和异步加载,从资源文件夹和AssetBundle中加载资源,以及如何使用回调进行异步加载。每种方法都有其优缺点,适用于不同的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// SingletonMonoAuto<LoadObjectManager>可换成MonoBehaviour
/// 同步的优点:管理方便,资源准备好可以及时返回。缺点:没有异步快。
///异步的优点:速度快与主线程无关。缺点:调用比较麻烦,最好的做法是使用回调。
/// </summary>
public class LoadObjectManager : SingletonMonoAuto<LoadObjectManager>
{
    /// <summary>
    /// 用Resources.Load()来加载在项目Resource文件夹下的资源文件
    /// 例 "Prefab/Bullet"
    /// Resources文件夹内的资源只读,所以想要动态更新的资源不要放在这里。
    /// 在Resources里面一般放Prefab,在打包的时候会自动过滤掉不需要的资源
    /// </summary>
    /// <param name="prefabPath">prefab的路径</param>
    /// <param name="root">创建的新对象的父级对象</param>
    /// <returns></returns>
    public T LoadSolution1<T>(string prefabPath, Transform root) where T : UnityEngine.Object
    {
        T prefabObject = Resources.Load<T>(prefabPath);
        T obj = Instantiate(prefabObject, root);
        return obj;
    }

    /// <summary>
    /// 从本地文件夹中使用AssetBundle同步加载(无需用协程), 只允许加载当前设备允许访问的路径
    /// 例 "AssetBundles/Bullet.unity3d"
    /// </summary>
    /// <param name="resourcePath">资源文件在项目的路径</param>
    /// <param name="resourceName">资源文件的名字</param>
    /// <param name="root">创建的新对象的父级对象</param>
    /// <returns></returns>
    public T LoadSolution2<T>(string resourcePath, string resourceName, Transform root) where T : UnityEngine.Object
    {
        AssetBundle ab = AssetBundle.LoadFromFile(resourcePath);

        T prefabObject = ab.LoadAsset<T>(resourceName);
        T obj = GameObject.Instantiate(prefabObject, root);
        return obj;
    }

    /// <summary>
    /// 异步从本地或服务器使用WWW下载、加载到项目中,并从asset
    /// bundle中提取其中一个资源对象实例一个新的对象。 
    /// 例 @"http://localhost/AssetBundles/Bullet.unity3d" 
    /// 或 @"file:/E:AssetBundleProject\AssetBundleProject\Bullet"
    /// 注:此方法在2019版本中已弃用
    /// </summary>
    /// <param name="resourceUrl">资源在本地或者服务器的路径</param>
    /// <param name="prefabName">资源的名字</param>
    /// <returns></returns>
    IEnumerator LoadSolution3<T>(string resourceUrl, string prefabName) where T : UnityEngine.Object
    {
        while (Caching.ready == false)
        {
            yield return null;
        }

        WWW www = WWW.LoadFromCacheOrDownload(resourceUrl, 1);
        //yield return www;

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }

        T prefabObject = www.assetBundle.LoadAsset<T>(prefabName);
        T obj = Instantiate(prefabObject);
        yield return obj;
    }

    /// <summary>
    /// 从本地异步加载资源,增加回调
    /// </summary>
    /// <param name="resourcePath">资源在本地的路径</param>
    /// <param name="prefabName">资源的名字</param>
    /// <returns></returns>
    IEnumerable LoadSolution4<T>(string resourcePath, string prefabName) where T : UnityEngine.Object
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(resourcePath);
        yield return request;

        AssetBundle ab = request.assetBundle;
        T prefabObject = ab.LoadAsset<T>(prefabName);
        T obj = Instantiate(prefabObject) as T;
        yield return obj;
    }

    /// <summary>
    /// 用UnityWebRequest访问服务器获得assetbundle
    /// </summary>
    /// <param name="resourceUrl"></param>
    /// <param name="prefabName"></param>
    /// <returns></returns>
    IEnumerable LoadSolution5<T> (string resourceUrl, string prefabName) where T : UnityEngine.Object
    {
        UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(resourceUrl);
        yield return webRequest.SendWebRequest();

        if (webRequest.isHttpError || webRequest.isNetworkError)
            Debug.Log(webRequest.error);
        else
        {
            Debug.Log(webRequest.downloadHandler.text);
        }

        //AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(webRequest);
        T prefabObject = ab.LoadAsset<T>(prefabName);
        T obj = Instantiate(prefabObject) as T;
        yield return obj;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值