Unity打包模型方法

发表于2018-06-28
评论0 3.7k浏览
资源管理第一步是资源打包,传统的打包可以将所有物件制成预设Prefab,打包成场景。下面就和大家介绍下Unity中打包模型的方法。

模型导出

首先要根据不同的平台有不同的打包方法,在代码中要有平台的区分执行不同的打包代码,用到的材质类型要在GraphicsSettings中设置一下,不然特效或者模型的材质容易丢失,打包时选中Project中的预设体,点击Custom Editor就可以导出,Custom Editor在菜单栏中,这个类要放在Editor文件夹中。

代码如下:
        using UnityEngine;
        using System.Collections;
        using UnityEditor;
        public class Test : Editor
        {
            [MenuItem("Custom Editor/Create AssetBunldes Main")]
            static void CreateAssetBunldesMain ()
            {
                //获取在Project视图中选择的所有游戏对象
                Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
                //遍历所有的游戏对象
                foreach (Object obj in SelectedAsset) 
                {
                    string sourcePath = AssetDatabase.GetAssetPath (obj);
                    //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
                    //StreamingAssets是只读路径,不能写入
                    //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
                    //
        #if UNITY_ANDROID
                    string targetPath = "F:/" + obj.name + ".assetbundle";
        #elif UNITY_IPHONE
                    string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
        #endif
                    bool dabao =
        #if UNITY_ANDROID
                    BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);
        #elif UNITY_IPHONE
                    BuildPipeline.BuildAssetBundle(obj, null, targetPath,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iOS );
        #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
                    BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies) 
        #else
                    string.Empty;  
        #endif
                    if (dabao) {
                        Debug.Log(obj.name +"资源打包成功");
                    } 
                    else 
                    {
                        Debug.Log(obj.name +"资源打包失败");
                    }
                }
                //刷新编辑器
                AssetDatabase.Refresh ();   
            }
            }

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论