Author Topic: Crashing Unity when calling a method that uses TweenAlpha  (Read 8343 times)

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Crashing Unity when calling a method that uses TweenAlpha
« on: January 05, 2014, 08:20:49 PM »
Crashing Unity when calling a method that uses TweenAlpha

I am including my example script, I am unsure why this would cause Unity to hard crash.
If I take out the call to Fade from within TestMe() Unity is fine, if I have the call in as shown above, it hard crashes Unity.

I am hoping I am doing something wrong, but it not, I figured you may want to know about it.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ActivePlayerHUDController : MonoBehaviour {
  5.  
  6.     public UILabel cardLabel;
  7.     public UISprite card1;
  8.     public UISprite card2;
  9.     public UISprite card3;
  10.     public UISprite card4;
  11.     public UISprite card5;
  12.  
  13.     private TweenAlpha activePlayerHUDAlpha;
  14.     private bool isVisible;
  15.  
  16.     void Start() {
  17.         activePlayerHUDAlpha = this.GetComponent<TweenAlpha>();
  18.         Fade(activePlayerHUDAlpha, 0.0f, 1.0f, 10.0f, "TestMe");
  19.     }
  20.  
  21.     void Update() {  
  22.     }
  23.  
  24.     void Fade(TweenAlpha alpha, float fromValue, float toValue, float duration, string methodAsString) {
  25.         alpha.from = fromValue;
  26.         alpha.to = toValue;
  27.         alpha.duration = duration;
  28.         if (methodAsString != "") {
  29.             alpha.callWhenFinished = methodAsString;
  30.         }
  31.         alpha.style = UITweener.Style.Once;
  32.         alpha.eventReceiver = gameObject;
  33.         alpha.animationCurve = new AnimationCurve(
  34.             new Keyframe(0.0f, 0.0f, 1.0f, 1.0f),
  35.             new Keyframe(1.0f, 1.0f, 1.0f, 1.0f));
  36.         alpha.Play(true);
  37.     }
  38.  
  39.     void TestMe() {
  40.         Fade(activePlayerHUDAlpha, 1.0f, 0.0f, 3.0f, "");
  41.     }
  42. }
  43.  
  44.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #1 on: January 06, 2014, 10:20:56 AM »
First of all, you're starting the tween wrong. TweenAlpha.Begin should be used if you're starting the tween locally. It also creates the tween for you if it's not present. Second, you should not be using the 'eventReceiver' at all. It's deprecated functionality, kept only for backwards compatibility. UITweener.onFinished should be used instead.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #2 on: January 08, 2014, 05:53:49 AM »
Thanks for the reply, and yes this code is decently old, and was working in an older version of NGUI. I am happy to move towards the newer ways of doing things. I have been able to stop crashing due to the changes I made to my code, but I am not getting a repeating call to my EventDelegate, and I am unsure why that would be the case, as I am calling Fade() again from the method the EventDelegate was pointing to, but with a null the second time around instead of the EventDelegate that the original Fade() was using.

I am also curious if I am approaching this the right way now. I do have a pre-existing TweenAlpha and I still make reference to it, and set values against it. As I am changing those values on the line after the TweenAlpha.Begin() line, are they actually going to be set in time for the first firing of Begin? ( my guess is yes as we are still on the same frame that Begin was called on. If there is still something incorrect to my approach in regards to the latest NGUI please let me know. Thanks!

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ActivePlayerHUDController : MonoBehaviour {
  5.     #region // Member Variable Declaration
  6.     public UILabel cardLabel;
  7.     public UISprite card1;
  8.     public UISprite card2;
  9.     public UISprite card3;
  10.     public UISprite card4;
  11.     public UISprite card5;
  12.  
  13.     private TweenAlpha activePlayerHUDAlpha;
  14.     private bool isVisible;
  15.     #endregion
  16.  
  17.     void Start() {
  18.          activePlayerHUDAlpha = this.GetComponent<TweenAlpha>();
  19.          EventDelegate testDelegate = new EventDelegate(TestMe);
  20.          Fade(activePlayerHUDAlpha, 1.0f, 0.2f, 3.0f, testDelegate);
  21.          //Fade(activePlayerHUDAlpha, 1.0f, 0.2f, 3.0f, null);
  22.     }
  23.  
  24.      void Fade(TweenAlpha alpha, float fromValue, float toValue, float duration, EventDelegate test) {
  25.          TweenAlpha.Begin(gameObject, duration, toValue).onFinished.Add(test);
  26.          alpha.from = fromValue;
  27.          alpha.style = UITweener.Style.Once;
  28.          alpha.animationCurve = new AnimationCurve(
  29.                   new Keyframe(0.0f, 0.0f, 1.0f, 1.0f),
  30.                   new Keyframe(1.0f, 1.0f, 1.0f, 1.0f));
  31.         }
  32.    
  33.      void TestMe() {
  34.          Debug.Log("Fade called me");
  35.          Fade(activePlayerHUDAlpha, 0.2f, 1.0f, 3.0f, null);
  36.     }
  37. }
  38.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #3 on: January 08, 2014, 12:38:10 PM »
Add a listener like so:
  1. TweenAlpha tween = TweenAlpha.Begin(gameObject, duration.toValue);
  2. EventDelegate.Add(tween.onFinished, test, false);
The last parameter specifies what happens with the function after it's executed once. "True" means it's only executed once (so it gets removed after execution). "False" means it's kept in the list, and will be able to execute again in the future.

I would suggest you to set up a tween as you want it in inspector (which you seem to be doing?), and not modify it inside your Fade function. Why set the "from", "style", and "animationCurve"? The "from" is always the current value by default, and the other two are derived from values set in the inspector.

You're basically confusing two ways of doing things... if the tween is present, you activate it using the PlayForward() function. If the tween is not present, there is no point in keeping the "activePlayerHUDAlpha" variable at all, and you can create it by using TweenAlpha.Begin, as per my code example above. Note how it returns the reference to the created tween.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #4 on: January 08, 2014, 02:38:33 PM »
I tend to find the UI for editing the Animation Curve supplied in the TweenAlpha component to be lackluster, especially in regards to accuracy, for example lacking numerical input editing of the keys, hence why I may resort to using code in conjunction with a pre-existing TweenAlpha.  No sure if that UI is just supplied natively by Unity. As for mixing a TweenAlpha component with also trying to edit it via code seems completely reasonable to do, though I can see reasoning to go and use only one or the other. Thanks for the info on the listener, will modify my code once I am back at my computer.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #5 on: January 08, 2014, 09:44:30 PM »
Yeah, it's native to Unity.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #6 on: January 09, 2014, 05:21:26 AM »
Setting the EventDelegate.Add to true worked and only ran my method once. I did run into one more issue, I made TestMe() make a call back against Fade(), but this time with a new EvenDelegate parameter which points at Last(). For the rest of my testing I am only creating the TweenAlpha at runtime. Things are almost working, the issue is that after the first call to Fade() finished, I get notification of TestMe() having been run, and the call to Fade() works functionally correct, except the new EventDelegate that we are using on our .onFinished fires off immediately, instead of truly waiting for the fade to finish playing ( it is still fading up to our new 1.0f value and I watch the runtime TweenAlpha playing out correctly ( even though the EventDelegate pointing at Last() gets called prematurely ). Visually, the 2 calls to Fade() look like they are working visually, just the premature call to the second EventDelegate is confusing. Am I missing something, why would the new EventDelegate which we added to .onFinished fire off without the Tween actually having finished?

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ActivePlayerHUDController : MonoBehaviour {
  5.  
  6.     #region // Member Variable Declaration
  7.     public UILabel cardLabel;
  8.     public UISprite card1;
  9.     public UISprite card2;
  10.     public UISprite card3;
  11.     public UISprite card4;
  12.     public UISprite card5;
  13.  
  14.     private TweenAlpha activePlayerHUDAlpha;
  15.     private bool isVisible;
  16.     #endregion
  17.  
  18.     void Start() {
  19.         Fade(1.0f, 0.2f, 3.0f, new EventDelegate(TestMe));
  20.     }
  21.  
  22.     void Fade(float fromValue, float toValue, float duration, EventDelegate method) {
  23.         TweenAlpha tween = TweenAlpha.Begin(gameObject, duration, toValue);
  24.         if(method != null) {
  25.             EventDelegate.Add(tween.onFinished, test, true);
  26.         }
  27.         tween.from = fromValue;
  28.         tween.style = UITweener.Style.Once;
  29.         tween.animationCurve = new AnimationCurve(
  30.             new Keyframe(0.0f, 0.0f, 1.0f, 1.0f),
  31.             new Keyframe(1.0f, 1.0f, 1.0f, 1.0f));
  32.     }
  33.    
  34.     void TestMe() {
  35.         Debug.Log("Fade called me");
  36.         Fade(0.2f, 1.0f, 5.0f, new EventDelegate(Last));
  37.     }
  38.  
  39.     void Last() {
  40.         Debug.Log("Last call!");
  41.     }
  42. }
  43.  
  44.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #7 on: January 09, 2014, 07:12:33 PM »
If duration is not zero then it certainly shouldn't fire right away. It should only fire right away if duration is zero. I can't double-check right now as I'm on vacation atm.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #8 on: January 10, 2014, 02:18:09 AM »
I hope your having a good time! As for the duration, I can see during runtime that the duration is indeed not set to zero.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #9 on: January 10, 2014, 05:56:30 PM »
If you can remind me to have a look at it after the 16th, I would much appreciate it. :)

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #10 on: January 11, 2014, 06:20:39 AM »
Sure thing, in the meanwhile enjoy the sand and sun.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #11 on: January 17, 2014, 04:52:54 AM »
You wanted me to bring this to your attention once we were past the 16th. Hope you had a nice vacation!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #12 on: January 17, 2014, 09:54:31 PM »
Yup, thanks. Fixed it, so you will see the fix in 3.0.9 tonight.

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #13 on: January 20, 2014, 07:05:25 AM »
Cool, will give it a shot in the morning, thanks!

Abyss_DoD

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Crashing Unity when calling a method that uses TweenAlpha
« Reply #14 on: January 21, 2014, 04:45:47 AM »
All fixed, thanks ArenMook, the fix was appreciated!