unity自定义创建资源包(代码片段)

来源:互联网 发布:vue.js和angularjs 编辑:程序博客网 时间:2024/06/16 03:40

                                                    自定义创建资源包

用unity写游戏时,为了提高游戏的运行效率,将一些资源打包,下面是自定义创建资源包代码片段,有注释不多说:
using UnityEngine;using System.Collections;using UnityEditor;using System.IO;public class CreateAssetBundles : MonoBehaviour {    [MenuItem("自定义资源包/创建资源包")]    static void ExecuteCreateAssetBundles()    {        //设置保存资源包的根路径;        string targetDir = Application.dataPath + "/AssetBundles";        Object[] selectedAssets = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);        if (!Directory.Exists(targetDir))        {            Directory.CreateDirectory(targetDir);        }        for (int i = 0; i < selectedAssets.Length; i++)        {            string filePath = targetDir + "/" + selectedAssets[i].name + ".unity3d";            if (File.Exists(filePath))            {                File.Delete(filePath);            }            //生成新的资源包文件            if (BuildPipeline.BuildAssetBundle(selectedAssets[i], null, filePath, BuildAssetBundleOptions.CollectDependencies))            {                //表示资源包文件生成成功;                Debug.Log("资源包文件生成成功");                //直接刷新Project视图,查看资源;                AssetDatabase.Refresh();            }            else {                //表示资源包文件生成失败;                Debug.Log("资源包文件生成失败");            }        }    }}

0 0
原创粉丝点击