国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[游戲開發(fā)][Unity]Assetbundle打包篇(2)打包資源配置篇

這篇具有很好參考價(jià)值的文章主要介紹了[游戲開發(fā)][Unity]Assetbundle打包篇(2)打包資源配置篇。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

打包與資源加載框架目錄

正文

可視化配置的方式有很多種,Json、XML、以及Unity內(nèi)置的ScriptableObject序列化

配置文件里要有哪些內(nèi)容呢,很顯然,最重要的就是目標(biāo)文件路徑,其次是權(quán)重類型,權(quán)重類型有:必要打包型、被引用打包型、忽略類型。為何會(huì)有忽略類型呢,是因?yàn)槲覀冊O(shè)置目標(biāo)文件路徑是個(gè)文件夾,同文件夾內(nèi)可能有不想被打包的文件夾,因此,我們需要另開一條配置,把該子文件夾也設(shè)置進(jìn)去,并且權(quán)重類型設(shè)置為:忽略類型即可。被引用打包型比較好理解,雖然該資源在目標(biāo)文件路徑中,如果最終統(tǒng)計(jì)該資源被引用的次數(shù)等于0,說明該資源不需要進(jìn)包。

我選擇的是使用 ScriptableObject,其可視化內(nèi)容可以參考下圖。

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

我安裝了Odin Inspector插件所以Inspector面板是可以直接操作的,有需要自行安裝

Unity2020以后不用裝Odin,已經(jīng)可以在Unity上編輯了。

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔
unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

CollectionSetting顧名思義就是收集配置,請注意還有一些比較特殊的配置


PackRule設(shè)置

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

PackRule看三個(gè)變量就知道是對(duì)文件夾是否收集的操作

  1. Collect必須收集該文件夾

  1. Ignore必須忽略該文件夾

  1. Passive 該文件夾被引用時(shí)收集


LabelRule

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

這里要重點(diǎn)強(qiáng)調(diào)一下這個(gè)規(guī)則的作用,我們測試項(xiàng)目中,所有的資源都在一個(gè)根目錄下Assets/Works/Resource,看我時(shí)如何運(yùn)用LabelRule規(guī)則對(duì)根目錄下的子文件夾實(shí)現(xiàn)控制,如下圖所示

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

首先,根目錄Assets/Works/Resource設(shè)置為LabelByFilePath,意思是說,該目錄下的每一個(gè)子文件夾內(nèi)文件,都要單獨(dú)打成一個(gè)AB包,那疑問就來了,每個(gè)文件一個(gè)AB包,肯定太碎了。

所以就有了第二步操作,第二個(gè)操作就是設(shè)置根目錄的子文件夾類型Assets/Works/Resource/Sprite/BuffIcon 的LabelRule為LabelByFolderPath,意思是以文件夾路徑打包。子路徑的LabelRule可以覆蓋根路徑的LabelRule

還有一個(gè)重點(diǎn)提示的內(nèi)容就是,有些文件我們想以文件夾打包,但是!部分子文件太大了,比如好幾十M,因此還有個(gè)特殊選項(xiàng)LabelByFolderPathExceptBig,意思是,該文件夾還是打成一個(gè)整包,但是過大的文件,會(huì)根據(jù)自己的路徑生成一個(gè)單獨(dú)的AB包。

具體代碼請看下面的第三個(gè)方法


EncryptRule加密規(guī)則

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔
unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

加密規(guī)則的作用是,AB包打出來后,使用我們規(guī)定的加密方式,對(duì)AB包數(shù)據(jù)進(jìn)行處理,從而讓外部的人無法查看包內(nèi)容。這些加密方式都是自己定的,沒有特定的規(guī)則

Quick模式:往AB包二進(jìn)制數(shù)據(jù)前插一段數(shù)據(jù),等將來讀取的時(shí)候再把插入部分刪掉。插入部分你可以自己定義,就算塞入一個(gè)數(shù),也可以起到加密作用,只不過別人還是有辦法破解,我的Quick模式是用該AB包的Hash生成了一個(gè)二進(jìn)制key插進(jìn)去,加載AB包時(shí)用Hash再算出來這個(gè)key,把這個(gè)key刪掉讀包。


BundlePos設(shè)置

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

buildin模式啟動(dòng)時(shí)要熱更進(jìn)APP,ingame模式比較特殊,邊玩邊下載。


CollectionSettingData腳本是用來管理使用CollectionSetting配置的腳本,該CollectionSettingData提供了4個(gè)基本的方法供搜集資源時(shí)使用。

下面是CollectionSettingData的全部代碼,部分代碼在下面單獨(dú)講解


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


public static class CollectionSettingData
{
public static CollectionSetting Setting;
private static long bigSizeMB = 1024;

static CollectionSettingData()
{
? ? // 加載配置文件
? ? Setting = AssetDatabase.LoadAssetAtPath<CollectionSetting>(EditorDefine.CollectorSettingFilePath);
? ? if (Setting == null)
? ? {
? ? ? ? Debug.LogWarning($"Create new CollectionSetting.asset : {EditorDefine.CollectorSettingFilePath}");
? ? ? ? Setting = ScriptableObject.CreateInstance<CollectionSetting>();
? ? ? ? EditorTools.CreateFileDirectory(EditorDefine.CollectorSettingFilePath);
? ? ? ? AssetDatabase.CreateAsset(Setting, EditorDefine.CollectorSettingFilePath);
? ? ? ? AssetDatabase.SaveAssets();
? ? ? ? AssetDatabase.Refresh();
? ? }
? ? else
? ? {
? ? ? ? Debug.Log("Load CollectionSetting.asset ok");
? ? }
}

/// <summary>
/// 存儲(chǔ)文件
/// </summary>
public static void SaveFile()
{
? ? if (Setting != null)
? ? {
? ? ? ? EditorUtility.SetDirty(Setting);
? ? ? ? AssetDatabase.SaveAssets();
? ? }
}

/// <summary>
/// 添加元素
/// </summary>
public static void AddElement(string folderPath)
{
? ? if (IsContainsElement(folderPath) == false)
? ? {
? ? ? ? CollectionSetting.Wrapper element = new CollectionSetting.Wrapper();
? ? ? ? element.FolderPath = folderPath;
? ? ? ? Setting.Elements.Add(element);
? ? ? ? SaveFile();
? ? }
}

/// <summary>
/// 移除元素
/// </summary>
public static void RemoveElement(string folderPath)
{
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? if (Setting.Elements[i].FolderPath == folderPath)
? ? ? ? {
? ? ? ? ? ? Setting.Elements.RemoveAt(i);
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? SaveFile();
}

public static void AddI18NDir(string folderPath)
{
? ? if(!Setting.I18nDirectories.Contains(folderPath))
    {
? ? ? ? Setting.I18nDirectories.Add(folderPath);
? ? ? ? SaveFile();
? ? }
}
public static void RemoveI18NDir(string folderPath)
{
? ? for (int i = 0; i < Setting.I18nDirectories.Count; i++)
? ? {
? ? ? ? if (Setting.I18nDirectories[i] == folderPath)
? ? ? ? {
? ? ? ? ? ? Setting.I18nDirectories.RemoveAt(i);
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? SaveFile();
}

public static void AddInGameDir(string folderPath)
{
? ? if (!Setting.InGames.Contains(folderPath))
    {
? ? ? ? Setting.InGames.Add(folderPath);
? ? ? ? SaveFile();
? ? }
}
public static void RemoveInGameDir(string folderPath)
{
? ? for (int i = 0; i < Setting.InGames.Count; i++)
? ? {
? ? ? ? if (Setting.InGames[i] == folderPath)
? ? ? ? {
? ? ? ? ? ? Setting.InGames.RemoveAt(i);
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? SaveFile();
}

/// <summary>
/// 編輯元素
/// </summary>
public static void ModifyElement(string folderPath, CollectionSetting.EFolderPackRule packRule, CollectionSetting.EBundleLabelRule labelRule, EEncryptMethod encryptMethod, EAssetDeliveryMode deliveryMode, EBundlePos bundlePos)
{
? ? // 注意:這里強(qiáng)制修改忽略文件夾的命名規(guī)則為None
? ? if (packRule == CollectionSetting.EFolderPackRule.Ignore)
? ? ? ? labelRule = CollectionSetting.EBundleLabelRule.None;
? ? else if (labelRule == CollectionSetting.EBundleLabelRule.None)
? ? ? ? labelRule = CollectionSetting.EBundleLabelRule.LabelByFilePath;

? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? if (Setting.Elements[i].FolderPath == folderPath)
? ? ? ? {
? ? ? ? ? ? Setting.Elements[i].PackRule = packRule;
? ? ? ? ? ? Setting.Elements[i].LabelRule = labelRule;
? ? ? ? ? ? //Setting.Elements[i].EncryptRule = encryptMethod;
? ? ? ? ? ? //Setting.Elements[i].DeliveryMode = deliveryMode;
? ? ? ? ? ? Setting.Elements[i].BundlePos = bundlePos;
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? SaveFile();
}

/// <summary>
/// 是否包含元素
/// </summary>
public static bool IsContainsElement(string folderPath)
{
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? ? ? if (Setting.Elements[i].FolderPath == folderPath) return true;

? ? return false;
}

public static CollectionSetting.Wrapper GetElement(string folderPath)
{
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? ? ? if (Setting.Elements[i].FolderPath == folderPath) return Setting.Elements[i];

? ? return null;
}

/// <summary>
/// 獲取所有的打包路徑
/// </summary>
public static List<string> GetAllCollectPath()
{
? ? List<string> result = new List<string>();
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if (wrapper.PackRule == CollectionSetting.EFolderPackRule.Collect)
? ? ? ? ? ? result.Add(wrapper.FolderPath);
? ? }
? ? return result;
}

/// <summary>
/// 是否收集該資源
/// </summary>
public static bool IsCollectAsset(string assetPath)
{
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if (wrapper.PackRule == CollectionSetting.EFolderPackRule.Collect && assetPath.StartsWith(wrapper.FolderPath))
? ? ? ? ? ? return true;
? ? }

? ? return false;
}

/// <summary>
/// 是否忽略該資源
/// </summary>
public static bool IsIgnoreAsset(string assetPath)
{
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if (wrapper.PackRule == CollectionSetting.EFolderPackRule.Ignore && assetPath.StartsWith(wrapper.FolderPath))
? ? ? ? ? ? return true;
? ? }

? ? //XML
? ? //List<string> ignoreList = ConfigParser.GetIgnoreResList();
? ? //foreach (string name in ignoreList)
? ? //? ? if (assetPath.Contains(name)) return true;

? ? return false;
}


private static bool IsSubPath(string folderA, string assetPath)
{
? ? if(assetPath.StartsWith(folderA))
? ? ? ? return assetPath.Replace(folderA, "").StartsWith("/");

? ? return false;
}

public static EEncryptMethod GetEncryptRule(string assetPath)
{
? ? // 注意:一個(gè)資源有可能被多個(gè)規(guī)則覆蓋
? ? List<CollectionSetting.Wrapper> filterWrappers = new List<CollectionSetting.Wrapper>();
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if (IsSubPath(wrapper.FolderPath, assetPath))
? ? ? ? ? ? filterWrappers.Add(wrapper);
? ? }

? ? // 我們使用路徑最深層的規(guī)則
? ? CollectionSetting.Wrapper findWrapper = null;
? ? for (int i = 0; i < filterWrappers.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = filterWrappers[i];
? ? ? ? if (findWrapper == null)
? ? ? ? {
? ? ? ? ? ? findWrapper = wrapper;
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? if (wrapper.FolderPath.Length > findWrapper.FolderPath.Length)
? ? ? ? ? ? findWrapper = wrapper;
? ? }

? ? // 如果沒有找到命名規(guī)則
? ? if (findWrapper == null) return EEncryptMethod.None;

? ? return findWrapper.EncryptRule;
}
/// <summary>
/// 獲取資源的打包標(biāo)簽
/// </summary>
public static string GetAssetBundleLabel(string assetPath)
{
? ? // 注意:一個(gè)資源有可能被多個(gè)規(guī)則覆蓋
? ? List<CollectionSetting.Wrapper> filterWrappers = new List<CollectionSetting.Wrapper>();
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if (IsSubPath(wrapper.FolderPath, assetPath))
? ? ? ? ? ? filterWrappers.Add(wrapper);
    }

    // 我們使用路徑最深層的規(guī)則
    CollectionSetting.Wrapper findWrapper = null;
? ? for (int i = 0; i < filterWrappers.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = filterWrappers[i];
? ? ? ? if (findWrapper == null)
? ? ? ? {
? ? ? ? ? ? findWrapper = wrapper;
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? if (wrapper.FolderPath.Length > findWrapper.FolderPath.Length)
? ? ? ? ? ? findWrapper = wrapper;
? ? }

? ? // 如果沒有找到命名規(guī)則
? ? if (findWrapper == null) return assetPath.Remove(assetPath.LastIndexOf("."));
? ? 
? ? string labelName = "";
? ? // 根據(jù)規(guī)則設(shè)置獲取標(biāo)簽名稱
? ? if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.None)
? ? {
? ? ? ? // 注意:如果依賴資源來自于忽略文件夾,那么會(huì)觸發(fā)這個(gè)異常
? ? ? ? throw new Exception($"CollectionSetting has depend asset in ignore folder : {findWrapper.FolderPath}, asset : {assetPath}");
? ? ? ? // MotionLog.Log(ELogLevel.Log, $"CollectionSetting has depend asset in ignore folder : {findWrapper.FolderPath}, asset : {assetPath}");
? ? ? ? // labelName = assetPath.Remove(assetPath.LastIndexOf("."));
? ? }
? ? else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFileName)
? ? {
? ? ? ? labelName = Path.GetFileNameWithoutExtension(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "test"
? ? }
? ? else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFilePath)
? ? {
? ? ? ? labelName = assetPath.Remove(assetPath.LastIndexOf(".")); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config\test"
? ? }
? ? else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderName)
? ? {
? ? ? ? string temp = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
? ? ? ? labelName = Path.GetFileName(temp); // "C:\Demo\Assets\Config" --> "Config"
? ? }
? ? else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderPath)
? ? {
? ? ? ? labelName = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
? ? }
? ? else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderPathExceptBig)
? ? {
? ? ? ? long sizeMB = EditorTools.GetFileSize(assetPath) / 1024;
? ? ? ? if (sizeMB < bigSizeMB)
? ? ? ? ? ? labelName = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
? ? ? ? else
? ? ? ? ? ? labelName = assetPath.Remove(assetPath.LastIndexOf(".")); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config\test"
? ? }
? ? else if(findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByRootFolderPath)
    {
? ? ? ? labelName = findWrapper.FolderPath;
    }
? ? else
? ? {
? ? ? ? throw new NotImplementedException($"{findWrapper.LabelRule}");
? ? }

? ? ApplyReplaceRules(ref labelName);

? ? return labelName.Replace("\\", "/");
}

/// <summary>
/// 獲取資源的打包位置
/// </summary>
public static EBundlePos GetAssetBundlePos(string assetPath)
{
? ? // if (assetPath.Contains("Assets/WorksArt/Model/Live2D")
? ? // ? ?  || assetPath.Contains("Assets/Works/Resource/Audio/live2D")
? ? // ? ?  || assetPath.Contains("Assets/Works/Resource/Model/Live2D"))
? ? // {
? ? // ? ? return EBundlePos.ingame;
? ? // }
? ? List<string> buildInList = ConfigParser.GetBuildInResList();

? ? foreach(string path in Setting.InGames)
? ? {
? ? ? ? if (assetPath.Contains(path))
? ? ? ? {
? ? ? ? ? ? bool match = false;
? ? ? ? ? ? foreach (string name in buildInList)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (assetPath.Contains(name))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? match = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (match)
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? else
? ? ? ? ? ? ? ? return EBundlePos.ingame;
? ? ? ? }
? ? }

? ? // 注意:一個(gè)資源有可能被多個(gè)規(guī)則覆蓋
? ? List<CollectionSetting.Wrapper> filterWrappers = new List<CollectionSetting.Wrapper>();
? ? for (int i = 0; i < Setting.Elements.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = Setting.Elements[i];
? ? ? ? if(IsSubPath(wrapper.FolderPath, assetPath))
        {
? ? ? ? ? ? filterWrappers.Add(wrapper);
? ? ? ? }
    }

    // 我們使用路徑最深層的規(guī)則
    CollectionSetting.Wrapper findWrapper = null;
? ? for (int i = 0; i < filterWrappers.Count; i++)
? ? {
? ? ? ? CollectionSetting.Wrapper wrapper = filterWrappers[i];
? ? ? ? if (findWrapper == null)
? ? ? ? {
? ? ? ? ? ? findWrapper = wrapper;
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? if (wrapper.FolderPath.Length > findWrapper.FolderPath.Length)
? ? ? ? ? ? findWrapper = wrapper;
? ? }

? ? // 如果沒有找到命名規(guī)則
? ? if (findWrapper == null)
? ? {
? ? ? ? return EBundlePos.buildin;
? ? }
? ? 
? ? return findWrapper.BundlePos;
}

public static void ApplyReplaceRules(ref string name)
{
? ? if (name.Contains("TempLuaCode"))
? ? {
? ? ? ? name = name.Replace("TempLuaCode", "Lua");
? ? }
? ? if (name.Contains("Resource_min"))
? ? {
? ? ? ? name = name.Replace("Resource_min", "Resource");
? ? }
}

}

2:獲取所有資源配置信息,是一個(gè)很重要的接口,代碼在上面也有


    public static List<string> GetAllCollectPath()
        {
            List<string> result = new List<string>();
            for (int i = 0; i < Setting.Elements.Count; i++)
            {
                CollectionSetting.Wrapper wrapper = Setting.Elements[i];
                if (wrapper.PackRule == CollectionSetting.EFolderPackRule.Collect)
                    result.Add(wrapper.FolderPath);
            }
            return result;
        }

3:獲取該資源目錄的AssetbundleLabel,不知道AssetbundleLabel的小伙伴,看下圖

unity 打包策略,開發(fā)心得,項(xiàng)目管理,unity,游戲引擎,Powered by 金山文檔

/// <summary>
/// 獲取資源的打包標(biāo)簽
/// </summary>
public static string GetAssetBundleLabel(string assetPath)
{
    // 注意:一個(gè)資源有可能被多個(gè)規(guī)則覆蓋
    List<CollectionSetting.Wrapper> filterWrappers = new List<CollectionSetting.Wrapper>();
    for (int i = 0; i < Setting.Elements.Count; i++)
    {
        CollectionSetting.Wrapper wrapper = Setting.Elements[i];
        if (IsSubPath(wrapper.FolderPath, assetPath))
            filterWrappers.Add(wrapper);
    }

    // 我們使用路徑最深層的規(guī)則
    CollectionSetting.Wrapper findWrapper = null;
    for (int i = 0; i < filterWrappers.Count; i++)
    {
        CollectionSetting.Wrapper wrapper = filterWrappers[i];
        if (findWrapper == null)
        {
            findWrapper = wrapper;
            continue;
        }
        if (wrapper.FolderPath.Length > findWrapper.FolderPath.Length)
            findWrapper = wrapper;
    }

    // 如果沒有找到命名規(guī)則
    if (findWrapper == null) return assetPath.Remove(assetPath.LastIndexOf("."));
    
    string labelName = "";
    // 根據(jù)規(guī)則設(shè)置獲取標(biāo)簽名稱
    if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.None)
    {
        // 注意:如果依賴資源來自于忽略文件夾,那么會(huì)觸發(fā)這個(gè)異常
        throw new Exception($"CollectionSetting has depend asset in ignore folder : {findWrapper.FolderPath}, asset : {assetPath}");
        // MotionLog.Log(ELogLevel.Log, $"CollectionSetting has depend asset in ignore folder : {findWrapper.FolderPath}, asset : {assetPath}");
        // labelName = assetPath.Remove(assetPath.LastIndexOf("."));
    }
    else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFileName)
    {
        labelName = Path.GetFileNameWithoutExtension(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "test"
    }
    else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFilePath)
    {
        labelName = assetPath.Remove(assetPath.LastIndexOf(".")); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config\test"
    }
    else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderName)
    {
        string temp = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
        labelName = Path.GetFileName(temp); // "C:\Demo\Assets\Config" --> "Config"
    }
    else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderPath)
    {
        labelName = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
    }
    else if (findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByFolderPathExceptBig)
    {
        long sizeMB = EditorTools.GetFileSize(assetPath) / 1024;
        if (sizeMB < bigSizeMB)
            labelName = Path.GetDirectoryName(assetPath); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config"
        else
            labelName = assetPath.Remove(assetPath.LastIndexOf(".")); // "C:\Demo\Assets\Config\test.txt" --> "C:\Demo\Assets\Config\test"
    }
    else if(findWrapper.LabelRule == CollectionSetting.EBundleLabelRule.LabelByRootFolderPath)
    {
        labelName = findWrapper.FolderPath;
    }
    else
    {
        throw new NotImplementedException($"{findWrapper.LabelRule}");
    }

    ApplyReplaceRules(ref labelName);

    return labelName.Replace("\\", "/");
}

4:獲取該資源的BundlePos,BundlePos是什么,前文已經(jīng)介紹了文章來源地址http://www.zghlxwxcb.cn/news/detail-703361.html


public static EBundlePos GetAssetBundlePos(string assetPath)
{
// if (assetPath.Contains("Assets/WorksArt/Model/Live2D")
//      || assetPath.Contains("Assets/Works/Resource/Audio/live2D")
//      || assetPath.Contains("Assets/Works/Resource/Model/Live2D"))
// {
//     return EBundlePos.ingame;
// }
List<string> buildInList = ConfigParser.GetBuildInResList();

foreach(string path in Setting.InGames)
{
    if (assetPath.Contains(path))
    {
        bool match = false;
        foreach (string name in buildInList)
        {
            if (assetPath.Contains(name))
            {
                match = true;
                break;
            }
        }
        if (match)
            break;
        else
            return EBundlePos.ingame;
    }
}

// 注意:一個(gè)資源有可能被多個(gè)規(guī)則覆蓋
List<CollectionSetting.Wrapper> filterWrappers = new List<CollectionSetting.Wrapper>();
for (int i = 0; i < Setting.Elements.Count; i++)
{
    CollectionSetting.Wrapper wrapper = Setting.Elements[i];
    if(IsSubPath(wrapper.FolderPath, assetPath))
    {
        filterWrappers.Add(wrapper);
    }
}

// 我們使用路徑最深層的規(guī)則
CollectionSetting.Wrapper findWrapper = null;
for (int i = 0; i < filterWrappers.Count; i++)
{
    CollectionSetting.Wrapper wrapper = filterWrappers[i];
    if (findWrapper == null)
    {
        findWrapper = wrapper;
        continue;
    }
    if (wrapper.FolderPath.Length > findWrapper.FolderPath.Length)
        findWrapper = wrapper;
}

// 如果沒有找到命名規(guī)則
if (findWrapper == null)
{
    return EBundlePos.buildin;
}

return findWrapper.BundlePos;
}

到了這里,關(guān)于[游戲開發(fā)][Unity]Assetbundle打包篇(2)打包資源配置篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包