Skip to content

Events and observables implemented as ScriptableObjects, SerializedReferences, and/or custom Serializable objects.

License

Notifications You must be signed in to change notification settings

ShortSleeveStudio/UnityObservables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UnityObservables

Events and observables implemented as ScriptableObjects, but also as plain serialized fields. The folloowing examples are both fields, but the could just as easily be ScriptableObjects. The "Raise" button fires the event, the other buttons ping the subscriber in the editor for debugging purposes,

Example Event (as a field): Event

Example Observable (as a field): Event

Example Code:

using Studio.ShortSleeve.UnityObservables;
using UnityEngine;

public class Tester : MonoBehaviour
{
    [SerializeField] private EventAssetVoid eventAssetVoid;
    [SerializeField] private ObservableAssetInt observableAssetInt;
    [SerializeField] private EventVoid eventVoid;
    [SerializeField] private ObservableInt observableInt; 

    private float _timeSinceLastRaise;

    void OnEnable()
    {
        observableAssetInt.Subscribe(OnObservableAssetFired);
        eventAssetVoid.Subscribe(OnEventAssetVoidFired);
        observableInt.Subscribe(OnObservableIntFired);
        eventVoid.Subscribe(OnEventVoidFired);
    }

    void OnDisable()
    {
        observableAssetInt.Unsubscribe(OnObservableAssetFired);
        eventAssetVoid.Unsubscribe(OnEventAssetVoidFired);
        observableInt.Unsubscribe(OnObservableIntFired);
        eventVoid.Unsubscribe(OnEventVoidFired);
    }

    void Update()
    {
        if (_timeSinceLastRaise > 1f)
        {
            observableAssetInt.Value++;
            observableInt.Value++;
            _timeSinceLastRaise = 0;
        }

        _timeSinceLastRaise += Time.deltaTime;
    }

    void OnEventAssetVoidFired()
    {
        Debug.Log("EventAssetVoid fired");
    }

    void OnObservableAssetFired(int value)
    {
        Debug.Log("ObservableAsset: " + value);
    }

    void OnEventVoidFired()
    {
        Debug.Log("EventVoid fired");
    }

    public void OnObservableIntFired(int value)
    {
        Debug.Log("Observable: " + value);
    }
}

About

Events and observables implemented as ScriptableObjects, SerializedReferences, and/or custom Serializable objects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages