Author Topic: UIEventListener subscribe/unsubscribe  (Read 1311 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
UIEventListener subscribe/unsubscribe
« on: January 29, 2015, 04:37:58 AM »
I was wondering if it is needed to unsubscribe onClick event for example that is subscribe to with UIEventListener?

here is what i am doing

  1. UIEventListener.Get(Button).onClick += go =>
  2.                     {
  3.                         Debug.Log(go.name);
  4.                     };
  5.  
  6. ...
  7.  
  8. UIEventListener.Get(Button).onClick -= go =>
  9.                     {
  10.                         Debug.Log(go.name);
  11.                     };
  12.  

Unsubscribe part gives me warning in Visual Studio...what is the proper way of doing this? Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener subscribe/unsubscribe
« Reply #1 on: January 29, 2015, 01:12:41 PM »
You can't do that with => I'm pretty sure, as this creates a new inlined function in that part of the code. They're effectively different functions.

The only way to -= is to have a proper non-anonymous function reference for both += and -=.