Quick Start

Importing the Package

  1. Open the Unity Package Manager (Window > Package Manager).

  2. Import the Observed package.

Adding the using Statement

In the scripts where you want to use the Observed classes, add the following using statement:

using Observed;

Creating an Observable

public Observed<int> scoreObserved;

Initialize the observable in the Start method:

void Start()
{
    scoreObserved = new Observed<int>();
}

Subscribe to the observable

void Start()
{
    scoreObserved = new Observed<int>();
    scoreObserved.Subscribe(UpdateScoreText);
}

private void UpdateScoreText(int newScore)
{
    scoreText.text = "Score: " + newScore;
}

Set the value of the observable:

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        scoreObserved.Value++;
    }
}

Now every time the player presses the space bar, the score will be incremented and the UpdateScoreText method will be called, updating the score text on the screen.

Last updated