• AssetBundle的使用


    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using System.IO;
    
    public class EditorPackageScript : EditorWindow
    {
        private static EditorPackageScript myWindow;
        [MenuItem("Resource/PackageWindow")]
        static void Package()
        {
            myWindow = EditorWindow.GetWindow(typeof(EditorPackageScript), false, "Bundle") as EditorPackageScript;
        }
    
        private Object[] SelelctObjs;
        void OnGUI()
        {
           if(GUI.Button(new Rect(0,20, Screen.width/2, 30),"PackageToOne"))
           {
               SelelctObjs = Selection.objects;
               PackageToOne();
           }
           if (GUI.Button(new Rect(0, 70, Screen.width / 2, 30), "PackageToEvery"))
           {
               SelelctObjs = Selection.objects;
               PackageToEvery();
           }
    
        }
    
        private void PackageToOne()
        {
            AssetBundleBuild[] bundles = new AssetBundleBuild[1];
            string[] names = new string[SelelctObjs.Length];
            for (int i = 0; i < SelelctObjs.Length; i++)
            {
                
                names[i] = AssetDatabase.GetAssetPath(SelelctObjs[i]);
            }
            bundles[0].assetBundleName = "SelectAllObjs";
            bundles[0].assetNames = names;
            if (!Directory.Exists(Application.dataPath + "/Package"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Package");
            }
            if(!Directory.Exists(Application.dataPath + "/Package/AllObjects"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Package/AllObjects");
            }
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/Package/AllObjects", bundles);
        }
    
        private void PackageToEvery()
        {
            AssetBundleBuild[] bundles = new AssetBundleBuild[SelelctObjs.Length];
            for (int i = 0; i < SelelctObjs.Length;i++)
            {
                string[] names = new string[1];
                names[0] = AssetDatabase.GetAssetPath(SelelctObjs[i]);
                bundles[i].assetNames = names;
                bundles[i].assetBundleName = SelelctObjs[i].name;
            }
            if (!Directory.Exists(Application.dataPath + "/Package"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Package");
            }
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/Package", bundles);
        }
    
    }
    

      

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class DownloadAssetDate : MonoBehaviour 
    {
        private string baseURL;
        void Start()
        {
            Caching.CleanCache();
            baseURL = "file:///" + Application.dataPath + "/Package/"; //基础路径
            StartCoroutine(LoadAssetDatas()); //加载各个打包的资源
            StartCoroutine(LoadAssetData());  //加载成打包成为一个的资源
            for(int i = 0; i < DataShareScript.LoadedData.Count; i++)
            {
                print(DataShareScript.LoadedData[0].name);
            }
        }
        
        
        IEnumerator LoadAssetDatas()
        {
            WWW www = WWW.LoadFromCacheOrDownload(baseURL + "Package", 1);
            yield return www;
            if (www.error != null) //出现错误信息
            {
                DataShareScript.errorMessage = www.error;
                yield break;
            }
            AssetBundle mainBundle = www.assetBundle;
             AssetBundleManifest mainfest = mainBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
             string[] mainObjs = mainfest.GetAllAssetBundles();
             for (int i = 0; i < mainObjs.Length; i++)
             {
                 www = WWW.LoadFromCacheOrDownload(baseURL + mainObjs[i], 1);
                 yield return www;
                 if (www.error != null) //出现错误信息
                 {
                     DataShareScript.errorMessage = www.error;
                     yield break;
                 }
                 AssetBundle bundle = www.assetBundle;
                 Object obj = bundle.LoadAsset(mainObjs[i]);
                 DataShareScript.LoadedData.Add(obj);
             }
        }
    
        IEnumerator LoadAssetData()
        {
            WWW www = WWW.LoadFromCacheOrDownload(baseURL + "AllObjects/AllObjects", 1);
            yield return www;
            if (www.error != null) //出现错误信息
            {
                DataShareScript.errorMessage = www.error;
                yield break;
            }
            AssetBundle bundle = www.assetBundle;
            AssetBundleManifest mainFest = bundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
            string[] names = mainFest.GetAllAssetBundles();
            for (int i = 0; i < names.Length; i++)
            {
                www = WWW.LoadFromCacheOrDownload(baseURL + "AllObjects/" + names[i], 1);
                yield return www;
                AssetBundle b = www.assetBundle;
                Object[] Obj = b.LoadAllAssets();
                for(int j = 0; j< Obj.Length;  j++)
                {
                    DataShareScript.LoadedData.Add(Obj[i]);
                }
            }
        }
    
        
    }
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class DataShareScript //用来让各个脚本之间的变量共享
    {
        public static string errorMessage;
        public static List<Object> LoadedData = new List<Object>();
    }
  • 相关阅读:
    一篇与面试官和蔼交流的深入了解JVM(JDK8)
    逆向工程,调试Hello World !程序(更新中)
    SpingBoot + Dubbo + Zookeeper实现简单分布式开发的应用
    Vue Axios 切片上传文件含实时进度
    Vue入门——常见指令及其详细代码示例
    女儿说要看烟花,但是政府规定不能放,程序员爸爸默默的拿起了键盘,程序员就是要为所欲为!
    逆向工程,调试Hello World !程序(更新中)
    python学习初始函数
    Python学习之装饰器
    Python学习之装饰器进阶
  • 原文地址:https://www.cnblogs.com/ZhiXing-Blogs/p/4997624.html
Copyright © 2020-2023  润新知