官术网_书友最值得收藏!

Avoid re-parenting Transforms at runtime

In earlier versions of Unity (version 5.3 and older), the references to Transform Components would be laid out in memory in a generally random order. This meant that iteration over multiple Transforms was fairly slow due to the likelihood of cache-misses. The upside was that re-parenting a GameObject to another one wouldn't really cause a significant performance hit since the Transforms operated a lot like a Heap data structure which tend to be relatively fast at insertion and deletion. This behaviour wasn't something we could control, and so we simply lived with it.

However, since Unity 5.4 and beyond, the Transform Component's memory layout has changed significantly. Since then, a Transform Component's parent-child relationships have operated more like dynamic arrays, whereby Unity attempts to store all Transforms that share the same parent sequentially in memory inside a pre-allocated memory buffer and are sorted by their depth in the Hierarchy window beneath the parent. This data structure allows for much, much faster iteration across the entire group, which is particularly beneficial to multiple subsystems like physics and animation. The downside of this change is that if we re-parent a GameObject to another one, the parent must fit the new child within its pre-allocated memory buffer as well as sort all of these Transforms based on the new depth. Also, if the parent has not pre-allocated enough space to fit the new child, then it must expand its buffer to be able to fit the new child, and all of it's children, in depth-first order. This could take some time to complete for deep and complex GameObject structures.

When we instantiate a new GameObject through GameObject.Instantiate(), one of its arguments is the Transform we wish to parent the GameObject to, which is null by default, and which would place the Transform at the root of the  Hierarchy window. All Transforms at the root of the Hierarchy window need to allocate a buffer to store its current children as well as those that might be added later (child Transforms do not need to do this). But, if we then re-parent the Transform to another one immediately after instantiation, then it discards the buffer we just allocated! To avoid this, we should provide the parent Transform argument into the GameObject.Instantiate() call, which skips this buffer allocation step.

Another way to reduce the costs of this process is to make root Transform pre-allocate a larger buffer before we need it so that we don't need to both expand and re-parent another GameObject into the buffer in the same frame. This can be accomplished by modifying a Transform Component's hierarchyCapacity property. If we can estimate the number of child Transforms the parent will contain, then we can save a lot of unnecessary memory allocations.

主站蜘蛛池模板: 石柱| 大方县| 寿宁县| 永嘉县| 石屏县| 榆林市| 高雄县| 阳春市| 礼泉县| 凉城县| 商城县| 香格里拉县| 施秉县| 刚察县| 土默特右旗| 田阳县| 荔浦县| 定西市| 永登县| 福海县| 萝北县| 报价| 商丘市| 沙雅县| 城口县| 宁蒗| 泸溪县| 芮城县| 宁明县| 横山县| 土默特左旗| 静宁县| 丰县| 铜山县| 元氏县| 肥东县| 蛟河市| 平利县| 舒城县| 梁河县| 水城县|