博客 / 詳情

返回

便捷打包Instant Asset腳本

使用説明

  1. 將腳本放在Assets/Editor目錄下
  2. 在需要打包同一個table的資源要放在同一個文件夾下,在Project中選中要打table的文件夾,右鍵->Build->Instant Assets

腳本


using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class CreateInstantAsset
{
    [MenuItem("Assets/Build/InstantAssets")]
    public static void CreateInstantAssets()
    {
        string[] selectedGUIDs = Selection.assetGUIDs;
        Debug.Log("Ready for build, jobs = " +  selectedGUIDs.Length);

        for(int i = 0; i < selectedGUIDs.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(selectedGUIDs[i]);
            Debug.Log("Initial build job, path = " + path);
            //tableName暫時默認文件夾名
            BuildInstantAsset(path);
        }
    }

    public static void BuildInstantAsset(string path, string tableName = "")
    {
        string[] pathFolders = path.Split('/');

        if(string.IsNullOrEmpty(tableName))
        {
            tableName = pathFolders[pathFolders.Length-1];
        }

        //構建路徑
        string  outputPath = Application.streamingAssetsPath + "/InstantAssets/" + pathFolders[pathFolders.Length-1];

        Debug.Log("start build job, outputPath = " +  outputPath + "\t" + "tableName = " + tableName);

        //如果沒有路徑則創建一個
        if (!System.IO.Directory.Exists(outputPath))
        {
            System.IO.Directory.CreateDirectory(outputPath);
        }

        //定義資源名列表
        InstantAssetAliasTable[] aliasTable = new InstantAssetAliasTable[]
        {
            //目錄方式指定資源
            new InstantAssetAliasTable
            {
                aliasTableName = tableName,
                buildDirectoryPath = path,   //示例,包含目錄下所有資源,指定所有ScriptableObjectData
                assetNames = null
            }
        };

        //設置構建選項
        InstantAssetOptions options = InstantAssetOptions.CompressionLz4HC | InstantAssetOptions.ForceRebuild;

        //調用構建方法
        bool success = InstantAssetEditorUtility.BuildAssetPacker(outputPath, aliasTable, options, 1024, EditorUserBuildSettings.activeBuildTarget);

        if (success)
        {
            Debug.LogFormat("build successed!output path: {0}", outputPath);
        }
        else
        {
            Debug.LogError("build failure!");
        }
    }

}

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.