admin 管理员组

文章数量: 887021


2024年2月20日发(作者:switch结构框图)

loaded from the server on demand. More on that in .

2. Loading objects from AssetBundles. Once the AssetBundle is downloaded, you might want to access its individual Assets from theBundle. More on that in

Please read this section of the documentation thoroughly to familiarize yourself with the workflow for using AssetBundles, discover thedifferent features they provide and learn best practices that can save you time and effort during also:Building AssetBundlesThere are three class methods you can use to build AssetBundles: allows you to build AssetBundles of any type of asset. is used when you want to include only scenes to be streamed and loaded as the data becomes available. is the same as ssetBundle but has an extra parameter to specify a custom string identifier (name) for example of how to build an AssetBundleBuilding asset bundles is done through editor scripting. There is basic example of this in the scripting documentation for .

BuildAssetBundle

For the sake of this example, copy and paste the script from the link above into a new C# script called ExportAssetBundles. This scriptshould be placed in a folder named Editor, so that it works inside the Unity in the Assets menu, you should see two new menu options.

1. Build AssetBundle From Selection - Track dependencies. This will build the current object into an asset bundle and include all of itsdependencies. For example if you have a prefab that consists of several hierarchical layers then it will recursively add all the childobjects and components to the asset bundle.

2. Build AssetBundle From Selection - No dependency tracking. This is the opposite of the previous method and will only include thesingle asset you have this example, you should create a new prefab. First create a new Cube by going to GameObject -> Create Other -> Cube, which willcreate a new cube in the Hierarchy View. Then drag the Cube from the Hierarchy View into the Project View, which will create a prefab ofthat should then right click the Cube prefab in the project window and select Build AssetBundle From Selection - Track dependencies. Atthis point you will be presented with a window to save the "bundled" asset. If you created a new folder called "AssetBundles" and savedthe cube as 3d, your project window will now look something like this.

At this point you can move the AssetBundle 3d elsewhere on your local storage, or upload it to a server of your choice.

An example of how to change the properties of the assets when building an AssetBundle

You can use to force reimporting the asset right before calling , and then use to set the required properties. The following example willshow you how to set different texture compressions when building the Asset Bundle.

C#// Builds an asset bundle from the selected objects in the project view,// and changes the texture format using an UnityEngine;using UnityEditor;public class ExportAssetBundles { // Store current texture format for the TextureProcessor. public static TextureImporterFormat textureFormat; [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")] static void ExportResourceRGB2 () { textureFormat = _RGB2; ExportResource();

}

[MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")] static void ExportResourceRGB4 () { textureFormat = _RGB4; ExportResource(); } static void ExportResource () { // Bring up save panel. string path = lePanel ("Save Resource", "", "New Resource", "unity3d"); if ( != 0) { // Build the resource file from the active selection. Object[] selection = tered(typeof(Object), sets); foreach (object asset in selection) { string assetPath = etPath(() asset); if (asset is Texture2D) { // Force reimport thru TextureProcessor. Asset(assetPath); } } ssetBundle(Object, selection, path, tDependencies | teAssets); s = selection; } }}

C#// Changes the texture format when building the Asset UnityEngine;using UnityEditor;public class TextureProcessor : AssetPostprocessor{

void OnPreprocessTexture() { TextureImporter importer = assetImporter as TextureImporter; eFormat = eFormat; }}You can also control how the asset is imported using the .In a test environment, you sometimes need to test a change that require AssetBundles to be rebuilt. In these cases, it is advisable to use theoption when you build the AssetBundles. This makes it faster to build and load the AssetBundles but they will also be bigger and thereforetake longer to download.

Building AssetBundles in a production enviromentWhen first using AssetBundles it may seem enough to manually build them as seen in the previous example. But as a project grows in size

and the number of assets increases doing this process by hand is not efficient. A better approach is to write a function that builds all of theAssetBundles for a project. You can, for example, use a text file that maps Asset files to AssetBundle ing thContains the path to the game data folder (Read Only).The value depends on which platform you are running on:Unity Editor: /AssetsMac player: /ContentsiPhone player: /<>/DataWin player: Web player: The absolute url to the player data file folder (without the actual data file name)Flash: The absolute url to the player data file folder (without the actual data file name)Note that the string returned on a PC will use a forward slash as a folder separator.

ingAssetsPathContains the path to the StreamingAssets folder (Read Only).If you have a "StreamingAssets" folder in the Assets folder of your project, it will be copied to your player builds and be present in the pathgiven by files placed in a folder called StreamingAssets in a Unity project will be copied verbatim to a particular folder on the target 's always best to use to get the location of the StreamingAssets folder, it will always point to the correct location on the platform where theapplication is assets in Unity are combined into the project when it is built. However, it is sometimes useful to place files into the normal filesystemon the target machine to make them accessible via a pathname. An example of this is the deployment of a movie file on iOS devices; theoriginal movie file must be available from a location in the filesystem to be played by the PlayMovie that on some platforms it is not possible to directly access the StreamingAssets folder because there is no file system access in theweb platforms, and because it is compressed into the .apk file on Android. On those platforms, a url will be returned, which can be usedusing the WWW class. // print the path to the streaming assets folder var filePath = e(, "MyFile"); var result = ""; if (ns("://")) { var www = new (filePath); yield www; result = ; } else result = lText(filePath);

You can retrieve the folder using the property. For reference, the location of this folder varies per platform:On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the following code: path = th + "/StreamingAssets";On iOS, you should use: path = th + "/Raw";...while on Android, you should use: path = "jar:file://" + th + "!/assets/";Note that on Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressedfiles). This means that if you do not use Unity's WWW class to retrieve the file then you will need to use additional software to see inside archive and obtain the file.


本文标签: 结构 框图 作者