Unity3D tutorial 13 C # destroy shells,

Source: Internet
Author: User

Unity3D tutorial 13 C # destroy shells,

GameObject. renderer. enabled
// Controls whether an object is rendered or displayed on the screen. What actually exists is that the object still exists when it is invisible and the collision body of the object still exists.

GameObject. Destroy ()
// Removing an object or a component on an object means that the memory of the object is not immediately released, but the memory resources are released in your slot in the next scenario, it is Destroy in your scenario a. Generally, the memory resources of this object are actually released in the c scenario (this is my experience and I don't know whether to understand it or not)

GameObject. active
// Whether to disable the object in the scene. In your gameObject. active = false, you cannot find the object using find in the scene.
// If this object has a subobject, you must use SetActiveRecursively (false) to determine whether to stop the object in the scene (recursive)

 

Destroy ()

1 using UnityEngine; 2 using System. collections; 3 4 public class acc: MonoBehaviour {5 6 // Use this for initialization 7 public Transform Q; 8 int speed = 50; 9 void Start () {10 11} 12 13 // Update is called once per frame14 void Update () {15 float x = Input. getAxis ("Horizontal") * Time. deltaTime * speed; // move around 16 float z = Input. getAxis ("Vertical") * Time. deltaTime * speed; // 17 before and after movement // 18 transform for the main camera object movement. translate (x, 0, z); 19 20 if (Input. getKeyDown (KeyCode. mouse0) 21 {22 23 // Transform 24 Transform n = Instantiate (Q) as Transform; 25 // The Position of the fired bullet is 26 n of the object. position = transform. position; 27 28 Vector3 f = transform. transformDirection (Vector3.forward); 29 n. gameObject. rigidbody. addForce (f * 3000); 30 Destroy (n. gameObject, 5); 31 // Destroy (n. gameObject); 32 // Destroy (gameObject); 33} 34 35 36} 37}

Destroy objects in 5 seconds

Destroy (n. gameObject, 5 );

Destroy objects now

Destroy (n. gameObject );

 

If the file is bound to an object

Destroy (gameObject );

To destroy objects

 

 

Memory application and release of Unity Resources

1. Resource Type

GameObject, Transform, Mesh, Texture, Material, Shader, Script and various other Assets.

2. Resource Creation Method
  • Static reference: Add a public GameObject variable to the script, drag a prefab to the variable in the Inspector panel, and then Instantiate where the variable needs to be referenced;
  • Resource. Load, which must be placed in the Assets/Resources Directory;
  • AssetBundle. Load, and Instantiate after Load. 3. Resource destruction method
  • GameObject. Destroy (gameObject) to Destroy this object;
  • AssetBundle. Unload (false): releases the memory image of the AssetBundle file and does not destroy the Assets object created by Load;
  • AssetBundle. Unload (true): releases the memory image of the AssetBundle file and destroys all the loaded Assets memory images;
  • Resources. UnloadAsset (Object) to release the loaded Asset Object;
  • Resources. UnloadUnusedAssets to release all Asset objects that are not referenced. 4. in the experiment of the life cycle experiment, a simple scenario is created. An Empty GameObject is created in the scenario, and a script is mounted on it to create resources through the coroutine function in the Awake function, specific Coroutine functions are available below. The Prefab created in the experiment is a tank car. In the scenario, the memory size increases by about 3 MB, and an AssetBundle resource is created for AssetBundle. 1. Load a Prefab in Resources. Load mode, and then Instantiate GameObjectThe Code is as follows:
IEnumerator LoadResources () {// clear to avoid affecting the test result Resources. unloadUnusedAssets (); // wait for 5 seconds to see the effect. yield return new WaitForSeconds (5.0f); // use Resources. load a resource GameObject tank = Resources. load ("Role/Tank") as GameObject; yield return new WaitForSeconds (0.5f); // Instantiate a resource out of GameObject tankInst = GameObject. instantiate (tank, Vector3.zero, Quaternion. identity) as GameObject; yield return new WaitForSeconds (0.5f); // Destroy a resource GameObject. destroy (tankInst); yield return new WaitForSeconds (0.5f); // release useless resource tank = null; Resources. unloadUnusedAssets (); yield return new WaitForSeconds (0.5f );}

The execution result is as follows:

The following are the statistical results:

Data Description Memory Texture Mesh Material GameObjects Objects in Scene Total Objects
Initial 72.8 M 1271/8. 0 M 35/223. 0 K 25/10. 2 K 7 211 2187
Resources. Load 72.8 M 1271/8. 0 M 36/0. 8 M 25/10. 2 K 7 211 2280
Instantiate 75.3 M 1272/9. 3 M 36/0. 8 M 26/10. 7 K 52 303 2375
Destroy 74.7 M 1272/9. 3 M 36/0. 8 M 26/10. 7 K 7 211 2283
Resources. UnloadUnusedAssets 72.3 M 1271/8. 0 M 35/223. 0 K 25/10. 2 K 7 211 2187

The following conclusions are drawn:

  • Resouces. load a Prefab is a relatively lightweight operation relative to Instantiate a resource. In the above process, Resources. loading a Prefab consumes almost no memory, while Instantiate consumes 2 MB of resource space. Resources. Load increases the number of Mesh Objects and Total Objects, while Instantiate increases the number of GameObjects, Objects In Scene, and Total Objects;
  • After Destroy a GameObject, the memory is reduced, but it is relatively small. In this example, the memory is reduced by 0.6 M. The Material and Texture before and after Instantiate and Destroy are not restored, so as to continue using Instantiate later.

If Resources. UnloadUnusedAssets is not called, the result is as follows:

 

The statistical results are as follows:

Data Description Memory Texture Mesh Material GameObjects Objects in Scene Total Objects
Initial 58.9 M 1258/7. 5 M 34/219. 2 K 22/9. 0 K 7 117 2078
Resources. Load 60.0 M 1258/7. 5 M 35/0. 8 M 22/9. 0 K 7 117 2171
Instantiate 62.5 M 1259/8. 9 M 36/0. 8 M 23/9. 5 K 52 209 2256
Destroy 61.8 M 1259/8. 9 M 35/0. 8 M 23/9. 5 K 7 117 2174

The following conclusions are drawn:If you do not manually execute Resources. UnloadUnusedAssets, the redundant Mesh, Material, and Object will not be released.

2. Load a Prefab as AssetBundle. Load, and then Instantiate a GameObject

The Code is as follows:

IEnumerator LoadAssets (string path) {// clear to avoid affecting the test result Resources. unloadUnusedAssets (); // wait for 5 seconds to see the effect. yield return new WaitForSeconds (5.0f); // create a WWW class WWW bundle = new WWW (path); yield return bundle; yield return new WaitForSeconds (0.5f); // AssetBundle. load a resource Object obj = bundle. assetBundle. load ("tank"); yield return new WaitForSeconds (0.5f); // Instantiate a resource out of GameObject tankInst = Instantiate (obj) as GameObject; yield return new WaitForSeconds (0.5f ); // Destroy a resource GameObject. destroy (tankInst); yield return new WaitForSeconds (0.5f); // Unload Resources bundle. assetBundle. unload (false); yield return new WaitForSeconds (0.5f); // release useless Resources // obj = null; // Resources. unloadUnusedAssets (); yield return new WaitForSeconds (0.5f );}

The execution result is as follows: The statistical results are as follows:

Data Description Memory Texture Mesh Material GameObjects Objects in Scene Total Objects
Initial 59.9 M 1267/7. 8 M 35/223. 0 K 25/10. 2 K 7 127 2099
New WWW 62.0 M 1267/7. 8 M 35/223. 0 K 25/10. 2 K 7 127 2099
AssetBundle. Load 64.5 M 1268/9. 2 M 36/0. 8 M 26/10. 5 K 7 127 2196
Instantiate 65.6 M 1268/9. 2 M 36/0. 8 M 26/10. 7 K 52 219 2288
Destroy 63.9 M 1268/9. 2 M 36/0. 8 M 26/10. 7 K 7 127 2196
AssetBundle. Unload 63.7 M 1268/9. 2 M 36/0. 8 M 26/10. 7 K 7 127 2196
Resources. UnloadUnusedAssets 61.8 M 1267/7. 8 M 35/223. 0 K 25/10. 2 K 7 127 2099

The following conclusions are drawn:When a resource is loaded using WWW Load AssetBundle, the corresponding Mesh, Texture, and Material are automatically loaded. However, when loading resources using Resouces. Load, only the Mesh information is loaded. Therefore, the memory consumption of Instantiate a resource is small after loading by AssetBundle. In this example, AssetBundle. Load increases by MB of memory, while Instantiate increases by MB of memory. The memory increment of Instantiate is much smaller than that of Resources. Load.

3. Instantiate a resource through static binding

The Code is as follows:

IEnumerator InstResources () {Resources. unloadUnusedAssets (); yield return new WaitForSeconds (5.0f); GameObject inst = GameObject. instantiate (tank, Vector3.zero, Quaternion. identity) as GameObject; yield return new WaitForSeconds (1f); GameObject. destroy (inst); yield return new WaitForSeconds (1f); // release useless resource tank = null; Resources. unloadUnusedAssets (); yield return new WaitForSeconds (1f );}

The execution result is as follows:

The statistical results are as follows:

Data Description Memory Texture Mesh Material GameObjects Objects in Scene Total Objects
Initial 62.0 M 1268/7. 9 M 36/0. 8 M 25/10. 2 K 7 134 2202
Instantiate 64.4 M 1269/9. 2 M 36/0. 8 M 26/10. 7 K 8 137 2207
Destroy 64.0 M 1269/9. 2 M 36/0. 8 M 26/10. 7 K 7 134 2204
UnloadUnused Resources 62.3 M 1268/7. 9 M 35/226. 3 K 25/10. 2 K 7 134 2107

The conclusion is as follows:By static binding, various Resources are loaded in sequence and Resources. the Load method is the same. When a GameObject is created, the static bound GameObject in its Component will only Load the Mesh information. Texture and Material information will be loaded only after the GameObject is Instantiate.

Theory

The resource loading process can be divided into two stages. The first stage is to use Resources. Load or AssetBundle. Load to Load various Resources. The second stage is to use GameObject. Instantiate to clone a new GameObject. The Load resource types include GameObject, Transform, Mesh, Texture, Material, Shader, and Script. However, Resources. Load and AssetBundle. Load are different. When Resources. Load is used, before the first Instantiate, the corresponding Asset object has not been created until the first Instantiate. It aims to implement an OnDemand usage method, which is created only when the resource is actually used. When the AssetBundle. Load method is used, the resource file will be read directly to create these Assets, so the first Instantiate cost will be relatively small. The above differences can help us explain why the first bullet was choppy.

Then let's take a look at the Instantiate process. Instantiate is a process of combining Clone (copy) and reference of Assets. The Clone process requires applying for memory to store your own data, the reference process only needs to direct a simple pointer to a resource that has been loaded. For example, Transform is cloned. Texture and TerrainData are copied by reference, while Mesh, Material, PhysicalMaterial, and Script both exist by Clone and reference. Take a Script as an example. A Script can be divided into code segments and data segments. The code used by all the gameobjects that need to use the Script is the same, while the data is different. Therefore, the data segment needs to be cloned, the code segment must be copied by reference. Therefore, the Load operation loads some data sources to Clone or reference new objects.

Then, the process of destroying resources. When Destory is a GameObject or another instance, only the cloned Assets in the instance are released, and those referenced Assets are not released, because Destroy does not know whether other people are referencing these Assets. When no objects in the scenario reference these Assets, they become UnusedAssets. In this case, Resources. UnloadUnusedAssets can be released. AssetBundle. unload (false) does not work, because it only releases the memory image of the file and does not release resources; AssetBunde. unload (true) does not work either, because it is a violent release, and other objects may be referenced in the Assets. violent release may lead to program errors. In addition, when the system loads a new scenario, all memory objects are automatically destroyed, including Resources. load-loaded Assets, static bound Assets, AssetBundle. load loaded resources and Instantiate instantiated objects. However, the file memory image of AssetBundle. Load itself (used to create various assets) will not be automatically destroyed. This must be manually destroyed using AssetBundle. Unload (false. We recommend that you call AssetBunble. Unload (false) immediately after loading the resource to destroy the file memory image. This helps you understand the relationship between Asset and GameObject in memory.

Summary
  • In order not to get stuck during the first Instantiate operation, we recommend that you use AssetBundle. Load instead of Resources. Load to Load Resources;
  • Call AssetBunble. Unload (false) immediately after the resources are loaded to release the file memory image;
  • Unity itself does not provide a good memory application and release management mechanism, and Destroy a GameObject will immediately release the memory rather than internal cache. Therefore, the application processes frequently unused objects such as NPC, it is necessary to manage object pools, such as FX, to reduce the number of memory requests;
  • When to perform Resources. UnloadUnusedAssets is a question to be discussed.

 

520 520 novels 520 novels 520 5200 novels 5200 5200 novels 5200 novels Novels

Www.520books.com

Http://www.cnblogs.com/goodchenqing/

Http://blog.sina.com.cn/goodchenqing

Http://goodchenqing.bokee.com/

Http://blog.csdn.net/abc288abcd/article/category/2786567

 

Related Article

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.