AssetBundle创建,加载,5.1版本

本文介绍如何使用Unity创建AssetBundles并加载资源。包括单独打包和批量打包AssetBundles的方法,以及如何根据不同平台加载这些资源。

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

using UnityEngine;
using System.Collections;
using UnityEditor;


//Assetbundle 创建
public class AssetBundleControl{
    
    //逐个打包   //不常用
    [MenuItem("Custom Editor/Create AssetBundles Main")]
    static void CreateAssetBundlesMain()
    {
        //获取选取的对象
        Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        
        //遍历对象
        foreach(var it in selectedAssets)
        {
            string sourcePath = AssetDatabase.GetAssetPath(it);


            //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
            //StreamingAssets是只读路径,不能写入
            //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。


            string targetPath = Application.dataPath + "/StreamingAssets/" + it.name + ".assetbundle";
            if(BuildPipeline.BuildAssetBundle(it,null,targetPath,BuildAssetBundleOptions.CollectDependencies))
            {
                Debug.Log(it.name+"资源打包成功");
            }
            else
            {
                Debug.Log(it.name+"资源打包失败");
            }
        }
        //刷新编辑器
        AssetDatabase.Refresh();
    }


    //打包在一起
    [MenuItem("Custom Editor/Create AssetBundles All")]
    static void CreateAssetBundlesAll()
    {
        Caching.CleanCache();   //清空缓存


        string path = Application.dataPath + "/StreamingAssets/All.assetbundle";


        Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        foreach (var it in selectedAssets)
        {
            //Debug.Log("create assetbundle name:" + it);
        }


        if (BuildPipeline.BuildAssetBundle(null, selectedAssets, path, BuildAssetBundleOptions.CollectDependencies))
        {
            AssetDatabase.Refresh();
            Debug.Log("打包所有");
        }
        else
        {


        }
    }
    
}





using UnityEngine;

using System.Collections;
using System.Collections.Generic;


//Assetbundle 生成资源
public class AssetBundle_LoadAsset:MonoBehaviour  {
    private static AssetBundle_LoadAsset instance;
    public static AssetBundle_LoadAsset Instance
    {
        get
        {
            if(instance==null)
            {
                Debug.Log("this must be added on the object");



                return null;
            }
            return instance;
        }
    }
    private AssetBundle_LoadAsset()
    {
        instance = this;
    }


    private GameObject preObj;   //还未生成的预制体
    private List<GameObject> preObjs; 


    //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
    public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    "file://" + Application.dataPath + "/StreamingAssets/";
#else
        string.Empty;
#endif


    void OnGUI()
    {
        //if(GUILayout.Button("单个"))
        //{
        //    LoadMainObject("test1.assetbundle");
        //}
        //if(GUILayout.Button("多资源的单个资源"))
        //{
        //    LoadTogetherObject_One("All.assetbundle", "test2");
        //}
        //if (GUILayout.Button("多资源的所有资源"))
        //{
        //    LoadTogetherObject_All("All.assetbundle");
        //}
    }


    //返回一个为生成的物体  包含一个资源的Assetbundle
    public GameObject LoadMainObject(string assetName)
    {
        preObj = null;
        StartCoroutine(LoadMainObject_(PathURL+assetName));
        return preObj;
    }


    //返回生成所有的资源   包含多个资源的Assetbundle
    public List<GameObject> LoadTogetherObject_All(string path)
    {
        if(preObjs!=null)
            foreach(var it in preObjs)        
               Destroy(it);        
        preObjs = null;
        preObjs = new List<GameObject>();
        StartCoroutine(LoadTogetherObject_All_(PathURL+path));
        return preObjs;        
    }


    //返回一个指定生成的物体  包含多个资源的Assetbundle
    public GameObject LoadTogetherObject_One(string path,string assetName)
    {
        preObj = null;
        StartCoroutine(LoadTogetherObject_One_(PathURL+path, assetName));
        return preObj;
    }


    private IEnumerator LoadMainObject_(string path)
    {
        WWW bundle = new WWW(path);


        yield return bundle;


        yield return preObj=Instantiate(bundle.assetBundle.mainAsset)as GameObject;
        bundle.assetBundle.Unload(false);
    }


    private IEnumerator LoadTogetherObject_All_(string path)
    {
        WWW bundle = new WWW(path);


        yield return bundle;


        foreach (var it in bundle.assetBundle.LoadAllAssets())
        {
            preObjs.Add(Instantiate(it)as GameObject);
        }
        bundle.assetBundle.Unload(false);
    }


    private IEnumerator LoadTogetherObject_One_(string path,string assetName)
    {
        WWW bundle = new WWW(path);


        yield return bundle;
                     
        yield return preObj =Instantiate(bundle.assetBundle.LoadAsset(assetName))as GameObject;
        bundle.assetBundle.Unload(false);
    }
}
CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器其他IC芯片。这款编程器的最新版本1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者专业工程师的一款实用工具,其强大的兼容性易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值