Posts

Showing posts with the label programming

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 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...