BuildAssetBundles文件打包


BuildAssetBundles文件打包:

public class BuildAssetBundlesTest : Editor

{

    //定義打包文件路徑

    public static string sourpath = Application.dataPath + "/Res";

    //窗口打包按鍵制作

    [MenuItem("XL/AssetBundle/BundleAssetsss")]

    //BuildAssetBundle方法(打包主方法)

    static void BuildAssetBundle()

    {

                  //調用方法清理原有打包文件名

        clearName();

                  //打包實現 方法

        pack(sourpath);

        string path = Application.streamingAssetsPath+"/haha";  //打包后文件路徑

            //判斷是否存在,有則進行刪除

        if (Directory.Exists(path) == false)

        {

            Directory.CreateDirectory(path);

        }

             //固定格式

        BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

    }

    //清理舊的打包文件

    static void clearName()

    {

        string[] oldName = AssetDatabase.GetAllAssetBundleNames();

        for (int i = 0; i < oldName.Length; i++)

        {

                      //刪除文件名

            AssetDatabase.RemoveAssetBundleName(oldName[i], true);

        }

    }

    //根據路徑進行打包

    static void pack(string source)

    {

         //字典存放數據

        DirectoryInfo folder = new DirectoryInfo(source);、

         //得到數據流

        FileSystemInfo[] files = folder.GetFileSystemInfos();

        int length = files.Length;

         //分文件夾分文件逐步打包

        for (int i = 0; i < length; i++)

        {

            if (files[i] is DirectoryInfo)

            {

                pack(files[i].FullName);

            }

            else

            {

                if (!files[i].Name.EndsWith(".meta"))

                {

                    substring(files[i].FullName);

                }

            }

        }

    }

    //得到想要的文件名格式

    static void substring(string sources)

    {

        string str01 = sources.Replace("\\", "/");

        string str02 = str01.Substring(Application.dataPath.Length - 6);

        string str03 = str01.Substring(Application.dataPath.Length + 1);

        AssetImporter assetImporter = AssetImporter.GetAtPath(str02);

        string str04 = str03.Substring(str03.IndexOf("/") + 1, str03.LastIndexOf("/") - str03.IndexOf("/")-2);

        assetImporter.assetBundleName = str04;

    }

}

 注:文件打包需要根據設置的路徑等進行設配修改文件打包的文件名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM