Resources and Assetbundle (old and new) learning

Source: Internet
Author: User

Resources:Disadvantages of resources: 1. The use of resources is inconvenient compared to the direct reference shown on Inspector.2. Regardless of whether the resources on your resource are invoked, when you publish, resources will all be packaged together and cannot be updated.Resources in: resources.load: Dynamic loading of files in special folders Resources. Resources.unloadasset: Reclaims the specified cache. Resources.unloadunusedasset: Recycle no referenced cache add extra things here, if you save something to the Resources folder, and the resources folder does not exist when the save will be error, so you want to added prevention mechanism (not only here, can also be extended to many aspects): first refer to System.IO and UnityeditorThe former is directory need, the latter is assetdatabase needString FilePath = Application.datapath + "/resources"; if (! Directory.Exists (FilePath))//Detect if the Resources folder exists{Assetdatabase.createfolder ("Assets", "Resources");//Create Resources folder}Gameobject go = casual;Assetdatabase.createasset (Go, "assets/resources/go.prefab");//create file GoAssetdatabase.refresh (); //Refresh a bit Assetbundle:

Assetbundle can export the files you need (music, textures, models, prefab, scenes) in a special file format (. Unity) and import these files at the appropriate time.


In some large-scale games, a scene inside the content is very much, if all of a sudden full load may have to wait a long time, the player and so on reading the bar read all want to sleep. We can read the contents of the vicinity and then slowly read the other content. This allows for efficient use of memory resources. Dynamically loading resources can be assetbundle,resources.load (). Assetbundle was originally designed to simplify game updates. Assetbundle can package any file that unity can recognize, as determined by the file format. If you want to package any custom binaries, you'll change the suffix to ". Bytes". Unity will make them into textassets. There are ways to create a resource bundle: unity5:At the bottom of the inspector, the function Buildpipeline.buildassetbundles () is called. He will automatically package according to dependencies. But there is a very deep pit: he will not give you automatic judgment of which resources are common, That is, you have 2 files using the same texture, but you texture not set Assetbundle He will repeat the print 1 times. The Momo method under this packaging method is also unusable, because Assetbundle.createfromfile cannot load a compressed assetbundle.Specific usage: [MenuItem ("Assets/build assetbundles")] static void Buildallassetbundles () {Buildpipeline.buildassetbundles (" Assetbundles ");//buildpipeline.buildassetbundles ("Assets/assetbundles");//This method other overloaded parameters and the old version is not bad}Create the Assetbundles folder in the project folder first. Note that it is the project folder instead of the assets, which is the official code, for convenience I would prefer to change the look of a comment. In Assetbundles, there will be 2 file formats, 1. There is no suffix, 2. Is the suffix is. manifest.There is no suffix to represent the dependencies of the project, with the suffix of the TXT open can see its resource dependencies and CRC. Old version: (the way to be aware 1,3 files packaged for different platforms are not generic)1.Buildpipeline.buildassetbundle: You can build any type of resource bundlePublic static function buildassetbundle (mainasset:object, assets:object[), pathname:string, out C5>CRC uint, assetbundleoptions:buildassetbundleoptions, targetplatform:buildtarget): bool; take the longest one directly to interpret the parameters:Mainasset: A single file that is packaged. Assets: Packaged file group.PathName: Store AddressOut UINTCRC: Outputs a parameter to use as a detection update, which can be used when using the WWW.LoadFromCacheOrDownLoad function. Assetbundleoptions: Special option, you can enter 0 (no special option). TargetPlatform: Select the platform to use for the package[MenuItem ("Test/create assetbundles")]static void Createassetbundle ()    {//Select a way to save filesstring path = Editorutility.savefilepanel ("Save Resource", "", "New Resource", "Assetbundle"); if (path. Length! = 0)        { selected Files in the//project object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);//Here the 1th and 2nd parameters seem to repeat, and the 1th parameter is null completely unaffected. The official code is also not able to trust ...Buildpipeline.buildassetbundle (selection.activeobject, Selection, path);selection.objects = Selection;        }    }2.Buildpipeline.buildstreamedsceneassetbundle: Package only the scene. 3.Buildpipeline.buildassetbundleexplicitassetnames : andBuildpipeline.buildassetbundleAlmost, just a few more parameters to customize each object's name.way 2,3 usage and way 1 almost. To read a resource bundle:The best example is the official example, very detailed.If I put the packing file in the resources, the file is called A.unity3d. void Start () {//on different platforms, the path is different, this requires attentionPublic static readonly string path =#if unity_android"jar:file://" + Application.datapath + "!/assets/";#elif Unity_iphoneApplication.datapath + "/raw/";#elif Unity_standalone_win | | Unity_editor"file://" + Application.datapath + "/streamingassets/";#elsestring. Empty; #endifstartcoroutine (Loadassetbundle (path + "A.assetbundle"));} IEnumerator Loadassetbundle (string path) { find assetbundle based on parameter 1 (server or local) and parameter 2 (version number). If it is not found locally, it will be downloaded from the server (that is, the update version) www www = WWW.LoadFromCacheOrDownload (path, 1); //Wait for downloadyield return www; //Read AssetbundleAssetbundle bundle = Www.assetBundle;//LoadObject obj1 = bundle. Loadasset ("Gameobject"); //Release the bundle's cachebundle. Unload (false); //Stop downloading and release the cachewww.Dispose ();    } www is a simple class that accesses Web pages.The download is started in the background by connecting the WWW (URL), and a new WWW object is returned. You can check the Isdone property to see if the download is complete, or yield automatically waits for the object to be downloaded until it is downloaded (without affecting the rest of the game).On Momo's blog, it is recommended not to useWWW.LoadFromCacheOrDownload. Because there's a better way. Perfect forwarding Momo original. link in the last

Because it is an async method, it also consumes memory.

It is strongly recommended to use Assetbundle.creatfromfile, which is a synchronous method. Now iOS and Android are supported, strongly recommended.

You need to choose not to compress when packaging.

1234 //Packaging Scenariosbuildpipeline. Buildstreamedsceneassetbundle(levels, path, target, Buildoptions. Uncompressedassetbundle)) //Packaging Resourcesbuildpipeline. Buildassetbundlenull assets, path, buildassetbundleoptions uncompressedassetbundle | Span class= "crayon-v" >buildassetbundleoptions. Collectdependencies, target

Because it does not compress, we need to compress the resources ourselves, we can use LZMA and gzip to compress.

1. Packing out the assetbundle we use LZMA compression, upload to the server.

2.IOS or Android download these Assetbundle

3. Unzip the Assetbundle and save it in the Application.persistentdatapath directory.

4. Read Assetbundle later via Assetbundle.creatfromfile.

about the effects of these methods on memory and how to clear the cache: Http://www.manew.com/home.php?mod=spacecp&ac=blog&blogid=3285&op=edit

Reference Blog Momo:http://www.xuanyusong.com/archives/2405/

(transfer) resources and Assetbundle (old and new) learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.