Search Unity

  1. We want you to join us at GDC this year! We've added new sessions to Unity Central, space is limited so sign up now!
    Dismiss Notice
  2. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  6. Be the first to take a peek at upcoming 2D Animation Preview. Drop into the forum and check it out!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[3USD] Key Value Observing - lightweight implementation of KVO for C#

Discussion in 'Assets and Asset Store' started by arklay_corp, Jun 2, 2015.

  1. arklay_corp


    Apr 23, 2014

    Key Value Observing v.1.0

    Link to the Asset Store: [HERE]

    This asset provides a lightweight approach to the Key Value Observing pattern in C#.

    Le’s start with the basic: what does Key Value Observing means?

    Key Value Observing (or KVO) is a design pattern, Objective-C implements it at language level, you can read an “official” explanation in apple’s website

    But let me try to give a more practical approach:

    In object oriented development one of the most used pattern (even used without recognizing is a as pattern, most of the time is just the natural approach) is the Model-View-Controller (or MVC):

    It means that what the user (or player) actually sees (the View) is not the actual game model (the mathematical Model being executed), to lower design coupling and improve cohesion both are separated, the model fills the values on the View while the View communicates with the model through a Controller.

    Yeah, I know I said I was gonna be practical: let’s say we have a 2D platform game, our hero has a number of lives that will change over time, he will lose and earn lives:

    In C# the MVC would be implemented in this case like this:

    Meaning that the model will provide the number of lives and the controller will notify the view when it changes.

    Now, what KVO proposes is, instead accessing directly to the model, the view will subscribe as an observer to a controller that will send updates when value for the given key changes:

    The main advantage will be that we are decoupling completely the model and the view.

    The asset includes 3 examples with different levels of complexity:

    · [LOW] A model of a lives container and a health bar (very similar to the one proposed here)

    · [MED] A model for a leader board, where the key observed is a list of values

    · [HIGH] A model for a lobby of a multiplayer game, where the model observes a list, and the items of that list

    The asset includes source code. I will try to help here whoever needs help with it and/or the KVO pattern.

    I use this asset myself and I plan to keep it update as I keep encountering different challenges in real life implementation
    Last edited: Dec 3, 2015
    unicoea and boysenberry like this.