Search Unity

only reacting to events when their outcome is visible

Discussion in 'Scripting' started by holyjewsus, May 24, 2015.

  1. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    I have a event driven system implementing INotifyPropertyChanged, sometimes this event gets fired many times per frame, and all the handlers update many times per frame, this is all just user interface stuff that doesn't need to be updated until the last event fires for that frame.

    I'm looking for a pattern or an idea for only handling events that will actually produce visible results, basically I only need to actually handle the last event each frame per object.

    any ideas?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    LateUpdate might work. Simply keep track of the most recent event called, then execute in LateUpdate.
     
  3. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    There's a few ways I handle these situations, it depends when you need them to occur. LateUpdate as suggested is pretty good, but you need to store which properties updated in some kind of container, and always check if there's already an entry.

    Otherwise you can just compare the current and last value, and if it hasn't changed don't update.
    You can check that you only update once per Time.frameCount. E.g. if Time.frameCount > lastUpdatedFrame.