Differences in dynamic loading of prefab in Unity:
 
There are actually three ways to load prefab:
 
One is static reference. Create a public variable and pull the prefab In the Inspector. instantiate
 
Second, resource. Load, instantiate after load
 
The third is assetbundle. load, and instantiate after load.
 
There are differences in details between the three methods. In the first two methods, the referenced object texture is loaded at instantiate, while assetbundle. Load loads all assets of perfab, and only generates clone during instantiate. Therefore, in the first two methods, unless you load the referenced object in advance, the first instantiate operation will include the operation to load the referenced assets, resulting in the first lag load. Some people in the official forum say resources. load and static references are the results of repeated tests that pre-load all resources, static references and resources. load is also OnDemand and will be loaded only when used.
 
 
 
 
 
Differences between several assetbundle creation methods:
 
Createfromfile: This method does not load the entire assetbundle file on the hard disk to the memory. Instead, it is similar to creating a file operation handle and a buffer, and real-time load is required, therefore, this loading method is the most resource-saving. Basically, assetbundle itself does not occupy any memory and only needs the memory of the asset object. Unfortunately, it can only be used in PC/MAC standalone programs.
 
Createfrommemory and www. assetbundle: in either of the two ways, assetbundle files will be fully mirrored in the memory. Theoretically, the size of the file will be much memory, and additional memory will be used to generate asset objects during load.
 
 
 
When is unusedassets?
 
Let's look at an example:
 
Object OBJ = resources. Load ("myprefab ");
 
Gameobject instance = instantiate (OBJ) as gameobject;
 
.........
 
Destroy (instance );
 
Create and then destroy a prefab instance. At this time, myprefab is no longer referenced by the actual object, but if:
 
Resources. unloadunusedassets ();
 
The memory is not released because myprefab is also referenced by this variable obj.
 
At this time:
 
OBJ = NULL;
 
Resources. unloadunusedassets ();
 
In this way, the assets object can be truly released.
 
Therefore, unusedassets can be understood as unused (reference count is 0) not only if they are not referenced by actual objects, but also by variables in the lifecycle)
 
So: if you use a global variable to save your load assets, it is not explicitly set to null, before this variable becomes invalid, unloadunusedassets cannot release those assets. If your assets are not loaded from the disk, there are no other methods except unloadunusedassets or loading new scenarios.
 
 
 
 
 
In a complicated example, the code is ugly, and it is impossible to do so, just to deepen understanding.
 
 
 
Ienumerator onclick ()
{
Resources. unloadunusedassets (); // clear to avoid affecting the test results
Yield return New waitforseconds (3 );
Float wait = 0.5f;
// Read an assetbundle from www, which is a basic sphere of unity and a material with a large texture. It is a prefab.
Www aa = new WWW (@ "file: // sphereprefab. unity3d ");
Yield return AA;
Assetbundle asset = AA. assetbundle;
Yield return New waitforseconds (wait); // wait for 0.5s in each step to facilitate analysis results
Texture TT = asset. Load ("balltexture") as texture; // load the texture
Yield return New waitforseconds (wait );
Gameobject BA = asset. Load ("sphereprefab") as gameobject; // load prefab
Yield return New waitforseconds (wait );
Gameobject obj1 = instantiate (BA) as gameobject; // generate an instance
Yield return New waitforseconds (wait );
Destroy (obj1); // destroy the instance
Yield return New waitforseconds (wait );
Asset. Unload (false); // uninstall assetbundle
Yield return New waitforseconds (wait );
Resources. unloadunusedassets (); // uninstall useless Resources
Yield return New waitforseconds (wait );
BA = NULL; // unload useless resources after the prefab reference is set to null
Resources. unloadunusedassets ();
Yield return New waitforseconds (wait );
Tt = NULL; // unmount useless resources after setting texture reference to null
Resources. unloadunusedassets ();
}
 
 
 
This is the memory profile graph of the test result.
 
 
It is a classic symmetrical shape.
 
 
 
This is a change in memory and other data at various stages.
 
 
Note:
 
1. Initial status
 
2. After the assetbundle file is loaded, the file image is added to the memory, and the usage increases. The total object and assets increase by 1 (assetbundle is also an object)
 
3. After loading texture, the memory continues to rise because the addition of texture asset, total objects, and assets increases by 1.
 
4. After the prefab is loaded, the memory does not change significantly because the texture that occupies the most memory has been loaded. The materials increase is because the prefab material is added, and the total objects and assets are increased by 6 because perfab contains many components.
 
5. After prefab is instantiated, the texture memory, gameobjecttotal, and objects in scene increase because a visual object is instantiated.
 
6. After the instance is destroyed, the changes in the previous step are restored, which is easy to understand.
 
7. After the assetbundle file is uninstalled, the memory occupied by the assetbundle file image is released, and the corresponding assets and total objects count are also reduced by 1.
 
8 direct resources. unloadunusedassets, no changes because all assets references are not cleared
 
9 after the prefab reference variable is set to null, the entire prefab has no reference except texture, so it is destroyed by unloadunusedassets, assets and total objects count minus 6
 
10. Set texture's reference variable to null, and then be destroyed by unloadunusedassets. The memory is released. Assets and total objects count are reduced by 1, which is basically restored to the initial state.
 
 
 
We can also see that:
 
After texture is loaded, it is in the memory. The texture memory is displayed only when it is displayed.
 
All things are based on objects.
 
Load is asset, instantiate is gameobject and object in scene
 
The load asset must be unloaded. The new or instantiate object can be destroy.
 
Unity Memory loading and release