Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Changing how Animation Curve window looks?

Discussion in 'Scripting' started by alexanderameye, Aug 18, 2017.

  1. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,372
    I have a public AnimationCurve that shows up in the inspector, but I'd like to choose the ranges of the grid manually.

    So when I click on the Animation curve in the inspector window I want this to show up with the grid a certain size etc. This image was created using EditorGUILayout.CurveField. So I need this:

    upload_2017-8-18_21-36-57.png

    instead of this:

    upload_2017-8-18_21-36-5.png


    How can I make it so that when I click on a regular public AnimationCurve in the inspector, the window shows up just as I can make it show up with EditorGUILayout.CurveField?

    Any help is really really appreciated.

    I have this code in a regular non-editor script

    Code (CSharp):
    1.     public class RotationTimelineData
    2.     {
    3.  
    4.         public AnimationCurve Curve;
    5.  
    6.     }
     

    Attached Files:

    Last edited: Aug 18, 2017
  2. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,372
    FIXED IT!

    I had to write a custom PropertyAttribute! Now I can just write this:

    Code (CSharp):
    1.    public class RotationTimelineData
    2.     {
    3.         [Curve(5f, 5f)]
    4.         public AnimationCurve Curve;
    5.  
    6.     }
    and that will set a range for the grid!

    I'm happy I learned something today!
     
    ProbePLayer, rv0000s and Doug_B like this.
  3. kookyoo

    kookyoo

    Joined:
    Apr 19, 2010
    Posts:
    49
    Hi Alex,
    It would be very nice if you could share your PropertyAttribute as I definitely could have use of it too :)
     
  4. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,372
    Sure! I have an exam in about 20 minutes but I'll clean it up a bit and post it after that :)
     
    Egad_McDad, rv0000s and Munchy2007 like this.
  5. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,372
    Code (CSharp):
    1. // CurveDrawer.cs
    2. // Created by Alexander Ameye
    3. // Version 1.1.0
    4.  
    5. using UnityEngine;
    6. using UnityEditor;
    7.  
    8. [CustomPropertyDrawer(typeof(CurveAttribute))]
    9. public class CurveDrawer : PropertyDrawer
    10. {
    11.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    12.     {
    13.         CurveAttribute curve = attribute as CurveAttribute;
    14.         if (property.propertyType == SerializedPropertyType.AnimationCurve)
    15.         {
    16.             if (curve.b) EditorGUI.CurveField(position, property, Color.cyan, new Rect(curve.PosX, curve.PosY, curve.RangeX, curve.RangeY));
    17.         }
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. // CurveAttribute.cs
    2. // Created by Alexander Ameye
    3. // Version 1.1.0
    4.  
    5. using UnityEngine;
    6.  
    7. public class CurveAttribute : PropertyAttribute
    8. {
    9.     public float PosX, PosY;
    10.     public float RangeX, RangeY;
    11.     public bool b;
    12.     public int x;
    13.  
    14.     public CurveAttribute(float PosX, float PosY,float RangeX, float RangeY, bool b)
    15.     {
    16.         this.PosX = PosX;
    17.         this.PosY = PosY;
    18.         this.RangeX = RangeX;
    19.         this.RangeY = RangeY;
    20.         this.b = b;
    21.     }
    22. }
    23.  
    Don't forget to put CurveDrawer.cs in an editor folder. You can use this code by doing something like:

    Code (CSharp):
    1. [Curve(0, 0, 1f, 1f, true)]
    2.         public AnimationCurve RotationCurve;
    I picked the curves to be cyan but you can of course choose something else.

    Good luck!
     
  6. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    711
    Thanks buddy.
     
  7. raducristiandimitrie

    raducristiandimitrie

    Joined:
    Nov 6, 2018
    Posts:
    2
    Great work! Thank you!
    I changed your code a little to display the name of the AnimationCurve, since it was blank in the editor:

    Code (CSharp):
    1. if (curve.b) EditorGUI.CurveField(position, property, Color.cyan, new Rect(curve.PosX, curve.PosY, curve.RangeX, curve.RangeY), label);
     
    Michael_Berna likes this.