Sitemap

Lua Global Variable Code Specification

9 min readMay 23, 2022

--

1) Lua global variable code specification

2) AssetBundle LockPersistentManager overhead

3) Unity’s built-in fonts are not considered redundant resources in the resource detection report

4) Adreno freezes on certain Android devices

5) Difference in performance between Mask and RectMask

Lua

Q: Using Lua as the script to assist development has become very popular but the global variables in Lua language are a headache because they can be used without declaration, and the compiler will not report errors for renaming and overwriting, which is a little inattentive. It will overwrite the global variable and cause a bug, and the reference to the GameObject by the global variable may cause leakage.

In the development process, will you formulate any code specifications for Lua global variables? For example: when can global variables be used? How to declare? How to avoid overwriting and other issues?

A: You can execute the luaGlobalCheck.lua file at an appropriate time after the Lua virtual machine is started. This file will set the metatable and metamethod of _G by overriding the _newindex and _index metamethods. It prompts an error when it is forbidden to create a new global variable and access a non-existing global variable. In this way, the problem of polluting the environment and overwriting by creating new global variables at will can be avoided.

The code of luaGlobalCheck.lua is as follows:

setmetatable(_G, { -- Controlling new global variables __newindex = function(_, k) error("attempt to add a new value to global,key: " .. k, 2) end, -- Controlling access to global variables __index = function(_, k) error("attempt to index a global value,key: "..k,2) end })

AssetBundle

Q: Observing the performance curve, it is found that the LockPersistentManager takes a lot of time when the AssetBundle is loaded in a certain frame. Can this be optimized?

A1: This means that there is a large amount of resources in the current frame or the previous frames that are loaded through LoadAsync. The essence is that the loaded resources are too large. Reasonable optimization of your own resources can reduce the overhead of Loading.LockPersistentManager. In addition, by replacing asynchronous loading with synchronous loading, LockPersistentManager will not appear, but its total loading time will not change, because the total loading has not changed.

For the loading optimization of main resources in Unity, please refer to the following links:

“Unity Loading Module Depth Analysis (Textures)”

“Unity Loading Module Depth Analysis (Grid)”

“Unity Loading Module Depth Analysis (Shader)”

“Animation Fragment of Deep Analysis of Unity Loading Module”

“Full Analysis of Mobile Game Loading Performance and Memory Management”

A2: Under the Unity 2019.4.1 version, bundle.LoadFromFileAsync is actually executed in the Integrate Asset of the main thread, and the LoadAssetAsync of the PreloadManager thread cannot be performed at the same time, and it must be locked, and LockPersistentmanager appears, which is locked until one party ends.

The essence is that the implementation is not perfect. You can use Spin Lock to lock it until the time specified by Application.backgroundLoadingPriority and then to the next frame. You don’t need to lock until one party releases it.

Press enter or click to view image in full size

In Unity 2019.4.11 and 2019.4.16, the problem of the main thread reading Bundle and waiting for locks was modified.

Press enter or click to view image in full size
Press enter or click to view image in full size

I tested it on iOS with Unity 2020.1.17, and the issue was basically solved:

Press enter or click to view image in full size

However, there are also some phenomena of asynchronously loading locks such as the main thread. It is estimated that the resource is too large and the collection dependency time is too long to trigger:

Press enter or click to view image in full size

AssetBundle

Q: In our project, the Prefabs of the two interfaces are both Bundles generated with Unity’s own Arial font and uploaded to the resource detection, but we did not see that the built-in fonts are redundant resources in the report.

A1: This Arial belongs to Unity’s built-in resources. When packaging the APK, it will be entered into the unity default resources. Therefore, the Arial font used in the AssetBundle is a reference relationship and will not be packaged into the corresponding AssetBundle. Therefore, see Less than redundancy is normal.

Use AssetStudio to open the unity default resources in the assets/bin/Data/ directory in the APK package, and you can see it, as shown below:

Press enter or click to view image in full size

A2: Arial font is not recommended, Chinese cannot be displayed on some low-end machines, mainly because the default font is different on different models.

Rendering

Q: Adreno will cause a screen frozen issue (GPU hangs) on certain Android devices.

Get UWA4d’s stories in your inbox

Join Medium for free to get updates from this writer.

The phenomenon is that the screen freezes and does not refresh, but the music and the sound effects of clicking on the UI can still be played. Using Unity Profiler to see the CPU has no exceptions and flashbacks. The error log caught when the screen is frozen can be viewed by clicking on the original question and answer. Please help me to see what might be the reason behind.

A1: Not sure if the stack is the same as you. The same situation has occurred on Xiaomi mobile phones with the Mono package before, but it did not recur after changing to IL2CPP.

A2: The above method is indeed effective for some models, but some MTK mobile phones still have similar problems. Our project also has the similar problem, such as a blurred or frozen screen. It feels like a problem with Unity batching, dynamic batching or GPU Instance. We try to reduce the number of GPU Instances in a single batch, which will be alleviated. This number is best adapted according to the GPU model of the mobile phone.

A3: It is implemented using the GPU Instance solution officially provided by Unity.

https://github.com/Unity-Technologies/Animation-Instancing

The default Shader of this scheme was used to render combat units, resulting in strange flashbacks and crashes. The default Shaders are as follows:

Press enter or click to view image in full size

The loadMatFromTexture function was called 9 times in this Shader. The implementation of loadMatFromTexture is as follows:

Press enter or click to view image in full size

It can be confirmed that the problem here is likely to be caused by too many sampling times, and then the current frame is used after modification, no Lerp operation is performed, and each vertex is only controlled by two bones, and then the test did not crash, and it is still being confirmed with the artist about the acceptance level of effect loss. The final code is probably like this:

Press enter or click to view image in full size

UGUI

Q: In Unity UI, what are the differences between Mask and RectMask in performance?

A1: RectMask2D: No need to rely on an Image component, its clipping area is the rect size of its RectTransform.

Property 1: All children under the RectMask2D node cannot be batched with external UI nodes and cannot be batched with multiple RectMask2Ds.

Property 2: When calculating Depth, all RectMask2Ds are treated as general UI nodes, but it does not have a CanvasRenderer component and cannot be regarded as the bottomUI of any UI controls.

Mask: The component needs to rely on an Image component, and the clipping area is the size of the Image.

Property 1: Mask will have two more Draw Calls at the beginning and end (the first = Mask node, the end = after the children under the Mask node have traversed), and if the multiple Masks meet the batch conditions, the two Draw Calls can be batched together ( The head of Mask1 and the head of Mask2 are combined; the tail of Mask1 and the tail of Mask2 are combined, and the head and tail cannot be combined.)

Property 2: When calculating Depth, when traversing to the head of a Mask, treat it as a UI node that cannot be batched, but pay attention to the bottomUI that can be used as its child UI node.

Property 3: UI nodes within a Mask and UI nodes outside of a Mask cannot be batched together, but UI nodes within multiple Masks can be batched together if they meet the batching conditions.

From the properties of Mask 3, it can be seen that it is not that the more masks the worse, because the masks can be batched together. Concluded as follow:

  • When an interface has only one Mask, then RectMask2D is better than Mask;
  • When there are two masks, then the two are similar;
  • When more than two Masks, then Mask is better than RectMask2D.

A2: It is possible to consider the conclusion upstairs from the batch of multiple Masks. Here is an addition: Mask writing Stencil will have additional OverDraw, Mask2D does not have OverDraw, but each Item must be compared with MaskRect, which has a certain CPU overhead (Mask is suitable for a lot of Items, and Mask2D is recommended for the case of few Items).

A3: From the perspective of UI batching, at the same level:

Mask is a special component with high priority and will be operated first. Mask will change itself and the Material of the UI, which are different.

RectMask2D is a clipper without special permissions, and its batching rules are the same as other ordinary components. RectMask2D will change the Clip Rect of the UI. Therefore, UI components not under the same RectMask2D cannot be batched together.

The reasons for whether the UI can be batched might be like:

Different Rect Clipping refers to switching from RectMask2D to normal components, or vice versa Different Clip Rect refers to switching between two RectMask2Ds.

Due to the content space, copyright, image limitation, for the full content with data and graphics, please feel free to read more here: https://blog.en.uwa4d.com/2022/05/19/lua-global-variable-code-specification/

Please feel free to read more related topics on the UWA official blog page at https://blog.en.uwa4d.com, over 9000 video games tested and optimized by UWA Game Optimization Toolkit, if you want to have a 15-day free try, you can visit the UWA official website and register an account at: https://en.uwa4d.com

You may also like:

GameLook: In the six months when the game version number was suspended and the pandemic repeated, how does UWA practice the game industrialization?

https://blog.en.uwa4d.com/2022/05/12/in-the-six-months-when-the-game-version-number-was-suspended-and-the-pandemic-repeated-how-does-uwa-practice-the-game-industrialization/

UWA DAY 2022 Conference Topic Submission — Practicing the Industrialization of Game R&D

https://blog.en.uwa4d.com/2022/03/14/uwa-day-2022-conference-topic-submission/

With 35 lectures in 2 days, focusing on industrialization problems, what substantial knowledge of game R&D did UWA DAY 2021 bring?https://blog.en.uwa4d.com/2021/12/08/with-35-lectures-in-2-days-focusing-on-industrialization-problems-what-substantial-knowledge-of-game-rd-did-uwa-day-2021-bring/

A Happy Knowledge Sharer:

To be honest, my current and previous video game studios and video game developer friends who develope video games all using UWA game performance optimization toolkit, It saves us lot of time, human resource and cost. I know UWA provides a variety of optimization products for developers who use Unity Engine and Unreal Engine, such as: Game Performance Monitor, Static Testing, Game Optimization Toolkit Online, etc., helping developers efficiently solve development problems, locate performance bottlenecks and find solutions.

By the way, if you want to test and optimize your game, you can tell them that i recommended you, they will free test your game and free provide you game optimization solutions. This is special ~~~

--

--

UWA4d
UWA4d

Written by UWA4d

UWA is an integrated platform dedicated to providing project research and development, technical consulting and talent training solutions for game, VR, AR apps