zoukankan      html  css  js  c++  java
  • UNITY引擎变量调用产生不必要内存分配

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

    Unity function calls

    It’s important to be aware that whenever we call code that 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 be used with care to avoid generating unnecessary garbage.

    There is 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 our game carefully, identify where garbage is being created and think carefully about how to handle it. In some cases, it may be wise to cache the results of the function; in other cases, it may be wise to call the function less frequently; in other cases, it may be best to refactor our code to use a different function. Having said that, let’s look at a couple of common examples of Unity functions that cause heap allocations and consider how best to handle them.

    Every time we access a Unity function that returns an array, a new array is created and passed to us as the return value. This behaviour isn’t always obvious or expected, especially when the function is an accessor (for example, Mesh.normals).

    In the following code, a new array is 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 cache the reference so that only 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 are accessors that return new strings, which means that calling these functions will generate garbage. Caching the value may be useful, but in this case there is 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 is created by the call to GameObject.tag:

    private string playerTag = "Player";
    
    void OnTriggerEnter(Collider other)
    {
        bool isPlayer = other.gameObject.tag == playerTag;
    }
    

    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 have alternative versions that cause no heap allocations. For example, we could use Input.GetTouch() and Input.touchCount in place of Input.touches, or Physics.SphereCastNonAlloc() in place of Physics.SphereCastAll().

  • 相关阅读:
    增强for循环赋值并且向list集合里添加元素,每个元素都一样
    《《《Spring 视频学习笔记
    xml中的<where><if>模糊查询
    《《《layui入门笔记
    《《《Vue element学习笔记
    《《《Spring Boot视频学习笔记
    intellij idea设置打开多个文件显示在多行tab上
    postMan安装完成,打开提示找不到,或者报错 可能原因
    Intellij IDEA debug模式下项目启动慢/无法启动的事件解决过程记录
    idea代码注释
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/7419461.html
Copyright © 2011-2022 走看看