• 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>();
    }
  • 相关阅读:
    JS加载机制
    js原型和原型链(用代码理解代码)
    解决echarts图形由于label过长导致文字显示不全问题
    微信小程序setData()异常
    Add GNOME to a CentOS Minimal Install
    vim的tab缩进及用空格设置
    ORA-12547: TNS:lost contact
    python+Django实现Nagios自动化添加监控项目
    python <tab>自动补全
    电信服务规范
  • 原文地址:https://www.cnblogs.com/ZhiXing-Blogs/p/4997624.html
Copyright © 2020-2023  润新知