Unity AssetBundle 踩坑記錄


Unity AssetBundle 踩坑記錄


editor 下選擇什么平台的 ab 加載

Material doesn't have a color property '_Color'
UnityEditor.DockArea:OnGUI()

Material doesn't have a float or range property 'PixelSnap'
UnityEditor.DockArea:OnGUI()

因為editor模式下所有的 platform ab 都是可以用的 並且打 android ab 包必須要把platform轉換為android
但是自己寫的cg shader 如果打成 android ab 在editor 模式下沒有OpenGL 2.0 環境會報錯
只有打成windows包

但是好像其他 surface shader 是可以的

參考

關於打包shader丟失參數的問題


The asset bundle ' ' can't be loaded because another asset bundle with the same files are already loaded

create scene assetbundle Scenes/battle/MapEditor/53.unityin _LoadImmediate failed

可能的原因

解決方法

后期使用asset guid 來給ab命名 ab的asset name path 隨意 guid 也行 相對於 asset path 也行

不同機子打包的ab md5 值不同 無語了


打 ab 最好把腳本打進去 有概率會出現看不見的情況


如果在同一個場景對ab做清理對話 會對當時的場景造成當時就材質丟失的情況 就是出現閃一下紅色

最好就是弄個東西擋一下 比如ui 或者 弄一個單獨加載場景的場景 在擋着的時候進行卸載

load sprite from ab

[MenuItem("Build/Tools/Test")]
public static void TestSceneAssets() {
    UnityEngine.Object[] assets = 
        new UnityEngine.Object[] { 
             AssetDatabase.LoadAssetAtPath("Assets/Resources/Icon/ectype_chapter/1.png",
                 typeof(UnityEngine.Object)) };
    UnityEngine.Object mainAsset = assets[0];

    string savePath =
        "f:/assetbundles/windows/"
            + "d4fe2a6e186849f4086398d2ceeb0510"
                + ResourceCommon.assetbundleFileSuffix;
    BuildPipeline.BuildAssetBundle(
        mainAsset, assets, savePath,
            BuildAssetBundleOptions.CollectDependencies 
                | BuildAssetBundleOptions.CompleteAssets
                    | BuildAssetBundleOptions.DeterministicAssetBundle, 
                        BuildTarget.StandaloneWindows);
}


mTest6.cs

void OnGUI()
{
    if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button")) Test();
}


void Test() 
{
    TextAsset bundleFile = Resources.Load("d4fe2a6e186849f4086398d2ceeb0510") as TextAsset;
    AssetBundle assetBundle = AssetBundle.CreateFromMemoryImmediate(bundleFile.bytes);
    Object[] assets = assetBundle.LoadAll();
    foreach (Object asset in assets)
    {
        Debug.Log("loaded asset " + asset.name + " of type " + asset.GetType());
    }
}

//loaded asset 1 of type UnityEngine.Texture2D
//loaded asset 1 of type UnityEngine.Sprite


string path = "Assets/NGUI/Examples/Models/Orc/FBX.FBX";
UnityEngine.Object[] assets = new UnityEngine.Object[] {
    AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)) };
string[] depends = AssetDatabase.GetDependencies(new string[] { path });
foreach (string str in depends)
{
    Debug.Log("depency: " + str);
} 
//depency: Assets/NGUI/Examples/Models/Orc/FBX@idle.FBX
//depency: Assets/NGUI/Examples/Models/Orc/FBX@idleBreaks.FBX
//depency: Assets/NGUI/Examples/Models/Orc/FBX.FBX


List<string> tmp = new List<string>();
int index = 0;
foreach (Object asset in assets){
    Debug.Log("loaded asset " + asset.name + " of type " + asset.GetType());
    index++;
    tmp.Add(index+"");
}
UnityEngine.Object mainAsset = assets[0];

string savePath =
    "f:/assetbundles/windows/"
        + "d4fe2a6e186849f4086398d2ceeb0510"
            + ResourceCommon.assetbundleFileSuffix;
-----1   
BuildPipeline.BuildAssetBundle(
    mainAsset, assets, savePath,
        BuildAssetBundleOptions.CollectDependencies
            | BuildAssetBundleOptions.CompleteAssets
                | BuildAssetBundleOptions.DeterministicAssetBundle,
                    BuildTarget.StandaloneWindows); 會收集依賴  比如 fbx@idle 等等

-----2   
BuildPipeline.BuildAssetBundleExplicitAssetNames(
        assets, tmp.ToArray(), savePath,
        BuildAssetBundleOptions.CollectDependencies
            | BuildAssetBundleOptions.CompleteAssets
                | BuildAssetBundleOptions.DeterministicAssetBundle,
                    BuildTarget.StandaloneWindows); ///不會收集依賴  比如 fbx@idle 等等



void Test() 
{
    TextAsset bundleFile = Resources.Load("d4fe2a6e186849f4086398d2ceeb0510") as TextAsset;
    AssetBundle assetBundle = AssetBundle.CreateFromMemoryImmediate(bundleFile.bytes);
    Object[] assets = assetBundle.LoadAll();
    foreach (Object asset in assets)
    {
        Debug.Log("loaded asset " + asset.name + " of type " + asset.GetType());
    }
}
-----1
//loaded asset FBX of type UnityEngine.GameObject
//loaded asset FBX of type UnityEngine.Transform
//loaded asset Orc of type UnityEngine.Mesh
//loaded asset FBX@idle of type UnityEngine.GameObject
//loaded asset FBX@idle of type UnityEngine.Transform
//loaded asset Orc of type UnityEngine.Mesh
//loaded asset idle of type UnityEngine.AnimationClip  .etc

------2
//loaded asset FBX of type UnityEngine.GameObject
//loaded asset FBX of type UnityEngine.Transform

從上述實驗看 BuildPipeline.BuildAssetBundle 和 BuildPipeline.BuildAssetBundleExplicitAssetNames 不同之處
就在於后者只打包你當前指定的資源,比如 fix 或者 texture2d 下面的 mesh 和 sprite 不會打包進去 ,
並且依賴比如 fbx@idle 也不會打包進去 , 不過這個還好,正好是我們當前的打包策略,依賴由我們自己掌握

這樣我們考慮 texture2d 的打包策略和 prefab 的打包策略不一樣了 或者 內存中動態生成sprite

參考

load-unity-43-sprites-with-assetbundles


參考

Unity亞洲研習營台北站-AssetBundle工作流程

Unite 2014 - New AssetBundle Build System in Unity 5

new-assetbundle-build-system-in-unity-5-0.293975

articledetail


免責聲明!

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



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