Author Topic: EventDelegate not working on () => {} if NGUI version > 3.5.5  (Read 6037 times)

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
  1. public class ClassA : BaseClass {
  2.  
  3.  
  4. }
  5.  
  6. public abstract class BaseClass : MonoBehaviour {
  7.        
  8.         void Awake()
  9.         {
  10.                 var tweenPosition = GetComponent<TweenPosition>();
  11.  
  12.         EventDelegate.Set(tweenPosition.onFinished, () => TestMethod() );
  13.                
  14.         }
  15.        
  16.         protected void TestMethod()
  17.         {
  18.                 Debug.Log( "BaseClass TestMethod" );   
  19.         }
  20. }
  21.  
  22.  

ClassA.cs is attached to GameObject with TweenPosition Component.

If NGUI = 3.5.5, it works without problem
If NGUI > 3.5.5, it throws the following exception
Could not find method '<Awake>m__0' on ClassA
UnityEngine.Debug:LogError(Object, Object)
EventDelegate:Cache() (at Assets/NGUI/Scripts/Internal/EventDelegate.cs:362)
EventDelegate:Execute() (at Assets/NGUI/Scripts/Internal/EventDelegate.cs:417)
EventDelegate:Execute(List`1) (at Assets/NGUI/Scripts/Internal/EventDelegate.cs:566)
UITweener:Update() (at Assets/NGUI/Scripts/Tweening/UITweener.cs:224)

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #1 on: April 23, 2014, 02:29:33 AM »
I find that it works only if Execute() is using mTarget.SendMessage.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #2 on: April 23, 2014, 03:04:04 AM »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #3 on: April 23, 2014, 06:14:11 AM »
Awake() is private in your class. It's not visible to the derived class at all. You are trying to call a function that's physically invisible to the derived class.

Make it "protected" at least.

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #4 on: April 23, 2014, 06:46:37 AM »
I ran into this issue myself as well a few days ago and making the method "more visible" didn't change anything for me. I ended up overriding that particular method in the sub-class and copy-pasting the entire method to the subclass' overridden method.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #5 on: April 23, 2014, 06:53:23 AM »
You can try changing EventDelegate's line 355 from this:
  1. mMethod = type.GetMethod(mMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
to this:
  1. mMethod = type.GetMethod(mMethodName);
Looking at the changes, this seems like the most likely cause.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #6 on: April 28, 2014, 03:07:43 AM »
No, it didn't work.
Can I use SendMessage instead of Reflection ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #7 on: April 29, 2014, 02:22:13 PM »
If you comment out the "#define REFLECTION_SUPPORT" at the top of the EventDelegate file, it will do just that.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #8 on: April 29, 2014, 09:21:03 PM »
So, what is the usage of REFLECTION_SUPPORT ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #9 on: April 30, 2014, 06:36:06 PM »
I re-read your post. Why do you have this?
  1. EventDelegate.Set(tweenPosition.onFinished, () => TestMethod() );
and not this instead?
  1. EventDelegate.Set(tweenPosition.onFinished, TestMethod);

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #10 on: May 01, 2014, 11:44:25 PM »
It is just an simple example using lambda expression, it may be more complicated if needed.
e.g.

  1. int t = 0;
  2. EventDelegate.Set(tweenPosition.onFinished, () => {
  3. if ( t > 0 )
  4.    TestMethod1();
  5. else
  6.    TestMethod2();
  7. });



yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #11 on: May 01, 2014, 11:48:08 PM »
If new EventDelegate cannot support this situation, I am keen to modify all of my code.
And I just wonder to know why we need  REFLECTION_SUPPORT ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #12 on: May 02, 2014, 08:58:25 AM »
I don't know about lambda, but this works just fine:
  1. EventDelegate.Set(tweenPosition.onFinished, delegate () {
  2. if ( t > 0 )
  3.    TestMethod1();
  4. else
  5.    TestMethod2();
  6. });
Reflection is necessary to make the events show up in inspector and to make it possible to call them. The only alternative is to use SendMessage, which is limited to 1 parameter.

wishful_st

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
« Reply #13 on: May 14, 2014, 07:31:33 PM »
yuewah: see my post: http://www.tasharen.com/forum/index.php?topic=9511.0

I believe lambda functions are fine - you should be able to use them except for the case that I've described in the post which may be applicable to you, since you seem to be deriving other classes from your abstract class.

Workaround is to use named method instead of a lambda function if your callback function is declared via a lambda function in the base class and if that function will be triggered from an inherited class.