最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity AssetBundle打包工具示例詳解

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

Unity批量打AB包

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

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

在項目更新時,替換.ab資源文件,即可完成熱更新;

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

打包操作屬于編輯器拓展,所有腳本放在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

功能:選中一個文件夾,將該文件夾中所有資源文件打包成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為文件夾,添加進listfileinfo,遞歸調(diào)用
            if ((item as DirectoryInfo) != null)
                listfileinfo.Add(item as DirectoryInfo);
            
            //剔除meta文件,其他文件都創(chuàng)建AssetBundleBuild,添加進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

打包時每個資源會添加一個標簽,如果重復(fù)打包,需要清空才可再次打包,否則會失?。?/p>

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

因為注釋寫的很詳細,就不贅述了;

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

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

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

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

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

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

        // 得到當前目錄下一級的文件信息集合
        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 標簽
                RemoveFileABLabel(fileInfoObj);
            }
            // 目錄類型
            else
            {
                // 如果是目錄,則遞歸調(diào)用
                JudgeDirOrFileByRecursive(fileInfo);
            }
        }
    }

    /// <summary>
    /// 給文件移除 Asset Bundle 標記
    /// </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.拓展

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

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

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

相關(guān)文章

  • C#靜態(tài)代碼織入AOP組件之Rougamo的使用詳解

    C#靜態(tài)代碼織入AOP組件之Rougamo的使用詳解

    Rougamo是一個靜態(tài)代碼織入的AOP組件,同為AOP組件較為常用的有Castle、Autofac、AspectCore等,下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧
    2024-01-01
  • C#實現(xiàn)發(fā)送手機驗證碼功能

    C#實現(xiàn)發(fā)送手機驗證碼功能

    之前基于c#實現(xiàn)手機發(fā)送驗證碼功能很復(fù)雜,真正做起來也就那回事,不過就是一個post請求就可以實現(xiàn)的東西,今天小編把思路分享到腳本之家平臺,供大家參考下
    2017-06-06
  • 使用c#實現(xiàn)微信自動化功能

    使用c#實現(xiàn)微信自動化功能

    這篇文章主要介紹了使用c#實現(xiàn)微信自動化,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • C#直線的最小二乘法線性回歸運算實例

    C#直線的最小二乘法線性回歸運算實例

    這篇文章主要介紹了C#直線的最小二乘法線性回歸運算方法,實例分析了給定一組點,用最小二乘法進行線性回歸運算的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#實現(xiàn)數(shù)獨解法

    C#實現(xiàn)數(shù)獨解法

    這篇文章介紹了C#實現(xiàn)數(shù)獨解法的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • c#實現(xiàn)一元二次方程求解器示例分享

    c#實現(xiàn)一元二次方程求解器示例分享

    這篇文章主要介紹了c#實現(xiàn)一元二次方程求解器示例,需要的朋友可以參考下
    2014-03-03
  • C# 6.0的屬性(Property)的語法與初始值詳解

    C# 6.0的屬性(Property)的語法與初始值詳解

    下面小編就為大家?guī)硪黄狢# 6.0的屬性(Property)的語法與初始值詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • C#迭代器方法介紹

    C#迭代器方法介紹

    這篇文章主要介紹了C#迭代器方法,可以使用foreach循環(huán)語句進行的迭代的方法,稱為可迭代方法,或者迭代器方法,方法操作,想了解更多內(nèi)容得小伙伴可以學(xué)習(xí)下面文章內(nèi)容,希望給你的學(xué)習(xí)帶來幫助
    2022-03-03
  • C#中AutoResetEvent控制線程用法小結(jié)

    C#中AutoResetEvent控制線程用法小結(jié)

    本文主要來自一道面試題,由于之前對AutoResetEvent的概念比較模糊,面試題題目很簡潔:兩個線程交替打印0~100的奇偶數(shù),你可以先動手試試,我主要是嘗試在一個方法里面完成這個任務(wù),需要的朋友可以參考下
    2022-07-07
  • C#編程實現(xiàn)發(fā)送郵件的方法(可添加附件)

    C#編程實現(xiàn)發(fā)送郵件的方法(可添加附件)

    這篇文章主要介紹了C#編程實現(xiàn)發(fā)送郵件的方法,具備添加附件的功能,涉及C#文件傳輸及郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11

最新評論

金湖县| 双牌县| 麟游县| 夏津县| 兴海县| 皋兰县| 甘德县| 汝阳县| 成武县| 贵州省| 龙泉市| 邵阳市| 三明市| 岳普湖县| 冕宁县| 大悟县| 绥江县| 永登县| 平原县| 淮滨县| 汾西县| 宁河县| 泽库县| 乌鲁木齐县| 海林市| 贺州市| 琼结县| 桂平市| 通辽市| 淮阳县| 邵阳县| 邵阳县| 永康市| 博兴县| 思茅市| 佳木斯市| 宣恩县| 洱源县| 徐闻县| 清水县| 嘉定区|