博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity批量更改assetbundle名称、清除名称打包脚本
阅读量:4147 次
发布时间:2019-05-25

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

设置Assetbundle名称是选中对应素材的文件夹,他会根据文件夹的名称命名bundle的名称,输出路径可自行更改,初始化的assetbundle.manifast主依赖列表的名称就是输出文件夹的名称

using UnityEngine;using System.Collections;using UnityEditor;using System.IO;using System.Collections.Generic;public class BuildAssetBundlesEditor : MonoBehaviour{    ///     /// AssetBundleManifestName == 对应AB依赖列表文件    ///     private static string AssetBundle_BuildDirectory_Path = @Application.streamingAssetsPath + "/../../../" + "AssetBundles";    private static string AssetBundle_TargetDirectory_Path = @Application.streamingAssetsPath + "/" + "ABFiles";    [MenuItem("Tools/Asset Bundle/Build Asset Bundles", false, 0)]    public static void BuildAssetBundleAndroid()    {        //Application.streamingAssetsPath对应的StreamingAssets的子目录        DirectoryInfo AB_Directory = new DirectoryInfo(AssetBundle_BuildDirectory_Path);        if (!AB_Directory.Exists)        {            AB_Directory.Create();        }        FileInfo[] filesAB = AB_Directory.GetFiles();        foreach (var item in filesAB)        {            Debug.Log("******删除旧文件:" + item.FullName + "******");            item.Delete();        }#if UNITY_ANDROID        BuildPipeline.BuildAssetBundles(AB_Directory.FullName, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);#elif UNITY_IPHONE        BuildPipeline.BuildAssetBundles(AB_Directory.FullName, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS);#endif        Debug.Log("******AssetBundle打包完成******");        Debug.Log("将要转移的文件夹是:" + AssetBundle_TargetDirectory_Path);        FileInfo[] filesAB_temp = AB_Directory.GetFiles();        DirectoryInfo streaming_Directory = new DirectoryInfo(AssetBundle_TargetDirectory_Path);        FileInfo[] streaming_files = streaming_Directory.GetFiles();        foreach (var item in streaming_files)        {            item.Delete();        }        AssetDatabase.Refresh();        foreach (var item in filesAB_temp)        {            if (item.Extension == "")            {                item.CopyTo(AssetBundle_TargetDirectory_Path + "/" + item.Name, true);            }        }        AssetDatabase.Refresh();        Debug.Log("******文件传输完成******");    }    private static string _dirName = "";    ///     /// 批量命名所选文件夹下资源的AssetBundleName.    ///     [MenuItem("Tools/Asset Bundle/Set Asset Bundle Name")]    static void SetSelectFolderFileBundleName()    {        UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered);        foreach (Object item in selObj)        {            string objPath = AssetDatabase.GetAssetPath(item);            DirectoryInfo dirInfo = new DirectoryInfo(objPath);            if (dirInfo == null)            {                Debug.LogError("******请检查,是否选中了非文件夹对象******");                return;            }            _dirName = dirInfo.Name;            string filePath = dirInfo.FullName.Replace('\\', '/');            filePath = filePath.Replace(Application.dataPath, "Assets");            AssetImporter ai = AssetImporter.GetAtPath(filePath);            ai.assetBundleName = _dirName;            SetAssetBundleName(dirInfo);        }        AssetDatabase.Refresh();        Debug.Log("******批量设置AssetBundle名称成功******");    }    static void SetAssetBundleName(DirectoryInfo dirInfo)    {        FileSystemInfo[] files = dirInfo.GetFileSystemInfos();        foreach (FileSystemInfo file in files)        {            if (file is FileInfo && file.Extension != ".meta" && file.Extension != ".txt")            {                string filePath = file.FullName.Replace('\\', '/');                filePath = filePath.Replace(Application.dataPath, "Assets");                AssetImporter ai = AssetImporter.GetAtPath(filePath);                ai.assetBundleName = _dirName;            }            else if (file is DirectoryInfo)            {                string filePath = file.FullName.Replace('\\', '/');                filePath = filePath.Replace(Application.dataPath, "Assets");                AssetImporter ai = AssetImporter.GetAtPath(filePath);                ai.assetBundleName = _dirName;                SetAssetBundleName(file as DirectoryInfo);            }        }    }    ///     /// 批量清空所选文件夹下资源的AssetBundleName.    ///     [MenuItem("Tools/Asset Bundle/Reset Asset Bundle Name")]    static void ResetSelectFolderFileBundleName()    {        UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);        foreach (UnityEngine.Object item in selObj)        {            string objPath = AssetDatabase.GetAssetPath(item);            DirectoryInfo dirInfo = new DirectoryInfo(objPath);            if (dirInfo == null)            {                Debug.LogError("******请检查,是否选中了非文件夹对象******");                return;            }            _dirName = null;            string filePath = dirInfo.FullName.Replace('\\', '/');            filePath = filePath.Replace(Application.dataPath, "Assets");            AssetImporter ai = AssetImporter.GetAtPath(filePath);            ai.assetBundleName = _dirName;            SetAssetBundleName(dirInfo);        }        AssetDatabase.Refresh();        Debug.Log("******批量清除AssetBundle名称成功******");           }}

转载地址:http://tljti.baihongyu.com/

你可能感兴趣的文章
火影394话情报
查看>>
关于应用程序和插件或DLL之间的关系
查看>>
黄鹤游
查看>>
关于函数调用得到传递参数的想法
查看>>
编程概述
查看>>
谈谈Windows程序中的字符编码
查看>>
CDSN上的一篇谈微软技术的帖子
查看>>
从subsystem开始概述NT内核
查看>>
眼见为实(1):C++基本概念在编译器中的实现
查看>>
编程实践
查看>>
介绍Windows的窗口、消息、子类化和超类化
查看>>
谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
查看>>
时标和历法
查看>>
浅谈文字编码和Unicode(上)
查看>>
浅谈文字编码和Unicode(下)
查看>>
编程之道与程序员境界
查看>>
SharpDevelop浅析_1_AddInTree
查看>>
SharpDevelop浅析_2_User Interface
查看>>
无边框窗体拖动大小
查看>>
程序员的灯下黑:重知识轻技术
查看>>