Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AnimationClip.SetCurve

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

Switch to Manual
public method SetCurve(relativePath: string, type: Type, propertyName: string, curve: AnimationCurve): void;
public void SetCurve(string relativePath, Type type, string propertyName, AnimationCurve curve);

Parameters

relativePath Path to the game object this curve applies to. The relativePath is formatted similar to a pathname, e.g. "root/spine/leftArm". If relativePath is empty it refers to the game object the animation clip is attached to.
type The class type of the component that is animated.
propertyName The name or path to the property being animated.
curve The animation curve.

Description

Assigns the curve to animate a specific property.

If curve is null the curve will be removed. If a curve already exists for that property, it will be replaced.

Note: SetCurve will only work at runtime for legacy animation clips. For non-legacy AnimationClips it is an editor-only function.

The following script example shows how a GameObject position can be animated using an animation clip. An animated curve is set onto the AnimationClip using SetCurve(). This example moves the x offset from 1.0 down to 0.0.

var anim: Animation;

function Start() { anim = GetComponent.<Animation>();

// Animates the x coordinate of a transform position. // Create the curve, starting at 1.0 and falling to 0.0 over 2 seconds var curve : AnimationCurve = AnimationCurve.Linear(0.0, 1.0, 2.0, 0.0);

// Create the clip with the curve. var clip : AnimationClip = new AnimationClip(); clip.legacy = true; clip.SetCurve("", Transform, "localPosition.x", curve);

// Add and play the clip anim.AddClip(clip, "test"); anim.Play("test"); } @script RequireComponent(Animation)
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animation))] public class ExampleClass : MonoBehaviour { public Animation anim; void Start() { anim = GetComponent<Animation>(); AnimationCurve curve = AnimationCurve.Linear(0.0F, 1.0F, 2.0F, 0.0F); AnimationClip clip = new AnimationClip(); clip.legacy = true; clip.SetCurve("", typeof(Transform), "localPosition.x", curve); anim.AddClip(clip, "test"); anim.Play("test"); } }

The SetCurve API can be used to animate a large variety of parameters. Some typical components such as Transform and Material have easy to access variables. For example the Transform has variables such as Transform.localPosition. The x, y, and z values of the localPosition can be animated using the AnimationClip API. View the Transform documentation to see the variables and how they can be animated.

The Material class also links to variables that can be animated. These come from the shader that is used for rendering. Using the "Edit Shader..." option from the material drop down shows all the parameters that can be animated. The animatable parameters all start with underscore, so, for example color (_Color) and scale (_BumpScale) can be animated.

To index into multiple materials on the same renderer you can prefix the attribute like this:
"[1]._MainTex.offset.y".

The example script below shows how a GameObject can be animated in two ways at the same time. In this example, the position of the GameObject is animated, and the Material color is also changed over time.

#pragma strict
// This script example shows how SetCurve() can be used
// Animate the position and color of the GameObject
public function Start() {
	var anim: Animation = GetComponent.<Animation>();
	var curve: AnimationCurve;
	// create a new AnimationClip
	var clip: AnimationClip = new AnimationClip();
	clip.legacy = true;
	// create a curve to move the GameObject and assign to the clip
	var keys: Keyframe[];
	keys = new Keyframe[3];
	keys[0] = new Keyframe(0.0f, 0.0f);
	keys[1] = new Keyframe(1.0f, 1.5f);
	keys[2] = new Keyframe(2.0f, 0.0f);
	curve = new AnimationCurve(keys);
	clip.SetCurve("", Transform, "localPosition.x", curve);
	// update the clip to a change the red color
	curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);
	clip.SetCurve("", Material, "_Color.r", curve);
	// now animate the GameObject
	anim.AddClip(clip, clip.name);
	anim.Play(clip.name);
}
// This script example shows how SetCurve() can be used
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { // Animate the position and color of the GameObject public void Start() { Animation anim = GetComponent<Animation>(); AnimationCurve curve;

// create a new AnimationClip AnimationClip clip = new AnimationClip(); clip.legacy = true;

// create a curve to move the GameObject and assign to the clip Keyframe[] keys; keys = new Keyframe[3]; keys[0] = new Keyframe(0.0f, 0.0f); keys[1] = new Keyframe(1.0f, 1.5f); keys[2] = new Keyframe(2.0f, 0.0f); curve = new AnimationCurve(keys); clip.SetCurve("", typeof(Transform), "localPosition.x", curve);

// update the clip to a change the red color curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f); clip.SetCurve("", typeof(Material), "_Color.r", curve);

// now animate the GameObject anim.AddClip(clip, clip.name); anim.Play(clip.name); } }

Property names can be located by setting Asset Serialization to Force Text mode in the Editor settings. Use Edit->Project Settings->Editor to enable this mode. The text files that are then written by the editor will include the names of the properties. For example, the yaml file written for a Scene object will include the Camera settings. Looking at this yaml file will show:

m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: .300000012
far clip plane: 1000
field of view: 60
orthographic: 0
orthographic size: 5
m_Depth: -1

This shows that the name for the FOV parameter is "field of view". If you wanted to create an animation clip to animate the camera field of view, you would pass "field of view" as the propertyName.

Another example is the access of Light settings. The scene.unity file (assuming a scene called scene) will have a string for the light color. Script can access the light color by accessing m_Color. The scene will need to have a light for this example to work.

See Also: ClearCurves function, and the AnimationCurve class.

Did you find this page useful? Please give it a rating: