欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity AssetBundle打包工具示例詳解

 更新時(shí)間:2021年10月14日 08:30:38   作者:小紫蘇xw  
這篇文章主要介紹了Unity AssetBundle打包工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Unity批量打AB包

為了資源熱更新,Unity支持將所有資源打包成AssetBundle資源,存放在SteamingAssets文件夾中;

在項(xiàng)目發(fā)布之前,需要將所有資源打包成.ab文件,動態(tài)加載;

在項(xiàng)目更新時(shí),替換.ab資源文件,即可完成熱更新;

ab文件在加載時(shí),會多一步解壓縮的過程,會增加性能消耗;

打包操作屬于編輯器拓展,所有腳本放在Eidtor文件夾下;

1.PathTool

根據(jù)不同平臺,獲取ab輸出和輸入路徑;

不同平臺的輸入輸出路徑不相同,ios,android,windows;《Unity資源文件夾介紹》

public class PathTools
{
    // 打包AB包根路徑
    public const string AB_RESOURCES = "StreamingAssets"; 
    
    // 得到 AB 資源的輸入目錄
    public static string GetABResourcesPath()
    {
        return Application.dataPath + "/" + AB_RESOURCES;
    }

    // 獲得 AB 包輸出路徑
    public static string GetABOutPath()
    {
        return GetPlatformPath() + "/" + GetPlatformName();
    }
    
    //獲得平臺路徑
    private static string GetPlatformPath()
    {
        string strReturenPlatformPath = string.Empty;

#if UNITY_STANDALONE_WIN
        strReturenPlatformPath = Application.streamingAssetsPath;
#elif UNITY_IPHONE
            strReturenPlatformPath = Application.persistentDataPath;
#elif UNITY_ANDROID
            strReturenPlatformPath = Application.persistentDataPath;
#endif
        
        return strReturenPlatformPath;
    }
    
    // 獲得平臺名稱
    public static string GetPlatformName()
    {
        string strReturenPlatformName = string.Empty;

#if UNITY_STANDALONE_WIN
        strReturenPlatformName = "Windows";
#elif UNITY_IPHONE
            strReturenPlatformName = "IPhone";
#elif UNITY_ANDROID
            strReturenPlatformName = "Android";
#endif

        return strReturenPlatformName;
    }
    
    // 返回 WWW 下載 AB 包加載路徑
    public static string GetWWWAssetBundlePath()
    {
        string strReturnWWWPath = string.Empty;

#if UNITY_STANDALONE_WIN
        strReturnWWWPath = "file://" + GetABOutPath();
#elif UNITY_IPHONE
            strReturnWWWPath = GetABOutPath() + "/Raw/";
#elif UNITY_ANDROID
            strReturnWWWPath = "jar:file://" + GetABOutPath();
#endif

        return strReturnWWWPath;
    }
}

2.CreateAB

功能:選中一個(gè)文件夾,將該文件夾中所有資源文件打包成AB文件;

主要邏輯:遍歷文件夾中所有文件,是文件的生成AssetBundleBuild存在鏈表中統(tǒng)一打包,是文件夾的遞歸上一步操作,將所有資源文件都放在listassets鏈表中;

官方Api:BuildPipeline.BuildAssetBundles統(tǒng)一打包所有資源;

public class CreateAB : MonoBehaviour
{
    private static string abOutPath;
    private static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>();
    private static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>();
    private static bool isover = false; //是否檢查完成,可以打包
    static private string selectPath;

    public static bool GetState()
    {
        return isover;
    }

    public static AssetBundleBuild[] GetAssetBundleBuilds()
    {
        return listassets.ToArray();
    }

    [MenuItem("ABTools/CreatAB &_Q", false)]
    public static void CreateModelAB()
    {
        abOutPath = Application.streamingAssetsPath;

        if (!Directory.Exists(abOutPath))
            Directory.CreateDirectory(abOutPath);

        UnityEngine.Object obj = Selection.activeObject;
        selectPath = AssetDatabase.GetAssetPath(obj);
        SearchFileAssetBundleBuild(selectPath);
        
        BuildPipeline.BuildAssetBundles(abOutPath,
            CreateAB.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
        Debug.Log("AssetBundle打包完畢");
    }

    [MenuItem("ABTools/CreatAB &_Q", true)]
    public static bool CanCreatAB()
    {
        if (Selection.objects.Length > 0)
        {
            return true;
        }
        else
            return false;
    }

這里為什么會紅我也不知道...

//是文件,繼續(xù)向下
    public static void SearchFileAssetBundleBuild(string path) 
    {
        DirectoryInfo directory = new DirectoryInfo(@path);
        FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos();
        listfileinfo.Clear();
        //遍歷所有文件夾中所有文件
        foreach (var item in fileSystemInfos)
        {
            int idx = item.ToString().LastIndexOf(@"\");
            string name = item.ToString().Substring(idx + 1);
            //item為文件夾,添加進(jìn)listfileinfo,遞歸調(diào)用
            if ((item as DirectoryInfo) != null)
                listfileinfo.Add(item as DirectoryInfo);
            
            //剔除meta文件,其他文件都創(chuàng)建AssetBundleBuild,添加進(jìn)listassets;
            if (!name.Contains(".meta"))
            {
                CheckFileOrDirectoryReturnBundleName(item, path + "/" + name);
            }
        }

        if (listfileinfo.Count == 0)
            isover = true;
        else
        {
            Debug.LogError(listfileinfo.Count);
        }
    }

    //判斷是文件還是文件夾
    public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) 
    {
        FileInfo fileInfo = fileSystemInfo as FileInfo;
        if (fileInfo != null)
        {
            string[] strs = path.Split('.');
            string[] dictors = strs[0].Split('/');
            string name = "";
            for (int i = 1; i < dictors.Length; i++)
            {
                if (i < dictors.Length - 1)
                {
                    name += dictors[i] + "/";
                }
                else
                {
                    name += dictors[i];
                }
            }

            string[] strName = selectPath.Split('/');
            AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
            assetBundleBuild.assetBundleName = strName[strName.Length - 1];
            assetBundleBuild.assetBundleVariant = "ab";
            assetBundleBuild.assetNames = new string[] {path};
            listassets.Add(assetBundleBuild);
            return name;
        }
        else
        {
            //遞歸調(diào)用
            SearchFileAssetBundleBuild(path);
            return null;
        }
    }
}

3.ClearABLable

打包時(shí)每個(gè)資源會添加一個(gè)標(biāo)簽,如果重復(fù)打包,需要清空才可再次打包,否則會失??;

使用官方API:AssetDatabase.RemoveUnusedAssetBundleNames();

因?yàn)樽⑨寣懙暮茉敿?xì),就不贅述了;

public class ClearABLable
{
    [MenuItem("ABTools/Remove AB Label")]
    public static void RemoveABLabel()
    {
        // 需要移除標(biāo)記的根目錄
        string strNeedRemoveLabelRoot = string.Empty;
        // 目錄信息(場景目錄信息數(shù)組,表示所有根目錄下場景目錄)
        DirectoryInfo[] directoryDIRArray = null;
        
        // 定義需要移除AB標(biāo)簽的資源的文件夾根目錄
        strNeedRemoveLabelRoot = PathTools.GetABResourcesPath();   

        DirectoryInfo dirTempInfo = new DirectoryInfo(strNeedRemoveLabelRoot);
        directoryDIRArray = dirTempInfo.GetDirectories();

        // 遍歷本場景目錄下所有的目錄或者文件
        foreach (DirectoryInfo currentDir in directoryDIRArray)
        {
            // 遞歸調(diào)用方法,找到文件,則使用 AssetImporter 類,標(biāo)記“包名”與 “后綴名”
            JudgeDirOrFileByRecursive(currentDir);
        }

        // 清空無用的 AB 標(biāo)記
        AssetDatabase.RemoveUnusedAssetBundleNames();
        // 刷新
        AssetDatabase.Refresh();

        // 提示信息,標(biāo)記包名完成
        Debug.Log("AssetBundle 本次操作移除標(biāo)記完成");
    }

    /// <summary>
    /// 遞歸判斷判斷是否是目錄或文件
    /// 是文件,修改 Asset Bundle 標(biāo)記
    /// 是目錄,則繼續(xù)遞歸
    /// </summary>
    /// <param name="fileSystemInfo">當(dāng)前文件信息(文件信息與目錄信息可以相互轉(zhuǎn)換)</param>
    private static void JudgeDirOrFileByRecursive(FileSystemInfo fileSystemInfo)
    {
        // 參數(shù)檢查
        if (fileSystemInfo.Exists == false)
        {
            Debug.LogError("文件或者目錄名稱:" + fileSystemInfo + " 不存在,請檢查");
            return;
        }

        // 得到當(dāng)前目錄下一級的文件信息集合
        DirectoryInfo directoryInfoObj = fileSystemInfo as DirectoryInfo; 
        // 文件信息轉(zhuǎn)為目錄信息
        FileSystemInfo[] fileSystemInfoArray = directoryInfoObj.GetFileSystemInfos();

        foreach (FileSystemInfo fileInfo in fileSystemInfoArray)
        {
            FileInfo fileInfoObj = fileInfo as FileInfo;

            // 文件類型
            if (fileInfoObj != null)
            {
                // 修改此文件的 AssetBundle 標(biāo)簽
                RemoveFileABLabel(fileInfoObj);
            }
            // 目錄類型
            else
            {
                // 如果是目錄,則遞歸調(diào)用
                JudgeDirOrFileByRecursive(fileInfo);
            }
        }
    }

    /// <summary>
    /// 給文件移除 Asset Bundle 標(biāo)記
    /// </summary>
    /// <param name="fileInfoObj">文件(文件信息)</param>
    static void RemoveFileABLabel(FileInfo fileInfoObj)
    {
        // AssetBundle 包名稱
        string strABName = string.Empty;
        // 文件路徑(相對路徑)
        string strAssetFilePath = string.Empty;

        // 參數(shù)檢查(*.meta 文件不做處理)
        if (fileInfoObj.Extension == ".meta")
        {
            return;
        }

        // 得到 AB 包名稱
        strABName = string.Empty;
        // 獲取資源文件的相對路徑
        int tmpIndex = fileInfoObj.FullName.IndexOf("Assets");
        // 得到文件相對路徑
        strAssetFilePath = fileInfoObj.FullName.Substring(tmpIndex); 
        
        // 給資源文件移除 AB 名稱
        AssetImporter tmpImportObj = AssetImporter.GetAtPath(strAssetFilePath);
        tmpImportObj.assetBundleName = strABName;
    }
}

4.拓展

更多的時(shí)候,我們打包需要一鍵打包,也可能需要多個(gè)文件打成一個(gè)ab包,只需要修改一下文件邏輯即可;

打ab包本身并不復(fù)雜,對文件路徑字符串的處理比較多,多Debug調(diào)試;

到此這篇關(guān)于Unity AssetBundle打包工具的文章就介紹到這了,更多相關(guān)Unity AssetBundle打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決C#中Linq GroupBy 和OrderBy失效的方法

    解決C#中Linq GroupBy 和OrderBy失效的方法

    最近發(fā)現(xiàn)了一個(gè)問題,在服務(wù)器端的Linq GroupBy 和OrderBy居然不管用,后來終于解決了所以現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒。
    2016-09-09
  • Unity ScrollView實(shí)現(xiàn)無限滑動效果

    Unity ScrollView實(shí)現(xiàn)無限滑動效果

    這篇文章主要為大家詳細(xì)介紹了Unity ScrollView實(shí)現(xiàn)無限滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C# double類型變量比較分析

    C# double類型變量比較分析

    這篇文章主要介紹了C# double類型變量比較分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#調(diào)用接口的四種方式介紹

    C#調(diào)用接口的四種方式介紹

    這篇文章介紹了C#調(diào)用接口的四種方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#?EF?Core可視化工具的使用及EF?Core入門語句操作代碼

    C#?EF?Core可視化工具的使用及EF?Core入門語句操作代碼

    EF?Core?可用作對象關(guān)系映射程序?(O/RM),以便于?.NET?開發(fā)人員能夠使用?.NET?對象來處理數(shù)據(jù)庫,這樣就不必經(jīng)常編寫大部分?jǐn)?shù)據(jù)訪問代碼了,接下來通過本文給大家介紹C#?EF?Core可視化工具的使用及EF?Core入門語句,感興趣的朋友一起看看吧
    2022-02-02
  • C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解

    C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解

    這篇文章主要為大家介紹了C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • C#實(shí)現(xiàn)驗(yàn)證字符串的長度的方法詳解

    C#實(shí)現(xiàn)驗(yàn)證字符串的長度的方法詳解

    這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)或者計(jì)算字符數(shù)組長度或字符串的長度來驗(yàn)證驗(yàn)證字符串的長度,感興趣的小伙伴可以學(xué)習(xí)一下
    2024-02-02
  • 淺談C#中的string駐留池

    淺談C#中的string駐留池

    這篇文章主要介紹了C#中的string駐留池的的相關(guān)資料,文中示例代碼非常細(xì)致,供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換

    C#實(shí)現(xiàn)字母與ASCII碼互相轉(zhuǎn)換

    ASCII是基于拉丁字母的編碼系統(tǒng),也是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),本文主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)字母與ASCII碼互轉(zhuǎn),需要的可以參考下
    2024-01-01
  • c# 用Base64實(shí)現(xiàn)文件上傳

    c# 用Base64實(shí)現(xiàn)文件上傳

    這篇文章主要介紹了c# 用Base64實(shí)現(xiàn)文件上傳的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08

最新評論