Slow loading of cocos2dxuiwidgets

Source: Internet
Author: User
The ui Implementation Method in our game project is implemented by using cocostudio to edit the ui and export exportjson and big image. there is always a problem with this implementation: When a page uses a large number of ui templates, the loading speed will be very slow. for example, when you click a button to display a page consisting of 7 or 8 ui templates, it takes one

The ui Implementation Method in our game project is implemented by using cocostudio to edit the ui and export exportjson and big image. there is always a problem with this implementation: When a page uses a large number of ui templates, the loading speed will be very slow. for example, if you click a button to display a page consisting of 7 or 8 ui templates, it takes one

The ui Implementation Method in our game project is implemented by using cocostudio to edit the ui and export exportjson and big image.

There is always a problem with this implementation: When a page uses a large number of ui templates, the loading speed will be very slow. for example, it takes one or two seconds to click a button to display a page consisting of 7 or 8 ui templates.

The root cause of the problem is that the function widgetFromJsonFile () is very slow.

At first, I suspected it was an io problem. That is to say, the function called CCFileUtils: sharedFileUtils ()-> getFileData () is slow, however, it was found that this function took a very small amount of time,

The performance is affected by the widgetFromJsonDictionary (widgetTree) called in this function. The function is very slow. This function parses various ui controls based on json strings.


To solve the choppy problem, I tried the following methods:

1. Use the same ui template to alternate positions to achieve infinite loops.

For example, if we have a page, we need to display dozens of list views composed of the same ui templates. at this time, you can calculate that the current page can display up to n items. On this basis, + 2 is called, that is, a total of n + 2 UIHelper: instance ()-> createWidgetFromJsonFile () functions are called.

With this n + 2 control, when moving down, the top control is moved down, and the current control is moved up to implement scrolling of dozens of ui templates.

Although this method saves a lot of time, even if you only read n + 2 ui templates, it still takes 1 ~ 2 seconds, unsatisfactory results


2. the ui control is not parsed from the json file, but copied from the memory.

The cocos2dx version of our project is relatively old, And the UIWidget does not have the clone () function.

At first, I called each sub-function in the UIWidget * CCSGUIReader: widgetFromJsonDictionary (const rapidjson: Value & data) function.

SetPropsForButtonFromJsonDictionary () setPropsForCheckBoxFromJsonDictionary () and other functions are used to write attribute assignment functions based on these functions. However, it is hard to write and the replication effect is poor. Many different UIWidget classes require different parameter settings, which makes it difficult to ensure correctness.

Later, we downloaded cocos2dx 2.2.5, And the UIWidget and its sub-classes all implemented the clone () function. I transplanted These clone () functions to our project.

However, the results are quite tragic: the clone () with UIWidget is actually slower than reading the json file every time! The experiment in version 2.2.5 also proves that the clone () function of the UIWidget class is inefficient.

Therefore, copying data from memory does not work.


3. Add the loading interface to the game and cache enough uiwidgets before entering the game.

Therefore, we wrote a singleton UIPool class. At the beginning of the game, we loaded enough required UIWidget controls and modified UIHelper: instance ()-> createWidgetFromJsonFile (), when there is an idle control in UIPool, use it directly. Otherwise, call the read from jsonfile function.

At first, I thought that the UIPool implementation would make it troublesome to control the memory of the cached UIWidget. However, after writing the code, I found that using CCDictionary to store n CCArray files, the implementation of each CCArray storage control instance is very simple. Both CCDictionary and CCArray can use the cocos2dx memory management mechanism to implement it conveniently.


In this way, the combination of method 3 and method 1 can eliminate serious freezing caused by UIWidget reading in most games.

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.