搞了一天終於搞定u3d的場景打包,這樣就可以不用修改太多程序,把資源放在外部修改了。好處多多
但是本來很簡單的東西搞了一天,google來的說作為場景scene。unity 文件 打包成 unityd,結果一直都不行(時間就花在這里了,我相信他)
后來問了別人,別人說看文檔,是打包frefab,我一次,一下就行 了,我去
下面是打包的代碼
using UnityEngine; using System.Collections; using UnityEditor; public class assetPack : Editor { //打包單個 [MenuItem("Custom Editor/Create AssetBunldes Main")] static void CreateAssetBunldesMain () { //獲取在Project視圖中選擇的所有游戲對象 Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets); //遍歷所有的游戲對象 foreach (Object obj in SelectedAsset) { //本地測試:建議最后將Assetbundle放在StreamingAssets文件夾下,如果沒有就創建一個,因為移動平台下只能讀取這個路徑 //StreamingAssets是只讀路徑,不能寫入 //服務器下載:就不需要放在這里,服務器上客戶端用www類進行下載。 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 + "/eScene.unity3d"; string[] levels = { "Assets/myScene.unity" }; //打包場景 BuildPipeline.BuildPlayer( levels, Path,BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes); AssetDatabase.Refresh (); } [MenuItem("Custom Editor/Save Scene")] static void ExportScene() { // 打開保存面板,獲得用戶選擇的路徑 string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d"); if (path.Length != 0) { // 選擇的要保存的對象 Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); string[] scenes = { "Assets/myScene.unity" }; //打包 BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes); } AssetDatabase.Refresh(); } [MenuItem("Custom Editor/Save Scene2")] 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; } } }
下面是加載的代碼
using UnityEngine; using System.Collections; public class loadScene : MonoBehaviour { // Use this for initialization void Start () { StartCoroutine(loadScenee()); } // Update is called once per frame void Update () { } IEnumerator loadScenee() { string path; path = "file://" + Application.dataPath + "/StreamingAssets/eScene.unity3d"; Debug.Log(path); WWW www = new WWW(path); yield return www; AssetBundle bundle = www.assetBundle; //GameObject go = bundle.Load("gCube",typeof(GameObject)) as GameObject; GameObject ObjScene = Instantiate(www.assetBundle.mainAsset) as GameObject; bundle.Unload(false); } }
全做記憶
