Unity engine variable invocation produces unnecessary memory allocations

Source: Internet
Author: User

https://unity3d.com/de/learn/tutorials/topics/performance-optimization/ optimizing-garbage-collection-unity-games?playlist=44069unity function calls

It's important to being aware that whenever we call code the We didn ' t write ourselves, whether that's in Unity itself or in A plugin, we could be generating garbage. Some Unity function calls create heap allocations, and so should is used with care to avoid generating unnecessary garbage .

There is the no list of functions that we should avoid. Every function can be useful in some situations and less useful in others. As ever, it's best-to-profile we game carefully, identify where garbage is being created and think carefully what to Handle it. In some cases, it is the wise to cache the results of the function; In other cases, it could be wise to call the function less frequently; In the other cases, the it may is best to refactor we code to use a different function. Have said that, let's look at a couple of common examples of Unity functions that cause heap allocations and consider Ho W best to handle them.

Every time we access a Unity function, a returns an array, a new array was created and passed to us as the return value. This behaviour isn ' t always obvious or expected, especially when the function was an accessor (for example, mesh.normal S).

In the following code, a new array was created for each iteration of the loop.

void Examplefunction () {for    (int i = 0; i < myMesh.normals.Length; i++)    {        Vector3 normal = Mymesh.normals[i ];    }}

It's easy-to-reduce allocations in cases like this:we can simply cache a reference to the array. When we do this, only one array is created and the amount of garbage created is reduced accordingly.

The following code demonstrates this. In this case, we call mesh.normals before the loop runs and caches the reference so, one array is created .

void Examplefunction () {    vector3[] meshnormals = mymesh.normals;    for (int i = 0; i < meshnormals.length; i++)    {        Vector3 normal = Meshnormals[i];}    }

Another unexpected cause of heap allocations can be found in the functions gameobject.name orgameobject.tag . Both of these is accessors that return new strings, which means that calling these functions would generate garbage . Caching The value may be useful, and the this case there are a related Unity function that we can use instead. To check a gameobject ' s tag against a value without generating garbage, we can use Gameobject.comparetag ().

In the following example code, garbage are created by the call to Gameobject.tag:

private string Playertag = "Player"; void Ontriggerenter (Collider other) {    bool Isplayer = Other.gameObject.tag = = Play Ertag;}

If we use gameobject.comparetag (), this function no longer generates any garbage:

private string Playertag = "Player"; void Ontriggerenter (Collider other) {    bool Isplayer = Other.gameObject.CompareTag (Playertag);}

gameobject.comparetag isn ' t unique; Many Unity function calls has alternative versions that cause no heap allocations. For example, we could use Input.gettouch () and Input.touchcount in place of input.touches, or C5>physics.spherecastnonalloc () in place of Physics.spherecastall ().

Unity engine variable invocation produces unnecessary memory allocations

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.