Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
In the process of learning unity, you will find that after packaging the release program, Unity will automatically package the resources that the scene needs to refer to in the installation package, without going in. The file structure in the asset that we see in the editor is only working in the editor environment and Unity will reorganize the database in the game. This is a requirement that we will encounter a need to dynamically load our own files, and want to maintain the location where the file is stored and loaded, and is a variety of custom files.
For example, if you dress up, you need to load a model dynamically from the disk, normally if the model is not referenced by the scene, it will not be punched into the installation package at all. Another example is that I want to store a file of my own defined configuration, I want to put it in a location on the disk, dynamic read, these requirements will be dynamically loaded resources.
1. First summarize what kinds of resource types unity has
Types of Unity Resources:
-a) unity built-in common asset, fbx\jpg ...
-b) textasset:txt, binary, etc., corresponding to its Textasset class, can be read directly into the text or binary byte
-c) Scriptable object It is a serialized object instance, such as saving an object instance as a scriptable object, which is read into the memory directly into that instance.
-d) Asset bundle It is a resource compression package that contains a bunch of resources
Usually our custom file types can be stored by textasset or scriptable object, except that it is a byte or text stream, which corresponds to a defined type in the program, and Textasset is also commonly used for resource encryption.
2. Several ways to dynamically load resources:
-Through the resources module, call its load function: You can directly load and return a type of object, if you want to put this resource under the resource named folder, Unity does not have a scene reference, will be all into the installation package.
-In the form of bundles: the resource will be asset bundle on the server or local disk, then use the WWW module get down, and then load an object from the bundle.
-Through Assetdatabase.loadasset: This method is only valid in the editor scope, the game runs without this function, it is usually used in the development of debugging
The difference: the way the resources are all going to be in the installation package, which is bad for the game's sub-release (micro-end) and version upgrade (patch), so unity recommended way is not to use it, all in the form of bundles, the resources to achieve a few small bundles, Which is the load which, this can also sub-release and patch, but in the development process, it is not possible to update a resource once the bundle, so in the editor environment can use Assetdatabase to simulate, which usually requires us to encapsulate a dynamic Resource's loader modules are implemented differently in different environments.
3. Storage of dynamic resources
Sometimes I need to store some of my own files on disk, for example I want to put a few bundles in the initial installation, unity has a concept of streaming asset to provide access to the storage interface. We need to set up a Streamingassets name folder in the editor, put the dynamic files we put on the client disk under this folder, so that after installation, these files will be placed in the user's disk at the specified location, This position can be obtained by Application.streamingassetspath.
The difference between dynamically loading resources.load () and asset bundles in unity