Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: yuewah on April 23, 2014, 02:00:40 AM

Title: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah on April 23, 2014, 02:00:40 AM
  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)
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah on April 23, 2014, 02:29:33 AM
I find that it works only if Execute() is using mTarget.SendMessage.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: helmesjo on April 23, 2014, 03:04:04 AM
+1

Same issue as I reported here: http://www.tasharen.com/forum/index.php?topic=6265
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: ArenMook 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.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: rain 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.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: ArenMook 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.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah on April 28, 2014, 03:07:43 AM
No, it didn't work.
Can I use SendMessage instead of Reflection ?
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: ArenMook 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.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah on April 29, 2014, 09:21:03 PM
So, what is the usage of REFLECTION_SUPPORT ?
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: ArenMook 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);
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah 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. });


Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: yuewah 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 ?
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: ArenMook 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.
Title: Re: EventDelegate not working on () => {} if NGUI version > 3.5.5
Post by: wishful_st 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.