Resources:Disadvantages of resources: 1. The use of resources is not convenient . 2 compared to the direct reference on the display inspector. Whether or not your resource is being invoked, when you publish it, Resources will all be packaged together and cannot be updated. resources method: Resources.load: Dynamically load special folders files in the Resources. Resources.unloadasset: Reclaims the specified cache. Resources.unloadunusedasset: Recycle no referenced cache Add extra things here, if you save things 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, but also extend to many aspects): First of all refer to System.IO and Unityeditor the former is directory needs, the latter is assetdatabase need string 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 Go assetdatAbase. 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 CRC UINT, Assetbund Leoptions: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 UINT CRC: 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//projectobject[] 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 |
Package scenario Buildpipeline.buildstreamedsceneassetbundle (levels, path, Target, buildoptions.uncompressedassetbundle))// Package resource Buildpipeline.buildassetbundle (NULL, assets, PATH, Buildassetbundleoptions.uncompressedassetbundle | 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.
Resources and Assetbundle (old and new) study (from Bull)