Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IObservable<T> for Unity (The Observer Pattern)

Discussion in 'Scripting' started by hasCode, May 8, 2014.

  1. hasCode

    hasCode

    Joined:
    Feb 3, 2014
    Posts:
    8
    Code (csharp):
    1.  
    2.     public interface IObserver<TType> {
    3.         void OnCompleted();
    4.         void OnError(Exception exception);
    5.         void OnNext(TType value);
    6.     }
    7.  
    8.     public interface IObservable<TType> {
    9.         IDisposable Subscribe(IObserver<TType> observer);
    10.     }
    11.  
    I started using the Observer Pattern at work. (We recently started using .Net 4.5.) I wanted to use it in Unity. I goggled it and found nothing so I rolled my own.

    I am sure some day Unity will take advantage of the newer versions of .Net. Until then, simply drop these two interfaces into your project and you can start building your observers and observables right away.

    Enjoy.
     
    erebel55 and tswalk like this.
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Why don't implement Obserber pattern from scratch? I genuinely believe that it is possible with .NET 2.0.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Isn't that exactly what they just did?
     
  4. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Yeah, you're right. I'd misunderstood.
    Early morning)