Posts

SortedAction - Guaranteeing order of execution in pure C# delegate

Image
There is something comforting about looking at a UnityEvent when serialized in Unity Editor's Inspector. It executes the actions assigned to it from top to bottom, in that order. No runtime accident can happen, this order of execution is pretty much guaranteed. Order of execution can make or break the logic of any program - did you refresh the money label before or after you subtract the amount of money paid for a restaurant meal? Order of execution is such an important concern that rearranging the actions on UnityEvent has been a recurring question asked many times on the Unity official forum, and a feature implemented by just as many packages . Order of execution is serious business. How then do we control order of execution without UnityEvent ? Surely you will encounter scenarios even within a Unity project where you have only access to pure C# delegates, such as a static event. In a pure C# delegate, actions are invoked in the ordered they are registered. This presents all...

A Laptop GPU thermal lesson learned the hard way: Never rely on thermal throttling

Alternative title: The importance of undervolting for lasting a laptop GPU There is a misconception out there about GPU thermal throttling. The fact that "thermal throttling" even exists gives off the impression that the hardware itself has been adequately tuned to protect itself from thermal damage. Nothing can be further from the truth. And this was how I found out the hard way. I bought a Clevo P750TM1-G under Sager brand in 2017. It came with the i7-8700K and the mobile GTX 1070. A year after purchase, when the factory thermal paste has all dried out, I installed The Witcher 3 and decided to see how far I can push this card. TW3 as you may already knew is a GPU-bound game. This was when the first signs of trouble began to appear. At this point, I trusted the GPU to automatically clock down when it hits a safe thermal limit and sustain at that clocked down frequency. It didn't go like that. I turned on supersample resolution for The Witcher 3 and saw that ...

CSS-like layout for Unity UI - Lessions from web development workflow

Image
Unity UI really is a pain, especially for mobile - the environment which the engine is really used widely in. You put everything into place for one screen aspect ratio, and then you switch to another screen aspect ratio and suddenly, the whole thing breaks. Left: Standard 9:16 layout. Middle: Same scene, in iPad Pro 11 (highest aspect ratio scenario). Right: Same scene, in Xperia 1 III (lowest aspect ratio scenario). This is a longstanding issue with Unity UI that has yet to be comprehensively covered by any guide I can find. Unity developers are just sort of... expected to figure this out. A major part of this issue is that Unity Editor's interface just naturally guides you toward using pixel count to layout UI elements, when this is absolutely the wrong way to handle multiple resolutions. Pixel numbers are absolute in Unity UI. Even with a Canvas Scaler set to a fixed reference resolution, it still cannot account for multip...

A better way to get letterboxing in Unity

Image
Alternative title: A critique of the existing Unity letterboxing solutions and how to improve them In a video titled " Handling different aspect ratios in your Unity 2D games ", Rabidgremlin pointed out a real issue with aspect ratio scaling in Unity: Different section of your game world is cut off from view when you switch aspect ratio due to Unity trying to preserve the vertical FOV of a camera at every moment. However, the solutions he chose have... problems. You see, the solution that Rabidgremlin chose for this issue was an asset on the Unity Asset Store called " Auto Letterbox ". The way this asset implements letterboxing is:  - At runtime, in a Start method, find all cameras in a scene using a FindObjectsOfType<Camera> call, save them to a list, modify each of their viewport rect to limit what they can display to fit the target aspect ratio. - Instantiate a new camera, set its background color to the color you would choose for your letterbox, set its...