Select your preferred scripting language. All code snippets will be displayed in this language.
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closeposition | Rectangle on the screen to use for the field. |
label | Optional label to display in front of the field. |
value | The curve to edit. |
color | The color to show the curve with. |
ranges | Optional rectangle that the curve is restrained within. |
AnimationCurve The curve edited by the user.
Make a field for editing an AnimationCurve.
Curve field in an Editor Window.
#pragma strict
// Makes the selected GameObject follow the animation curve. // // Usage: Generate the curves for X,Y and Z axis of your desired GameObject // Select a GameObject and click Generate Curve. // Press Play and see your object moving. // Note that this is an Editor script.
class EditorGUICurveField extends EditorWindow {
var curveX : AnimationCurve = AnimationCurve.Linear(0,0,1,0); var curveY : AnimationCurve = AnimationCurve.Linear(0,0,1,1); var curveZ : AnimationCurve = AnimationCurve.Linear(0,0,1,0);
@MenuItem("Examples/Create Curve For Object") static function Init() { var window = GetWindow(EditorGUICurveField); window.position = Rect(0,0,400,199); window.Show(); }
function OnGUI() { curveX = EditorGUI.CurveField( Rect(3,3,position.width-6,50), "Animation on X", curveX); curveY = EditorGUI.CurveField( Rect(3,56,position.width-6,50), "Animation on Y", curveY); curveZ = EditorGUI.CurveField( Rect(3,109,position.width-6,50), "Animation on Z", curveZ);
if(GUI.Button(Rect(3,163,position.width-6,30),"Generate Curve")) AddCurveToSelectedGameObject(); }
// A GameObject with the FollowAnimationCurve script must be selected function AddCurveToSelectedGameObject() { if(Selection.activeGameObject) { var comp : FollowAnimationCurve = Selection.activeGameObject.GetComponent.<FollowAnimationCurve>(); comp.SetCurves(curveX, curveY, curveZ); } else { Debug.LogError("No Game Object selected for adding an animation curve"); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class EditorGUICurveField : EditorWindow { AnimationCurve curveX = AnimationCurve.Linear(0, 0, 1, 0); AnimationCurve curveY = AnimationCurve.Linear(0, 0, 1, 1); AnimationCurve curveZ = AnimationCurve.Linear(0, 0, 1, 0);
[MenuItem("Examples/Curve Field demo")] static void Init() { EditorWindow window = GetWindow(typeof(EditorGUICurveField)); window.position = new Rect(0, 0, 400, 199); window.Show(); }
void OnGUI() { curveX = EditorGUI.CurveField( new Rect(3, 3, position.width - 6, 50), "Animation on X", curveX); curveY = EditorGUI.CurveField( new Rect(3, 56, position.width - 6, 50), "Animation on Y", curveY); curveZ = EditorGUI.CurveField( new Rect(3, 109, position.width - 6, 50), "Animation on Z", curveZ);
if (GUI.Button(new Rect(3, 163, position.width - 6, 30), "Generate Curve")) AddCurveToSelectedGameObject(); }
// A GameObject with the FollowAnimationCurve script must be selected void AddCurveToSelectedGameObject() { if (Selection.activeGameObject) { FollowAnimationCurve comp = Selection.activeGameObject.GetComponent<FollowAnimationCurve>(); comp.SetCurves(curveX, curveY, curveZ); } else { Debug.LogError("No Game Object selected for adding an animation curve"); } } }
This is the run-time script which animates the attached GameObject:
#pragma strict
// This script is called by the editor script. // It must not be in the Editor folder. // Also it needs to be called FollowAnimationCurve.js for the // the editor script to call the SetCurves() function.
var curveX : AnimationCurve; var curveY : AnimationCurve; var curveZ : AnimationCurve;
function SetCurves(xC : AnimationCurve, yC : AnimationCurve, zC : AnimationCurve) { curveX = xC; curveY = yC; curveZ = zC; }
function Update() { transform.position = Vector3(curveX.Evaluate(Time.time), curveY.Evaluate(Time.time), curveZ.Evaluate(Time.time)); }
// Note that this must be FollowAnimationCurve.cs
using UnityEngine; using System.Collections;
public class FollowAnimationCurve : MonoBehaviour { public AnimationCurve curveX; public AnimationCurve curveY; public AnimationCurve curveZ;
public void SetCurves(AnimationCurve xC, AnimationCurve yC, AnimationCurve zC) { curveX = xC; curveY = yC; curveZ = zC; }
void Update() { transform.position = new Vector3( curveX.Evaluate(Time.time), curveY.Evaluate(Time.time), curveZ.Evaluate(Time.time)); } }
position | Rectangle on the screen to use for the field. |
property | The curve to edit. |
color | The color to show the curve with. |
ranges | Optional rectangle that the curve is restrained within. |
label | Optional label to display in front of the field. Pass [[GUIContent.none] to hide the label. |
Make a field for editing an AnimationCurve.