Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Copy and paste curves?

Discussion in 'Editor & General Support' started by dijital, Dec 14, 2012.

  1. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    Hi, I have a racing game I am working on, I have setup an acceleration curve to control a non player car and would like to apply that curve to my other cars.

    Is there a way to copy and paste a curve? as it is mind numbing trying to do it manually.

    Thanks!
     
  2. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    I'll take that as a 'no' then.
     
  3. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    32
    Try this ;)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomPropertyDrawer(typeof(AnimationCurve))]
    6. public class CurvePropertyDrawer: PropertyDrawer {
    7.    
    8.     private const int _buttonWidth = 12;
    9.    
    10.     private static Keyframe[] _buffer;
    11.     private static WrapMode _preWrapMode;
    12.     private static WrapMode _postWrapMode;
    13.    
    14.     public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
    15.         prop.animationCurveValue = EditorGUI.CurveField(
    16.             new Rect(pos.x, pos.y, pos.width - _buttonWidth * 2, pos.height),
    17.             label,
    18.             prop.animationCurveValue
    19.         );
    20.         // Copy
    21.         if (
    22.             GUI.Button(
    23.                 new Rect(pos.x + pos.width - _buttonWidth * 2, pos.y, _buttonWidth, pos.height),
    24.                 ""
    25.             )
    26.         ) {
    27.             _buffer = prop.animationCurveValue.keys;
    28.             _preWrapMode = prop.animationCurveValue.preWrapMode;
    29.             _postWrapMode = prop.animationCurveValue.postWrapMode;
    30.         }
    31.         GUI.Label(
    32.             new Rect(pos.x + pos.width - _buttonWidth * 2, pos.y, _buttonWidth, pos.height),
    33.             "C"
    34.         );
    35.         // Paste
    36.         if (_buffer == null) return;
    37.         if (
    38.             GUI.Button(
    39.                 new Rect(pos.x + pos.width - _buttonWidth, pos.y, _buttonWidth, pos.height),
    40.                 ""
    41.             )
    42.         ) {
    43.             AnimationCurve newAnimationCurve = new AnimationCurve(_buffer);
    44.             newAnimationCurve.preWrapMode = _preWrapMode;
    45.             newAnimationCurve.postWrapMode = _postWrapMode;
    46.             prop.animationCurveValue = newAnimationCurve;
    47.         }
    48.         GUI.Label(
    49.             new Rect(pos.x + pos.width - _buttonWidth, pos.y, _buttonWidth, pos.height),
    50.             "P"
    51.         );
    52.     } // OnGUI()
    53.    
    54. } // class CurvePropertyDrawer
    55.  
    Put this script in Editor folder.
     
    Last edited: Jun 19, 2013
  4. TomPendergrass

    TomPendergrass

    Joined:
    Apr 20, 2013
    Posts:
    3
    Hey there, just tried out that script; works like a charm! I think this should be a standard feature. I can't thank you enough for saving me soooo much time. I'm using Animation Curves to control object velocity for special maneuvers. Time to make some magic :D
     
  5. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    496
    Thank you!! That's a very neat piece of code there!

    Cheers
     
  6. Dark Keny

    Dark Keny

    Joined:
    Aug 1, 2013
    Posts:
    1
    Hello,

    I put this script in Editor folder but I can't see any copy/paste functionality in Animation window. Could you please explain me how to use it?

    Thanks in advance,
    Alejandro.
     
  7. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    32
    It's for AnimationCurve property.
    Not Animation window!
     
  8. John_MLS

    John_MLS

    Joined:
    Sep 11, 2013
    Posts:
    21
    Thank you!
     
    Last edited: May 28, 2015
  9. Elsammak

    Elsammak

    Joined:
    Nov 11, 2013
    Posts:
    9
    I've added this code to the Editor folder but I don't know how to use it or what to do.
    Any help will be appreciated.
    Thanks in advance.
     
  10. Ross_S

    Ross_S

    Joined:
    Jul 10, 2012
    Posts:
    29
    Hi - this seems awesome for animationCurves defined in your own scripts - but doesn't work for animationCurves that you've added to animations... (in the Curve section...) any c
     
  11. Sixus1Media

    Sixus1Media

    Joined:
    Aug 26, 2013
    Posts:
    5
    I also think this could be incredibly useful, but am unclear on how to install/use it. :(
     
  12. dijital

    dijital

    Joined:
    Apr 28, 2010
    Posts:
    232
    Haven't logged in in a long time, Thanks!
     
  13. imlabel

    imlabel

    Joined:
    Nov 24, 2014
    Posts:
    1
    define a variable of type AnimationCurve, and in the 'inspector' property panel, you'll see it,,,,
     
  14. bfg_jeff

    bfg_jeff

    Joined:
    Jan 23, 2015
    Posts:
    1
    Thanks a lot for that script :)))
     
  15. Kamilche_

    Kamilche_

    Joined:
    Jan 11, 2015
    Posts:
    74
    Is it possible to do the same thing for the Particle system curves?
     
  16. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    7,903
    For those just add it as a preset. Then select it when want to "paste" it.
     
    deab likes this.
  17. wuhuan

    wuhuan

    Joined:
    Mar 12, 2014
    Posts:
    7
  18. Pkleynhans

    Pkleynhans

    Joined:
    May 2, 2016
    Posts:
    1
    Awesome!!!
    For those not knowing how to use:
    1. Create Editor folder in your project (I did it within my Scripts folder, ending with Scrips\Editor)
    2. Create C# script, I named it CurveCopier (Obviously, move this script into the editor folder if it is not there yet)
    3. Edit this script, kill all its contents and place the above script within it.
    4.SAVE
    5. Now look next to your curve property on the script where you defined your curve... a small "C" button appeared
    6. Click that button and a "P" button will appear
    7. Now go to your target script with the curve you want to paste to, click the "P" and Voila!!!
    (In my case, the curve picture suddenly matched my source curve, proving it worked.

    Thanks again, I was just about to start typing values manually.
     
    theANMATOR2b likes this.
  19. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    891
    Awesome... thanks!
     
  20. MusiuIsmael

    MusiuIsmael

    Joined:
    Jun 29, 2014
    Posts:
    1
    I know this is really old, but thank you
     
  21. pjbaron

    pjbaron

    Joined:
    Jan 12, 2017
    Posts:
    10
    Still useful 5 years on :)
     
  22. off99555

    off99555

    Joined:
    Jul 25, 2016
    Posts:
    2
    The script is useful. It adds Copy and Paste button next to the animation curve indeed.
    But it causes the Tooltip() on the animation curve field to disappear.
    How do you fix that?
     
    macagu likes this.
  23. macagu

    macagu

    Joined:
    Sep 30, 2012
    Posts:
    7
    Thank you!

    This also solves the Tooltip not appearing issue.

    I had to remove the
    EditorGUI.PrefixLabel
    inside AnimationCurveGUI.OnGUI to avoid duplicating the label (I'm using Unity 2019.1)
     
  24. mishasniper

    mishasniper

    Joined:
    Apr 14, 2013
    Posts:
    1
    Still useful 6 years later :)
    AShim-3D Thank YOU for quick fix !!!