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

原文地址:https://www.cnblogs.com/chongxin/p/5562469.html