Optimization call overhead uGUI in the SetParent

Overview of
    the current project is a similar kind of scratch building blocks of programming tools, editor favors the Unity of uGUI completed, but on large projects (more than 600 block, each block has about five or six GameObject), the original completion very slow when loading the project, profiler view and found that a large part of the time spent on RectTransform.SetParent calls these days took some time to optimize this part of the call.
Reason
     RectTransform.SetParent call will trigger a number of other transfers, for example Graphic.OnBeforeTranformParentChange, due to the relatively large project, scene of a large number of GameObjects, SetParent many calls lead to a reduction in load performance.
The original project as an example of the step of loading the data storage of all GameObjectsLoad recovery invoked between GameObjects, this time will be called SetParent resume paternity Since the initial sequence of data problems of structural design, you can not know when instantiating parent who is so the parent can not pass parameters when invoked to reduce expenses incurred Instantiate not want.
Disposal options
     One way to think of is not dependent on the parent-child relationship Transform, but manually calculate all coordinates, so we can not call SetParent in demand instantiated, there will be spending the UI. In order to adhere to the fundamental structure of the current code unchanged, even with the local coordinates of the center can still specify a local coordinate, introduces the concept of a logical coordinate system (LogicTransform). Hierarchical structure with the logical coordinates the use of the original unity of the hierarchical structure of Transform establish, coordinate with the main difference in the calculation of the overall hierarchy of our own, rather than unity do it. Together with our ui do not need to rotate, zoom only scratched the war shifted, theoretical calculation code is very simple. The following is a hierarchy of GameObjects, wherein the Transform logic level black arrow composition, composed of the red arrow unity hierarchy.
Each has a corresponding LogicTransform Transform, satisfies the following relationship between
LogicTransform.localScale == Transform.localScaleTransform logical hierarchy in local coordinates (non localPositon), equal LogicTransform.worldPosition. With this completed, project from the loading time the original 30s down to the 15s (still slow).
     Optimization ReapplyDrivenPropertiesProfiler also found, RectTransform.reapplyDrivenProperties also very time-consuming, looked uGUI the source found that the local spending comes from LayoutRebuilder at the disposal of the matter, called MarkForLayoutRebuild due. Because of our good planning block is manual, so this local spending can also be dispensed with. But things LayoutRebuilder disposal is private, but fortunately able to dispose of the matter after reflection off the exchange function. After the exchange of the following logic

     There will be no demand for automatic layout of game objects assigned to prevent spending on special layer. This step brings the optimization and upgrading is not very clear, parsimonious about a period of about 1.5s.
     Optimization SetActive call and show some elements hiding after SetActive to complete. Since each call is triggered OnEnable / OnDisable, in game objects relatively large number of conditions, spending is not small, so then Canvas using nested approach to disposal. After the changes, the loading speed has improved.
Knot on
a large number of calls RectTransform.SetParent have a negative impact on the performance of
preventing not want SetActive, use nested Canvas also can improve the performance of local
well-designed Save data structure should be able to handle expenses brought back SetParent

 

Released six original articles · won praise 43 · views 570 000 +

Guess you like

Origin blog.csdn.net/hany3000/article/details/94545341