This is my current OnGUI() code I have:
private void OnGUI() {
GUIStyle style = new GUIStyle();
style.normal.textColor = Color.black;
style.alignment = TextAnchor.MiddleCenter;
Vector3 healthPosition = Camera.main.WorldToScreenPoint(this.transform.position);
Rect rectPosition = new Rect(healthPosition.x - 50f, (Screen.height - healthPosition.y) - 45f, 100f, 25f);
GUI.Label(rectPosition, new GUIContent(this.currentHealth.ToString() + "/" + this.maxHealth.ToString()), style);
}
What this code does, is it first sets the style of the GUI text. Next, it retrieves the current unit position in the world and converts it to screen coordinates that GUI.Label() can understand. Finally, the text containing current unit health and max health is shown on the screen, and follows wherever the unit is at relative to the camera.
This code is very simplistic and straightforward when it comes to depicting unit health in MonoBehaviour that is attached to the unit game object. It works perfectly in an RTS game I’m working on.
But, knowing that this is supposed to be deprecated, I can’t help but wonder how to convert this to the new UI system. The new UI system uses anchors and panels, which is kind of hard to picture how it works with a unit’s world position, while the player is looking at other portions of the map, or when the unit is off-screen.
Does anyone have hints on how to convert this old OnGUI() code to new UI system? Thanks in advance.