How to optimize the use of setactive in UI


1) How to optimize the use of setactive in UI
2) the problem of APK packets produced by ASTC compression and etc2 compression
3) Png picture format and TGA picture format
4) The crash of game running
5) On the applicable environment of AssetBundle loading mode

This is the 244th push of UWA technology knowledge sharing. Today, we continue to select a number of issues related to development and Optimization for you. It is suggested to read for 10 minutes. If you read carefully, you will get something.

UWA Q & a
UWA QQ group 2: 793972859 (the original group is full)


Q: In the daily development of UI, a lot of setactive (true) / (false) codes are used to show or hide some go. But setactive costs too much, so I don’t want to use setactive. Is there any other solution?

A1: in this case, the overhead of active / inactive is mainly in the following aspects:

1. The speed of shuttle call from C # layer to native layer is slower than that in C # layer.
2. The change of UI element leads to the change of canvas. The trigger functions canvas. Sendwillrendercanvases() and canvas. Buildbatch() cause high time consuming.
3. Changing the grid vertex array of UI elements will cause heap memory allocation and trigger GC, which will lead to time-consuming (however, moving the location of UI elements will not cause heap memory allocation).

Therefore, optimization can also be considered from the following points:
1. Set variables in the C # layer to identify whether the corresponding go is active or not. Avoid setactive (true) for active objects and setactive (false) for non active objects.

When setting active (true) to active, the “bottom layer” will judge, but when calling, the bottom layer is already called from the C # layer, resulting in high overhead. If we judge well in the C # layer, we avoid letting the bottom layer judge.

2. The UI elements that need to change frequently and those that don’t change frequently are placed in different canvass to reduce the time-consuming when changing UI elements.

3. By moving the coordinates of UI elements out of the scope of canvas to show and hide, the time-consuming of setactive and sendwillrendercanvases can be avoided.

4. After testing, the operation of enabled = false on component is less time-consuming than the operation of setactive (false) on go.

5. Display and hide by adding the canvas group component to set the transparency.

thank [email protected] The Q & a community provides answers

A2: Recently, we found this problem in the optimization, especially in the case of image and tmptext. I plan to optimize from the following aspects:

1. Directly modify the layer for hanging uicanvas.

2. If uicanvas is not linked, use canvasgroup to control alpha
There’s a problem with this place. Because our project has been running for a long time, it’s not realistic to let the students of the spell interface go to the node to be controlled and hang the canvas group. There are some concerns about the performance when the component is dynamically hung at run time. Therefore, we plan to change to the node where the active node is modified at runtime, and add a canvasgroup control under the corresponding prefab, so prefab will complete the canvasgroup control. After the official launch, if there are omissions that do not exist, directly use setactive.

3. There is a disadvantage in using the canvasgroup method. It only changes the alpha and still occupies the layout. Therefore, the parent interface is layout and canvasgroup cannot be used. The initial plan is to compare the time-consuming of setscale0 and setactive, both of which should cause redrawing.

Thank Chengying @ UWA Q & a community for providing answers


Q: In an empty project, I put several large pictures 2048 * 2048. When using etc2 4bits format, the single size is 2MB, and when using ASTC 6×6 compression, the single size is 1.8mb.

According to my understanding: the APK using ASTC compression format should be smaller, but the fact is contrary to my expectation: the APK using etc2 compression format is 21.1mb; The APK produced by ASTC compression is 25.7mb.

Why is the enclave compressed by etc2 smaller? What about the APK of ASTC?

A: The size of the occupied inclusion is different from that seen in the preview interface of the editor.

The size seen in the preview interface is the size of the resource in ASTC or etc2 format. After packaging, the resource will be further compressed (lz4 or LZMA). It can only show that the size of etc2 compressed into lz4 is smaller than that of ASTC compressed into lz4. The reason depends on the implementation of the specific compression algorithm.

AssetBundle can be used to verify that if no compression is selected when making AssetBundle package, the AssetBundle package in ASTC format is smaller than that in etc2 format. If lz4 or LZMA compression is selected, the AssetBundle package in etc2 format is smaller than that in ASTC format.

thank [email protected] Q & a community provides answers:


Q: Art said that a picture with transparent channel must be in TGA format, not in PNG format, lacking transparent channel. Why do some PNG formats have transparent channels in unity? How to make art in Photoshop out of a PNG format, but also to meet the effect? Can you elaborate on the principle of difference between the two figures in unity?

A1: in Photoshop, there is no transparent channel for PNG. Export can only export rgb3 channels. To modify the transparency of PNG pixels, you need to use a mask.

You and art students said, “this piece of information painted on the mask, black through white”, he understood.

How to optimize the use of setactive in UI

Thanks to Zhang Shoufeng @ UWA Q & a community for providing answers

A2: it’s easier to deal with TGA art, and you don’t need to care about the difference between PNG formats. You don’t need to deal with alpha in Photoshop. After all, in the engine, you have to compress according to different platforms. Regardless of the size of the project, more convenient process is the key.

Thanks to Zheng Xiao @ UWA Q & a community for providing answers


Q: After the game has been running for some time, some machines will crash as shown below. The specific log can be checked by poking the original Q & A. have you ever encountered similar problems?

JNI ERROR (app bug): global reference table overflow (max=51200)

I think there is a related content in unity 2018.3. The version we are using now is 2019.4.10, which should have been repaired. I hope you can guide me. Thank you!

How to optimize the use of setactive in UI

A1: is there too many JNI calls? I’ve tried before. A Tencent voice API has been calling JNI. After running for a certain time, it crashes. The log seems to be the same as you.

thank [email protected] The Q & a community provides answers

A2: it should be caused by the frequent new calls of androidjavaclass and androidjavaobject without calling dispose.

Thank you for your reply from @ UWA Q & a community

A3: refer to the following information:

2019.4.21f1 Release Notes

  • Android: Fixed Java local reference leaking when using AndroidJavaClass/Object. (1283209)

stay the fixes.

thank [email protected] The Q & a community provides answers


Q: What are the applicable environments of assetbundle.loadfrommemory and assetbundle.loadfromstream in the applicable environment of AssetBundle loading mode?

1. Use AssetBundle resources downloaded from unitywebrequest and do not save them locally after use;
2. AssetBundle resources with encryption requirements.

1. AssetBundle resources with encryption requirements (the memory value is theoretically smaller than AssetBundle. Loadfrommemory);
2. In Android, you need to copy out the streamingassets directory and create a stream.

Thanks to Zheng Xiao @ UWA Q & a community for providing answers

A2: when resources need to be encrypted, you can read AssetBundle into memory, decrypt it, and then call assetbundle.loadfrommemory to load it. The maximum amount of memory consumed by this method will be at least twice that of AssetBundle. For referencePrinciples and best practices of AssetBundle

Assetbundle.loadfromstream can be streamed. It does not need to read all assetbundles into memory and then decrypt and load them. Instead, it can be loaded by reading and decrypting a part of the buffer each time, which will not occupy a large amount of memory. If you use this interface, you need to customize an inherited FileStream class, and then XOR encrypt and decrypt the byte array in the read and write methods.

For specific usage, please refer to:

thank [email protected] The Q & a community provides answers

Cover image source: uifaderpro
A set of scripts that make it easy for the UI (4.6b + ugui) in the unity engine to fade in and out.

That’s all for today’s sharing. Of course, life has its limits, but knowledge has no limits. In the long development cycle, the problems you see may only be the tip of the iceberg. We have already prepared more technical topics on UWA Q & a website for you to explore and share. You are welcome to join us. Maybe your method can solve other people’s urgent needs; The “stone” of his mountain can also attack your “jade”.

Official technology
Official Q & a
Official technical QQ group: 793972859 (the original group is full)