☄️Tips & Code Examples

v1.0

Getting an OverlayIcon at Runtime

If you're customizing things, such as adding a UI Health Bar to your icons, you may need to access the OverlayIcon component at runtime associated with your object, but it won't be created until the game starts.

We can use a coroutine to grab this just a couple frames after Start(). You can also do additional logic and plumbing at that point as well.

// In this example, we are grabbing the OverlayIcon associated with the
// Screen & Edge Overlay. You can also grab the NavigationBarIcon from
// the TrackedTargetOverlay if you wish.

public class MyScript : MonoBehaviour
{
    private TrackedTargetOverlay _trackedTargetOverlay; 
     OverlayIcon _overlayIcon;
    
    private void Awake()
    {
        _trackedTargetOverlay = GetComponent<TrackedTargetOverlay>();
        StartCoroutine(RegisterOverlayIcon());
    }
    
    private IEnumerator RegisterOverlayIcon()
    {
        while (_overlayIcon == null)
        {
            _overlayIcon = _trackedTargetOverlay.ScreenIcon;
            if (_overlayIcon != null)
            {
                // Do additional actions here
                yield break;
            }
            yield return null;
        }
        yield break;
    }
}

Get Distance to the Target

Assuming you have access to the OverlayIcon as shown above, you can get the distance to the target. Additional members can be found on the Overlay Icon page.

var distanceToTarget = _overlayIcon.DistanceToTarget;

Check if Icon is on Screen, on Edge, or in Transition

The OverlayIcon handles both screen and edge overlay, and there's a period between the two where it will transition.

// 0 = Fully on screen
// 1 = Fully on edge
// Otherwise, it's in transition
var PercentToEdge => _overlayIcon.PercentToEdge;
var OnScreen => PercentToEdge == 0;
var OnEdge => PercentToEdge == 1;
var InTransition => PercentToEdge != 0 && PercentToEdge != 1;

Last updated