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

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.

主站蜘蛛池模板: 井陉县| 南昌市| 家居| 静宁县| 驻马店市| 和静县| 广饶县| 宕昌县| 普兰店市| 喜德县| 乌鲁木齐县| 都兰县| 信丰县| 旺苍县| 榆林市| 迁安市| 鸡泽县| 巨鹿县| 黄梅县| 美姑县| 和平县| 平乐县| 梁山县| 长汀县| 五原县| 盖州市| 丰原市| 宁南县| 兴国县| 喀喇沁旗| 会昌县| 孟村| 云阳县| 哈巴河县| 赤水市| 乌海市| 重庆市| 甘孜县| 睢宁县| 城市| 新昌县|