博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
u3d 场景资源打包
阅读量:4983 次
发布时间:2019-06-12

本文共 3938 字,大约阅读时间需要 13 分钟。

搞了一天终于搞定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);           }}

全做记忆

 

 

转载于:https://www.cnblogs.com/dragon2012/p/3845442.html

你可能感兴趣的文章
[面试没有回答上的问题4]常用字符串和数组的操作。
查看>>
WPF知识点全攻略09- 附加属性
查看>>
敏捷开发 流程 - 及产出
查看>>
关于SQL Server 2017中使用json传参时解析遇到的多层解析问题
查看>>
[转]SVN客户端解决authorization failed问题
查看>>
/etc/init.d目录和/etc/rc.local脚本
查看>>
Kubernetes StatefulSets
查看>>
用Python对html进行编码
查看>>
[转载]Java文件路径详解
查看>>
18.3.2从Class上获取信息(构造器)
查看>>
【TortoiseGit】TortoiseGit将本地库push到远端
查看>>
怎么样学习算法导论理论知识-算法何用
查看>>
Jenkins使用手册及总结
查看>>
Latest Common Ancestor
查看>>
getByClass--获取指定标签且class为指定的所有元素
查看>>
三点顺序
查看>>
拟物化设计与扁平化设计
查看>>
PS小实验-去除水印
查看>>
IS-IS完整笔记
查看>>
★如何解释特修斯之船问题?
查看>>