HUD Distance Lookup

In C#, whenever you set a floating point number to a string, it creates garbage in memory that must be cleaned up by the garbage collector.

This means that when a HUD displays a numeric value, there is the potential for it to impact your game's performance.

To get around this, the HUD Distance Lookup component has been created to pre-build a list of distance values as strings when the game starts, so that it doesn't have to be done during gameplay.

This is used for example by the HUD Target Box component for displaying the distance to a target efficiently.

Setting Up

To set it up, add a HUD Distance Lookup component anywhere in the scene. It is a singleton, which means there should only be one in the scene at any time.

This component has settings in the inspector such as the maximum distance to display and the distance resolution.

Looking Up A Distance Value

To look up a distance value and get it returned efficiently as a string, simply call the Lookup function from any script using the static Instance reference:

// Get a distance as string
string distanceStringValue = HUDDistanceLookup.Instance.Lookup(someDistanceValue);

Last updated