Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

If you use iTween switch to Surge (I made both!)

Discussion in 'Editor & General Support' started by pixelplacement, Dec 16, 2017.

  1. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    After 4 years of work I finally released my developer toolset Surge for Unity. This suite of tools includes the next version of the globally used iTween, a state machine, singleton, display object, and spline. Surge is 100% free and has been battle tested on hundreds of projects and varying teams sizes to get work done faster and better in Unity.

    Grab Surge on the Asset Store: https://www.assetstore.unity3d.com/en/#!/content/107312

    And also grab the examples which support the project and help demonstrate advanced uses of this amazing set of solutions: https://www.assetstore.unity3d.com/en/#!/content/107314
     
    xCyborg, Mauri, Taran3D and 5 others like this.
  2. Dsiak

    Dsiak

    Joined:
    Oct 11, 2015
    Posts:
    11
    I just realized this was released a few days before I started my project. Talk about just in time! I'm loving the state machines so far!

    Can I ask something about the splines? Is it possible to fill the list of followers in the inspector all at once? And what about evenly distributing them across the path automatically?

    I have been doing this operations a lot.
     
    pixelplacement likes this.
  3. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    That's the only way to distribute them through the inspector. If you want easier control I'd recommend simply making your own array and then using the GetPosition method to get them laid out. As for distribution, we are working with a pure spline here so the rules of a spline exist. I'll take a look and see if I can add a helper that will make even distribution easier for you.
     
  4. neapolitanstudios

    neapolitanstudios

    Joined:
    Feb 17, 2011
    Posts:
    48
    I'm having some issues with GetPosition with a spline.

    I've basically copied your example in the documentation:
    Code (CSharp):
    1. using UnityEngine;
    2. using Pixelplacement;
    3.  
    4. public class SplineUser : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public Spline spline;
    8.     [Range (0,1)] public float percentage;
    9.  
    10.     void Update ()
    11.     {
    12.         target.position = spline.GetPosition (percentage);
    13.     }
    14. }
    but I am getting a NULLReferenceException error at Pixelplacement.Spline.GetPosition (Single percentage) (at Assets/Pixelplacement/Surge/Spline/Objects/Spline.cs:255)

    I'm not quite sure what's going on here..
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,194
    Question... I haven't used iTween in long time, one of the challenges I had with the pathing is that it didn't interpolate evenly along the whole path, (movement would be faster between nodes that were closer). Is that something that has been addressed in Surge (spline)?
     
  6. neapolitanstudios

    neapolitanstudios

    Joined:
    Feb 17, 2011
    Posts:
    48
    I managed to sort out my NullReferenceException error (I had an initialization script from an old state machine script on the spline object, which started doing strange things)

    However, I am still having no luck with my spline's GetPosition function. It is constantly returning a zero.

    Alternatively, you mentioned in one of the asset store reviews that you had Pause/Resume functionality.. Stop works fairly well as a Pause, however whenever I use Start to resume my tween, it jumps back to the beginning.. Is there a way to have it resume from it's location?
     
  7. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    Responded to your emails and hope what I provided is able to get you rolling!
     
  8. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    I am using a much more robust and accurate spline system in Surge. Try it out and let me know if it is working how you'd expect. If not let me know and I'll add some more functionality.
     
  9. darkmax

    darkmax

    Joined:
    Feb 21, 2012
    Posts:
    77
    Hi I was trying the surge package and is awsome, but just some of the other things that some users already post I'm dealing, first I'm trying to evenly distribute object along the path, and the other thing is on nodes that are far away from each other the speed of the objects travel faster than the anchors that are closer. Even if I have a linear tween animation.

    I want to Animate the objects with linear animation along all the path, so how can I achieve this?

    This is my line to animate each object, so I'm using linear animation, but it doesn't seem to be like that.
    Code (CSharp):
    1. Tween.Spline(mySpline, sf.target, 0, 1, true, duration, 1/10.0f, Tween.EaseLinear, Tween.LoopType.Loop);
    This is a gif of the animation that I have right now
     
  10. rberkebile

    rberkebile

    Joined:
    Mar 22, 2018
    Posts:
    2
    Yup. I'm aware of this request to keep things at a consistent velocity despite the spline's layout. Stand by... I'll have a solution for this soon!
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,194
    Yea, that is issue I meant above the even interpolation along a path. That’s why I stopped using a few years back.
     
  12. rberkebile

    rberkebile

    Joined:
    Mar 22, 2018
    Posts:
    2
    I solved the issue with uneven spline interpolation and will have an update soon. In the meantime, here's a tease...

    Capture.PNG
     
  13. darkmax

    darkmax

    Joined:
    Feb 21, 2012
    Posts:
    77
    Thanks that's awesome, I will update my project immediately
     
  14. darkmax

    darkmax

    Joined:
    Feb 21, 2012
    Posts:
    77
    I just having some trouble with my spline, I want to limit the face direction in two axis how I can achieve this? right now when I rotate the parent of the spline all objects that moves along the spline rotate in three axis so sometimes they are viewing correctly to the camera and sometimes they are not.

    I hope explain myself, if not I will submit a video or gif explaining me better

    Edit: Here is the gif


    Here is a gif explaning what I'm referring, if you see if I rotate the parent of the spline it looks that the followers always stay with the same orientation instead of rotating with the orientation of the path
     
    Last edited: Apr 13, 2018
  15. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    By default, the Tween system in Surge will use world up when calculating the look direction. Since you want to instead lock this to the Spline's "right" axis you will need to simply run your own solution to apply this in conjunction with a Value tween:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Pixelplacement;
    5.  
    6. public class SplineLookSolution : MonoBehaviour
    7. {
    8.     //Public Variables
    9.     public Spline spline;
    10.     public Transform target;
    11.  
    12.     //Init
    13.     private void Awake()
    14.     {
    15.         Tween.Value(0f, 1f, HandleSplinePosition, 6, 0, null, Tween.LoopType.Loop);
    16.     }
    17.  
    18.     //Event Handlers
    19.     void HandleSplinePosition(float value)
    20.     {
    21.         target.position = spline.GetPosition(value);
    22.  
    23.         //set the world up (last parameter) to be the axis of the spline you want to act as up:
    24.         target.LookAt(spline.GetDirection(value), spline.transform.right);
    25.     }
    26. }
     
  16. darkmax

    darkmax

    Joined:
    Feb 21, 2012
    Posts:
    77
    Thank you very much, I will experiment with the code you posted
     
  17. IndieGeek

    IndieGeek

    Joined:
    Sep 30, 2016
    Posts:
    16
    How to create run-time spline?
     
  18. sidartha138

    sidartha138

    Joined:
    Jan 25, 2015
    Posts:
    4
    Hello, I have been playing around with iTween for a week or so and really enjoying it's features (thanks for developing it!), although I'm stuck on a problem.

    I'm using the grid movement example for the following and am having issues and am trying to make the character "hop" from one square to the next. I can move up/down/left/right one space at a time with no problem. I can also jump up and down with no problem.

    The issue is when I am trying to use both at the same time; that is, hop from one spot to the next. See the comments in the code below.

    Any suggestions? Would Surge help, or is it too advanced for what I'm trying to do?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         if (Input.GetKeyDown("space"))
    9.         {
    10.             //This works
    11.             iTween.MoveTo(gameObject, iTween.Hash("y", 2.5f, "time", 1, "easeType", iTween.EaseType.linear));
    12.             iTween.MoveFrom(gameObject, iTween.Hash("y", 2.5f, "time", 0.1f, "delay", 0.0f, "easetype", iTween.EaseType.linear));
    13.         }
    14.         if (Input.GetKeyDown(KeyCode.W))
    15.         {
    16.             //This works
    17.             iTween.MoveBy(gameObject, iTween.Hash("x", 1.0f, "time", 0.1f, "easeType", iTween.EaseType.linear));
    18.         }
    19.         if (Input.GetKeyDown(KeyCode.S))
    20.         {
    21.             //This works
    22.             iTween.MoveBy(gameObject, iTween.Hash("x", -1.0f, "time", 0, "easeType", iTween.EaseType.linear));
    23.         }
    24.         if (Input.GetKeyDown(KeyCode.A))
    25.         {
    26.             //This works
    27.             iTween.MoveBy(gameObject, iTween.Hash("z", 1.0f, "time", 0, "easeType", iTween.EaseType.linear));
    28.         }
    29.         if (Input.GetKeyDown(KeyCode.D))
    30.         {
    31.             //This works
    32.             iTween.MoveBy(gameObject, iTween.Hash("z", -1.0f, "time", 0, "easeType", iTween.EaseType.linear));
    33.         }
    34.  
    35.         //This does not work, it moves from 0,1,0 to 0,2.5f,0
    36.         if (Input.GetKeyDown(KeyCode.Q))
    37.         {
    38.             iTween.MoveTo(gameObject, iTween.Hash("y", 2.5f, "time", 1, "easeType", iTween.EaseType.linear));
    39.             iTween.MoveFrom(gameObject, iTween.Hash("y", 2.5f, "time", 0.1f, "delay", 0.0f, "easetype", iTween.EaseType.linear));
    40.             iTween.MoveBy(gameObject, iTween.Hash("x", 1.0f, "time", 0.1f, "easeType", iTween.EaseType.linear));
    41.         }
    42.  
    43.         //This does not work, it moves from 0,1,0 to 0,2.5f,0 then to to 1,2.5f,0 then back to 0,1,0
    44.         if (Input.GetKeyDown(KeyCode.Z))
    45.         {
    46.             iTween.MoveBy(gameObject, iTween.Hash("x", 1.0f, "delay", 1.1f, "easeType", iTween.EaseType.linear));
    47.             iTween.MoveTo(gameObject, iTween.Hash("y", 2.5f, "delay", 2.2f, "easeType", iTween.EaseType.linear));
    48.             iTween.MoveFrom(gameObject, iTween.Hash("y", 2.5f, "delay", 3.3f, "easetype", iTween.EaseType.linear));
    49.            
    50.         }
    51.     }
    52. }
    53.  
     
  19. Mr-Logan

    Mr-Logan

    Joined:
    Apr 13, 2006
    Posts:
    455
    I just discovered that this had been released and went ahead and downloaded it asap!
    What I want to use it for is moving (tweening) the player object on the world map (at least that's the first thing).
    If I simply do "Tween.Position" I come up with two issues
    1) Right now it uses duration rather than speed (which means that short distances will be really slow, and long distances will go really fast).
    2) is that it moves 'mechanically' from point A to point B.
    I'd like it to be more of a flourish, kind of like if I'd used a spline (and also for the 'player piece' to point in the movement direction)

    Is there a way to generate a movement spline on the fly during runtime?

    EDIT

    I just discovered that the AddAnchors method, actually returns the anchors as gameobjects, doh.
    But now I'm getting a null reference exception in Spline.cs, line 66
    if (transform.childCount != _previousAnchorCount || transform.childCount == 0)

    EDIT
    Doh.. I hadn't noticed it was a monobehaviour.

    Solution:
    So in the end I figured something out



    Code (CSharp):
    1.  
    2. var pathGo = new GameObject("Spline");
    3. var spline = pathGo.AddComponent<Spline>();
    4. spline.AddAnchors(2);
    5.  
    6. var anchors = pathGo.GetComponentsInChildren<SplineAnchor>();
    7.  
    8. anchors[0].transform.position = transform.position;
    9. anchors[1].transform.position = transform.position + transform.forward * 10;
    10. anchors[2].transform.position = Vector3.Lerp(anchors[1].transform.position, target, 0.5f);
    11. anchors[3].transform.position = target;
    12. foreach (var anchor in anchors)
    13. {
    14.     anchor.tangentMode = TangentMode.Free;
    15.     anchor.transform.localScale = Vector3.one * 3;
    16. }
    17.  
    This isn't smooth and it isn't neat, but it does give a spline. Also the scale part doesn't actually scale the gameobjects (annoyingly enough), but at least it creates a path :)

    EDIT
    This works way better, it still isn't perfect but it's pretty darn close.
    Code (CSharp):
    1.  
    2. var pathGo = new GameObject("Spline");
    3. var spline = pathGo.AddComponent<Spline>();
    4. var anchors = pathGo.GetComponentsInChildren<SplineAnchor>();
    5.  
    6. anchors[0].transform.position = transform.position;
    7. anchors[1].transform.position = target;
    8. anchors[0].tangentMode = TangentMode.Free;
    9. anchors[1].tangentMode = TangentMode.Free;
    10.  
    11.  
    12. var splineTan = anchors[0].transform.Find("OutTangent");
    13. splineTan.position = anchors[0].transform.position + (transform.forward * 100);
    14.  
    15. splineTan = anchors[1].transform.Find("InTangent");
    16.  
    17. var directionVector = target - transform.position;
    18. var dotResult = Vector3.Dot(transform.forward, directionVector);
    19.  
    20. if (dotResult > 0)
    21.     splineTan.position = ((transform.position - target).normalized * 30) + target;
    22. else
    23.     splineTan.position = target + (transform.right * 100);
    24.  
     
    Last edited: Apr 24, 2018
  20. Dankann

    Dankann

    Joined:
    Oct 25, 2016
    Posts:
    2
    Is there any solution for the linear velocity along the spline already?
     
  21. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28
    Hey, is this still coming? If it's not going to be in the asset store, can you share the build shown in the image? Thanks!
     
  22. DesigningKnight

    DesigningKnight

    Joined:
    Aug 21, 2018
    Posts:
    21
    Hi. I'm new to the whole spline thing. I understand what TangentMode.Mirrored and Free do, but can someone explain Aligned? I'm not sure exactly what that does compared to the other two.
     
  23. DesigningKnight

    DesigningKnight

    Joined:
    Aug 21, 2018
    Posts:
    21
    Hey a quick question, is there an easy way to remove an anchor point from an existing spline at runtime?
     
  24. gaiastellar

    gaiastellar

    Joined:
    Nov 8, 2013
    Posts:
    47
    hi. ive been working with surge and its great!!

    however i'm having trouble with Tween - i'm using completeCallback to call a function when it finished, but my function is something like 'public void oncompletemove(vector 3)', but when i add the vector 3 in the tween like command like this:
    Tween.LocalPosition(myobject.transform, vector3,2,0,Tween.EaseInOutStrong,Tween.LoopType.None,completeCallback:myfunction(myvector3));

    but it throws an error, complaining about the part at the end in brackets. it wont accept any variables getting passed with the function name.

    any help please?

    many thanks

    paul uk
     
  25. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28


    Hey Paul, DoSomething(float) shows how to pass a function reference with a value, and the delegate part shows an alternative way to just run some statements on completion.

    Code (CSharp):
    1. Tween.Value(1f, 2f, DoSomething, 2.1f, 0, completeCallback: delegate {
    2.                 Debug.Log("Tween Competed");
    3.             });
    4.  
    5. protected void DoSomething(float val)
    6.         {
    7.             Debug.Log(val);
    8.         }

    The terms to read up on are actions, delegates and events in C# :)
     
  26. Gabrio

    Gabrio

    Joined:
    Jan 31, 2016
    Posts:
    14
    Hi, is there a possbility to change the duration/speed of a Tween.spline runtime?
    Thanks
     
  27. MasonEmpyreal

    MasonEmpyreal

    Joined:
    Jul 12, 2018
    Posts:
    1
    Firstly, thank you for creating such a useful development toolset and making it available to the community free of charge!

    I was, however, disappointed to learn that it was not possible to perform interpolation relative to the entire spline instead of each individual curve. As some others have noted, this causes objects to move at an inconsistent speed.

    Since it's unclear when, or if, this feature will be implemented, I decided to implement it myself. I have attached the modified cs files to this post. Place the attached files in the Surge\Spline\Objects directory (replacing the old files). I haven't yet had a chance to robustly test the new functionality, so I cannot guarantee that it is free of bugs.

    To enable the feature, check the 'Constant Velocity' toggle of the 'Spline (Script)' component in the inspector.

    @ Developer: Please feel free to incorporate my changes, if you wish.

    Edit: Fixed a bug that caused an exception for non-looping splines. Sorry about that!
     

    Attached Files:

    Last edited: Dec 3, 2018
    jethrogillgren likes this.
  28. IvyKun

    IvyKun

    Joined:
    Sep 28, 2013
    Posts:
    98
    An update came out today but there's nothing in the release notes about this.

    @pixelplacement ?
     
    Last edited: Jan 30, 2019
  29. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    I FINALLY solved consistent speed on splines regardless of anchor placement and arrangement. I'll have this wrapped up and included in the next updated as soon as I can!

     
  30. IvyKun

    IvyKun

    Joined:
    Sep 28, 2013
    Posts:
    98
    Niiiiice!
     
  31. pixelplacement

    pixelplacement

    Joined:
    Oct 18, 2008
    Posts:
    11
    Just pushed 1.0.39 to the Asset Store which finally brings normalized spline traversal. Thanks for the patience everyone.
     
    Taeren, der_abu and IvyKun like this.
  32. sabotage3d

    sabotage3d

    Joined:
    Nov 10, 2014
    Posts:
    5
    How does it compare performance-wise to other tweening libraries?
     
  33. danielmahon

    danielmahon

    Joined:
    Jan 23, 2014
    Posts:
    4
    @pixelplacement Hey there! Just started using Surge and already I can see my code size shrinking and getting cleaner. So thanks for that! I have a question about switching states asynchronously or how to (for example) use a StateMachine to fade in/out canvases...? Right now it looks like that's hard to do with OnDisable since the state changes are handling by disabling and enabling GameObjects. Am I missing a good way to do this as-is? I'm currently leaning towards adding an "IsAsync" flag to the State class. and then doing a check in the StateMachine class where it would normally just switch it off. Thoughts? Should something like this be implemented in the package? Thanks and keep it up!

    Code (CSharp):
    1.         /// <summary>
    2.         /// Exit the current state.
    3.         /// </summary>
    4.         public void Exit() {
    5.             if (currentState == null) return;
    6.             Log("(-) " + name + " EXITED state: " + currentState.name);
    7.             int currentIndex = currentState.transform.GetSiblingIndex();
    8.  
    9.             //no longer at first:
    10.             if (currentIndex == 0) AtFirst = false;
    11.  
    12.             //no longer at last:
    13.             if (currentIndex == transform.childCount - 1) AtLast = false;
    14.  
    15.             if (OnStateExited != null) OnStateExited.Invoke(currentState);
    16.  
    17.             // Check is State Component wants to be treated as async
    18.             State stateComponent = currentState.GetComponent<State>();
    19.             // I added the "IsAsync" field to the State class
    20.             if (stateComponent.IsAsync) {
    21.                 // This is an async/await method overridden on my State class
    22.                 // but you could also use a Coroutine.
    23.                 // You need to manually call gameObject.SetActive(false); in this method
    24.                 stateComponent.AsyncOnDisable();
    25.             } else {
    26.                 currentState.SetActive(false);
    27.             }
    28.             currentState = null;
    29.         }
    30.  
    PS: Thoughts on implementing "Awaiters" for your Tweens? :wink: http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/
     
    Last edited: Apr 27, 2019
  34. Taeren

    Taeren

    Joined:
    Oct 10, 2013
    Posts:
    11
    Can we have something like "Initial placement of spline" on the Loop Over Spline script?

    Basically I have 1 spline, with two game objects to follow each other over the loop, with one slightly ahead of the other.

    I tried using delay, but that occurs on every loop + it moves the position every loop too.
     
    Last edited: May 30, 2019
  35. Taeren

    Taeren

    Joined:
    Oct 10, 2013
    Posts:
    11
    From what I've read, iTween was not ideal on performance with a large amount of objects due to GC, and the author has taken that into consideration when creating Surge.

    I haven't ever gotten to the scale where it matters, so my opinion may not be valid :)
     
    Last edited: May 30, 2019
  36. Alic

    Alic

    Joined:
    Aug 6, 2013
    Posts:
    121
    Is anyone running into broken tweens in 2019? Using 2019.1.3 with the latest Surge and tweens seem to start with a weird time offset on the animation curve, so some of them are playing starting at 50% of the animation curve, then getting to the end, going back to the beginning, and playing to 50%. This is with no looping in the tween call.
     
  37. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    165
    Hey anyone having issues with 1.0.40 Jun 24, 2019 update. I discovered a bug in the TweenEngine. I have emailed developer about it. Just replace the entire file with this.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Pixelplacement.TweenSystem
    4. {
    5.     public class TweenEngine : MonoBehaviour
    6.     {
    7.         public void ExecuteTween(TweenBase tween)
    8.         {
    9.             Tween.activeTweens.Add(tween);
    10.         }
    11.  
    12.         private void Update()
    13.         {
    14.             Tween.activeTweens.RemoveAll(x => !x.Tick());
    15.         }
    16.     }
    17. }
    Hope it helps
     
    Alic likes this.
  38. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    165
    Bit delayed. But I only updated to the new version couple days ago. Hopefully my above fix helps you as well :)
     
    Alic likes this.
  39. Alic

    Alic

    Joined:
    Aug 6, 2013
    Posts:
    121
    Nice! Thanks Brogan! I actually can't even remember what the buggy behavior was in my game related to Surge, and whether or not I already worked around it, but this is great to get fixed. Thanks for the heads up.
     
    Brogan89 likes this.
  40. delVhar

    delVhar

    Joined:
    Sep 29, 2012
    Posts:
    9
    I ran into a problem making a "windmill" with multiple evenly spaced platforms moving along a circular spline.
    I made a couple of tweaks to the Surge/Tween/TweenActions/SplinePercentage.cs file as below:

    In public SplinePercentage, change:
    Code (CSharp):
    1. startPercentage = Mathf.Clamp01 (startPercentage);
    2. endPercentage = Mathf.Clamp01 (endPercentage);
    To:
    Code (CSharp):
    1. startPercentage = Mathf.Clamp(startPercentage, -1, 2);
    2. endPercentage = Mathf.Clamp(endPercentage, -1, 2);
    Then add the following lines in the Operation function, after calculatedValue is set, but before it is used to set _target.position:
    Code (CSharp):
    1. if(calculatedValue > 1)
    2.     calculatedValue-=1;
    3. else if(calculatedValue < 0)
    4.     calculatedValue += 1;
    Attaching a gif showing the working rotation.
     

    Attached Files:

  41. paul-velocity

    paul-velocity

    Joined:
    Oct 20, 2012
    Posts:
    7
    Cant even get it up and running ...

    Ive tried declaring them:

    using Pixelplacement;
    using Pixelplacement.TweenSystem;

    And get an error, "using directive is unnecessary".
    Therefore the term tween (for example) is not recognised if i remove or keep: "using Pixelplacement"
     
  42. neoRiley

    neoRiley

    Joined:
    Dec 12, 2008
    Posts:
    142
    @pixelplacement I saw this post and thought I'd say "Thank you" for your work with iTween and Surge - you've made my life much easier at work and when I applied at the company you worked for, despite the fact that I had a horrible interview, you were very cool to me, and I've never really forgotten about that.

    So, with that said, I thought I'd try and give back a little. I don't know if someone else has done this and I couldn't seem to locate it in the api for Surge, so I wrote a simple extension that allows you to set the AnimationCurve type in monobehaviours and via a nice enum setup :)

    First, the class - this basically farms the available animation curves in Surge that come stock and then creates an extension method GetCurve() to utilize the curve in your code:

    Code (CSharp):
    1. using Pixelplacement;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Reflection;
    5. using UnityEngine;
    6.  
    7.  
    8.     public static class SurgeUtils
    9.     {
    10.         static Dictionary<AnimationCurveTypes, AnimationCurve> curves;
    11.  
    12.         [RuntimeInitializeOnLoadMethod]
    13.         public static void Initialize()
    14.         {
    15.             curves = new Dictionary<AnimationCurveTypes, AnimationCurve>();
    16.          
    17.             Type tweenType = typeof(Tween);
    18.             foreach (AnimationCurveTypes curve in Enum.GetValues(typeof(AnimationCurveTypes)))
    19.             {
    20.                 if (curve.Equals(AnimationCurveTypes.EaseLinear)) continue;
    21.                 PropertyInfo prop = tweenType.GetProperty(curve.ToString());
    22.  
    23.                 if (prop.GetValue(curve.ToString()) != null)
    24.                 {
    25.                     curves.Add(curve, (AnimationCurve)prop.GetValue(curve.ToString()));
    26.                 }
    27.             }
    28.         }
    29.  
    30.  
    31.         /// <summary>
    32.         /// extention method for AnimationCurve to allow someone to
    33.         /// select a AnimationCurveType enum and get the surge AnimationCurve back
    34.         /// </summary>
    35.         public static AnimationCurve GetCurve(this AnimationCurveTypes value)
    36.         {
    37.             AnimationCurve curve = Tween.EaseLinear;
    38.             curves.TryGetValue(value, out curve);
    39.  
    40.             return curve;
    41.         }
    42.  
    43. }
    Then, create this enum definition:
    Code (CSharp):
    1.  
    2. public enum AnimationCurveTypes
    3. {
    4.     EaseLinear,
    5.     EaseIn,
    6.     EaseInStrong,
    7.     EaseOut,
    8.     EaseOutStrong,
    9.     EaseInOut,
    10.     EaseInOutStrong,
    11.     EaseInBack,
    12.     EaseOutBack,
    13.     EaseInOutBack,
    14.     EaseSpring,
    15.     EaseBounce,
    16.     EaseWobble
    17. }
    Sample code:
    Code (CSharp):
    1. // declare a public property to expose in IDE if you want...
    2. // you can also do: AnimationCurveTypes.EaseLinear.GetCurve();
    3. public AnimationCurveTypes curve = AnimationCurveTypes.EaseLinear;
    4.  
    5. // later in your code...
    6. void OnDoSomething()
    7. {
    8.     Tween.Value
    9.     (
    10.         cam.backgroundColor,
    11.         color,
    12.         (Action<Color>)(x =>
    13.         {
    14.             cam.backgroundColor = x;
    15.         }),
    16.         1.0f, 0, curve.GetCurve(), Tween.LoopType.None, null,
    17.         () =>
    18.         {
    19.             // do something when complete
    20.         }
    21.     );
    22. }
    Thanks again, hope all is well - John
     
    Last edited: Apr 14, 2020
unityunity