unity 打包資源及網絡請求資源包


第一步 導包c#


在Assets新建一個Editor文件夾 緩存

新建一個Test類網絡

 

using UnityEngine;
using System.Collections;
using UnityEditor;
 
public class Test : Editor
{
    //打包單個
    [MenuItem("Custom Editor/Create AssetBunldes Main")]
    static void CreateAssetBunldesMain ()
    { 
        Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets); 
        foreach (Object obj in SelectedAsset) 
        { 
            string 	targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
            if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {
                Debug.Log(obj.name +"資源打包成功");
            } 
            else
            {
                Debug.Log(obj.name +"資源打包失敗");
            }
        }
        //刷新編輯器
        AssetDatabase.Refresh ();   
         
    }
     
    [MenuItem("Custom Editor/Create AssetBunldes ALL")]
    static void CreateAssetBunldesALL ()
    {
         
        Caching.CleanCache ();
  
 
        string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
  
         
        Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
  
        foreach (Object obj in SelectedAsset) 
        {
            Debug.Log ("Create AssetBunldes name :" + obj);
        } 
        if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
            AssetDatabase.Refresh ();
        }else{
             
        }
    }
     
    [MenuItem("Custom Editor/Create Scene")]
    static void CreateSceneALL ()
    {
        //清空一下緩存
        Caching.CleanCache();
        string Path = Application.dataPath + "/MyScene.unity3d";
        string  []levels = {"Assets/Level.unity"};
        //打包場景
        BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
        AssetDatabase.Refresh ();
    }
     
}


選擇須要導出的prefab 在菜單中找到Custom Editor編輯器

導出便可,導出的目標文件夾 爲StreamingAssets  函數

show in explore 就能夠找到導出的 assetbundleui


 網絡請求資源包spa


須要注意的是:調用網絡請求,須要使用協同 tartCoroutine(load());3d

load 函數以下code

private IEnumerator load() {
		WWW www = new WWW("http://localhost/model/Cube.assetbundle"); 
		yield return www;
		print("load complate:------------------------------------"+Time.time);
 
		
    }
相關文章
相關標籤/搜索