使用Assetbundle时可能遇到的坑

 No Comments

转自 http://www.unitymanual.com/blog-3571-132.html

1.Editor版本不能读取与自己版本不同的assetbundle
这个问题描述起来很简单:比如:老板原来让你用4.1打包(BuildAssetBundle)开发,开发完毕后,下半年,unity升级了, 于是老板要求与时俱进,让你用4.3开发维护,这时,问题就出现了:4.1下的editor打包的assetbundle在editor下不能被4.3读 取,会报错。
那么解决方法是啥呢?把所有资源重新在4.3下打包。很坑吧?但是,这问题仅仅只是在editor下会出现,webplayer不会出现。但你恐怕很少说调试程序不走editor吧?

2.assetbundle如果从WWW中被读取过一次,再读取会报错
当你把assetbundle资源用www下载下来后,往往都会使用“wwwResource.assetbundle”,但是注意,这个函数调用assetbundle的时候,只能调用一遍。意思是,当你要再次读取的时候,会报错。这时候,需要你写一套对于assetbundle的控制程序,保证第一次读取资源的时候是使用wwwResource.assetbundle,第二次再次利用,则要用已经读取出来的资源。

3.慎用BuildPipeline的PushAssetDependencies()和PopAssetDependencies()功能
在我们的项目中,打包时,用到了BuildPipeline.PushAssetDependencies()和 BuildPipeline.PopAssetDependencies()的功能,这是unity里比较好的一项功能,它可以把unity里的依赖关系 比较好的保存下来,并正常读取就可以还原。比如:一个模型用到了贴图1,贴图2,贴图3,这时,我们可以用这种依赖关系的方式,把模型单独打出来,同时把 贴图1贴图2贴图3也单独打包出来,这样的话,如果有别的模型也用到了贴图123,只需要下载一次,然后在读进工程的时候,贴图自动找到模型,并贴上去。 听起来很美,但是也有问题:严格根据依赖关系按顺序下载资源。否则,留给你的是一个个白模。我们被这个问题坑了好久。

4.Unity编译的webplayer工程,免费cache只有50MB
听起来是不是好惊讶?webplayer下的免费cache居然这么少?意味着,在webplayer下,你的LoadFromCacheOrDownload用处并不是非常大。因为你的资源需要一遍又一遍的下载,cache的作用大大缩小了。而如果你想扩展,需要联系他们的销售部,据说价格不菲。不过针对这个问题,现在有人已经利用游览器的缓存绕过了这个问题,思路比较巧妙,但是涉及到unity官方利益,我在这里就不介绍了。

在IOS平台加载Assetbundle的注意事项

  No Comments

1、Assetbundle打包时需要使用 BuildTarget.iPhone 参数。不同发布平台打包的文件是不通用的。

2、当上传已经打包好的文件到FTP服务器时,注意在上传软件菜单里选择传输类型为二进制格式,而不是默认的 ASCII 格式。

 

AssetBundles are files which you can export from Unity to contain assets of your choice. These files use a proprietary compressed format and can be loaded on demand by your application. This allows you to stream in content, such as models, textures, audio clips, or even entire scenes separately from the scene in which they will be used. AssetBundles have been designed to simplify downloading content to your application. AssetBundles can contain any kind of asset type recognized by Unity, as determined by the filename extension. If you want to include files with custom binary data, they should have the extension ".bytes". Unity will import these files as TextAssets.

When working with AssetBundles, here's the typical workflow:

During development, the developer prepares AssetBundles and uploads them to a server.

 
Assetbundle的杂七杂八                 
     Building and uploading asset bundles
 
  1. Building AssetBundles. Asset bundles are created in the editor from assets in your scene. The Asset Bundle building process is described in more detail in the section for Building AssetBundles
     
  2. Uploading AssetBundles to external storage. This step does not include the Unity Editor or any other Unity channels, but we include it for completeness. You can use an FTP client to upload your Asset Bundles to the server of your choice.

At runtime, on the user's machine, the application will load AssetBundles on demand and operate individual assets within each AssetBundle as needed.

Assetbundle的杂七杂八
                       Downloading AssetBundles and loading assets from them
 
  1. Downloading AssetBundles at runtime from your application. This is done from script within a Unity scene, and Asset Bundles are loaded from the server on demand. More on that in Downloading Asset Bundles.
     
  2. Loading objects from AssetBundles. Once the AssetBundle is downloaded, you might want to access its individual Assets from the Bundle. More on that in Loading Resources from AssetBundles

Please read this section of the documentation thoroughly to familiarize yourself with the workflow for using AssetBundles, discover the different features they provide and learn best practices that can save you time and effort during development.

See also:

Building AssetBundles

There are three class methods you can use to build AssetBundles:

An example of how to build an AssetBundle

Building asset bundles is done through editor scripting. There is basic example of this in the scripting documentation for BuildPipeline.BuildAssetBundle.

 

BuildPipeline.BuildAssetBundle
static function BuildAssetBundle(mainAsset: Object, assets: Object[], pathName: string): bool;
static function BuildAssetBundle(mainAsset: Object, assets: Object[], pathName: string, assetBundleOptions: BuildAssetBundleOptions): bool;
static function BuildAssetBundle(mainAsset: Object, assets: Object[], pathName: string, assetBundleOptions: BuildAssetBundleOptions, targetPlatform: BuildTarget): bool;
Description
Builds an asset bundle (Unity Pro only).

Creates a compressed unity3d file that contains a collection of assets. AssetBundles can contain any asset found in the project folder. This lets you stream resource data of any type, fully setup prefabs, textures, meshes, animations, any type of asset shown in the project window. mainAsset lets you specify a specific object that can be conveniently retrieved using AssetBundle.mainAsset. The compressed asset bundle file will be saved at pathName. options allows you to automatically include dependencies or always include complete assets instead of just the exact referenced objects. All paths are relative to the project folder. Like: "AssetsMyTextureshello.png"
Note that asset bundles built for standalone or webplayer targets cannot be loaded by applications built for mobile platforms and vice versa. Furthermore, bundles are not compatible between iOS and Android platforms.
See Also: AssetBundle class, WWW.assetBundle.
// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle

using UnityEngine;
using UnityEditor;

public class ExportAssetBundles {
    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void ExportResource () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0) {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
            Selection.objects = selection;
        }
    }
    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
    static void ExportResourceNoTrack () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0) {
            // Build the resource file from the active selection.
            BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
        }
    }
}
BuildAssetBundle

相关文章:

  • 2021-08-27
  • 2022-12-23
  • 2021-08-27
  • 2021-05-16
  • 2021-05-28
  • 2022-12-23
  • 2021-10-21
  • 2021-10-18
猜你喜欢
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案