天天看點

[Unity3d]安卓無法加載assetbundle的問題

前幾天做了AssentBundle的例子,遇到了問題,在論壇上問了三天都沒人解答,最後在一個朋友的幫助下解決了。下面介紹AssentBundle。

看了雨凇MOMO的關于 AssetBundles的文章,受益很多,但是他把資源全達成.assetbundle 二進制格式,與Unity幫助腳本中不一樣,Unity幫助文檔中打包和加載的檔案字尾名都是.unity3d格式,而且網上其他人也都用的.unity格式,是以我也就打成.unity3d格式。

打包AssentBundle:

最核心的方法其實就它:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)

不過這是預設的電腦上打包的資源隻可以電腦上用

安卓打包需要添加參數:

BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android);

Iphone打包也需要添加參數:

 BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.iPhone);

需要在項目檔案根目錄Assets下建立Editor檔案夾,建立C#腳本“ExportAssetBundles”。

using UnityEngine;

using System.Collections;

using UnityEditor;//必須引用此類

public class ExportAssetBundles : MonoBehaviour

{

    /// <summary>

    /// 将所選擇的的物體和物體有依賴關系的對象一起打包

    /// </summary>

    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]

    static void ExportResource()

    {

        // Bring up save panel

        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");

        if (path.Length != 0)

        {

            // Build the resource file from the active selection.

            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies |

BuildAssetBundleOptions.CompleteAssets);

            Selection.objects = selection;

        }

    }

    /// 隻打包選擇的物體

    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]

    static void ExportResourceNoTrack()

            BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);

}

儲存之後,選中項目中的檔案,滑鼠右鍵或者Assent菜單下都會有這兩個選項,根據自己的需要打包即可。

下載下傳AssentBundle:Assetbundle是可以同時放在伺服器或者本地的,無論放在哪裡兩種下載下傳讀取的方式是完全一樣的。是以我建議在做unity項目的時候開始就把資源放在Assetbundle中在本地來做,等做的差不多了直接把Assetbundle放在伺服器上,因為兩種讀取的方式完全一樣,這樣以後更新資源會友善很多。 using UnityEngine;

using System.Collections;public class assentBuntleScript : MonoBehaviour 

{    

    public static readonly string URL = http://"+"網址";網絡路徑

    //public static readonly string URL = "file://" + Application.dataPath+"/mm.unity3d";//本地路徑    void OnGUI()

        if(GUILayout.Button("下載下傳資源"))

            StartCoroutine(loadBundleOBject(URL));

    }    IEnumerator loadBundleOBject(string url)

        WWW date = new WWW(url);

        yield return date;

        Instantiate(date.assetBundle.mainAsset);//執行個體化加載的資源,我加載的是個模型。

        date.assetBundle.Unload(false);

下載下傳類WWW

WWW bundle = new WWW(path);

這樣的做法是通過一個路徑進行下載下傳(無論是伺服器路徑還是本地路徑下載下傳操作都一樣)但是bundle隻能儲存在記憶體中,也就是退出遊戲在進入還得重新下,很顯然在遊戲中我們不能使用這種方式。

IEnumerator loadBundleOBject(string url)

        Assetbundle 中的腳本,在移動平台下Assetbundle裡面放的腳本是不會被執行的,在手機上将Assetbundle下載下傳到本地後,加載進遊戲中Prefab會自動在本地找它身上挂着的腳本,他是根據腳本的名來尋找,如果本地有這條腳本的話,Prefab會把這個腳本重新綁定在自身,并且會把打包前的參數傳遞進來。如果本地沒有,身上挂的條腳本永遠都不會被執行。(轉自雨松MOMO研究院)

最後再說說我遇到的問題,本地加載.unity3d的資源沒問題,但是我挂到IIS上就失敗了,顯示404錯誤,找不到資源,無法執行個體化。這就需要配置伺服器的MIME了,因為伺服器不識别.unity3d格式的資源,是以伺服器不響應請求,是以就下不下來。

    這樣就行了,可以下載下傳.unity3d,這就是困擾我三天的問題啊,怎麼都下載下傳不了,最終是這麼個問題。

[Unity3d]安卓無法加載assetbundle的問題
[Unity3d]安卓無法加載assetbundle的問題

編譯目标:

[Unity3d]安卓無法加載assetbundle的問題

本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366116,如需轉載請自行聯系原作者

繼續閱讀