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>();
}
时间: 2024-10-02 21:02:56

AssetBundle的使用的相关文章

Assetbundle 打包加载及服务器加载等(采用unity3d5.0后的新版)

Assetbundle为资源包不是资源 打包1:通过脚本指定打包 AssetBundleBuild ab = new AssetBundleBuild { assetBundleName = PlayerSettings.bundleVersion + "@" + "zhao",//资源包assets的名字 assetNames = new string[1],  //包里的每个资源的名字 }; string outputPath = Path.Combine(Ut

unity 打包AssetBundle

unity提供了assetbunlde机制,下面介绍一种方法将指定目录下的所有文件打包成AssetBundle 先说明步骤,再上代码. 步骤一.选择要打包成assetbundle的目录,本案例使用assetbundle_raw 步骤二.把要打包的资源或者目录都放到assetbundle_raw目录,资源可以是 prefab,png,fbx,font,audio,txt等. 步骤三.给assetbundle_raw目录下所有的资源文件设置assetbundle名称,如果是目录,会递归. 名称格式:

Unity5 打包assetbundle

Assetbundle打包完成后出现的问题 1:Lightmap丢失? LightMap丢失的主要原来是场景中用到的各种系统自带Shader没有打包进去 Edit->Project Setting->Graphics 添加系统级Shader 2:  加载prefab后纹理丢失? 这个主要是加载顺序问题,先加载依赖文件,并调用www.assetbund

Unity3D之AssetBundle学习:Android上运行笔记

路径统一 在Android上加载StreamingAssets文件夹下的AssetBundle文件,首先需要对加载地址进行处理,注意PC.Android和IOS的地址不一致需要针对不同的平台不同的处理,通用代码如下: 1 //统一不同平台下 StreamingAssets 路径 2 public static readonly string STREAMING_ASSETS_PATH = 3 #if UNITY_ANDROID 4 "jar:file:///" + Applicatio

Unity3d 同步加载 Assetbundle 方法 AssetBundle.CreateFromFile

今天在群里问朋友有没有 同步加载 Assetbundle 的方法,同事给我指点 Unity3d 不知道在哪一个版本新增了一个 AssetBundle.CreateFromFile 的接口,这个接口是同步读取Assetbundle的.于是到帮助文档查询相关资料. http://docs.unity3d.com/ScriptReference/AssetBundle.CreateFromFile.html 文章转自http://blog.csdn.net/huutu 星环游戏 http://www.

Unity资源Assetbundle

转  Unity资源打包之Assetbundle 本文原创版权归 csdn janeky 所有,转载请详细注明原创作者及出处,以示尊重! 作者:janeky 原文:http://blog.csdn.net/janeky/article/details/17652021 如果这篇文章对你有帮助,敬请关注作者<Unity手游之路>系列教程. 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制成预设Prefab,打包成场景.今天我们来一起学习官方推荐的As

Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用

下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确,是可以保证资源完全没有泄露的. using UnityEngine; using System.Collections; public class TestAssetBundle : MonoBehaviour { public string AssetBundleName = "cube1.ass

Unity中Android加载assetbundle需要注意的两点问题

1.打包需要用Android的路径打包 using UnityEngine;using System.Collections;using UnityEditor; public class Test : Editor{ [MenuItem("Custom Editor/Create AssetBunldes Main")] static void CreateAssetBunldesMain() { //获取在Project视图中选择的所有游戏对象 Object[] SelectedA

Unity资源打包之Assetbundle

转  Unity资源打包之Assetbundle 本文原创版权归 csdn janeky 所有,转载请详细注明原创作者及出处,以示尊重! 作者:janeky 原文:http://blog.csdn.net/janeky/article/details/17652021 如果这篇文章对你有帮助,敬请关注作者<Unity手游之路>系列教程. 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制成预设Prefab,打包成场景.今天我们来一起学习官方推荐的As

AssetBundle杂谈

首先我们来看一下官方Build AssetBundle的示例代码: // C# Example // Builds an asset bundle from the selected objects in the project view. // Once compiled go to "Menu" -> "Assets" and select one of the choices // to build the Asset Bundle using Unit