0
\$\begingroup\$

I have created a basic game that runs in a view. It consists of a thread which repeatedly calls the update() and draw(Canvas canvas) method. Everything works fine so far.

Next, I want to implement a UI that consists of a textView showing the score, and other stats of the game. This is put above the view of the main game. Should this UI have its own thread that repeatedly checks to see if the score has changed, and then update that change in the UI? At first I just made a class that allows the main game thread to update elements in the UI via something like scoreView.setText(currentScore). But this leads to some instability, and that object to talk to the UI has to be passed throughout the methods in the main game UI which isn't the cleanest approach.

What is the standard approach for implementing the UI in a clean way which is easy to maintain.

visual guide: (the line indicates that the two communicate with another) enter image description here

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$
using UnityEngine;

using UnityEngine.UI;

public class HitPoints : MonoBehaviour {

public Text HitPointsText;

    float hitPointsProp;
    public float HitPointsProp {
        get {
            return hitPointsProp;
        }
        set {
            hitPointsProp = value;
            HitPointsText.text = hitPointsProp.ToString();
        }
    }
}

Drag your Text game object to public field; When you will use HitPoints=10; the text will be automatically updated to 10.

\$\endgroup\$
0
\$\begingroup\$

I would suggest that you make use of DrawText. You can draw this using the existing thread that is drawing your current interface. You can change your variables anywhere in the code based on different conditions and you will know that what is being displayed is an up-to-date value as the thread will be checking/ drawing it so frequently.

\$\endgroup\$

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .