Version: 5.4 beta (switch to 5.3)
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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again 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 function 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. relativePath is formatted similar to a pathname, e.g. "rootrelativePathleftArm". 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 AnimationClips. For Non-Legacy AnimationClips it is an editor-only function.

Common names are: "localPosition.x", "localPosition.y", "localPosition.z", "localRotation.x", "localRotation.y", "localRotation.z", "localRotation.w" "localScale.x", "localScale.y", "localScale.z".

var anim: Animation;

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

// Animates the x coordinate of a transform position. // Create the curve. var curve : AnimationCurve = AnimationCurve.Linear(0, 1, 2, 3);

// 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, 1, 2, 3); AnimationClip clip = new AnimationClip(); clip.legacy = true; clip.SetCurve("", typeof(Transform), "localPosition.x", curve); anim.AddClip(clip, "test"); anim.Play("test"); } }

Material properties can be animated using the property name exported in the shader. Common property names are: "_MainTex", "_BumpMap", "_Color", "_SpecColor", "_Emission". How to animate different material property types:

Float properties: "PropertyName"

Vector4 properties: "PropertyName.x", "PropertyName.y", "PropertyName.z", "PropertyName.w"

Color properties: "PropertyName.r", "PropertyName.g", "PropertyName.b", "PropertyName.a"

UV Rotation properties: "PropertyName.rotation"

UV Offset and scale: "PropertyName.offset.x", "PropertyName.offset.y", "PropertyName.scale.x", "PropertyName.scale.y"

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

See Also: ClearCurves function, AnimationCurve class.

var anim: Animation;

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

// Animate color's alpha and main texture's horizontal offset. var clip = new AnimationClip (); clip.legacy = true; clip.SetCurve ("", Material, "_Color.a", AnimationCurve (Keyframe(0, 0, 0, 0), Keyframe(1, 1, 0, 0))); clip.SetCurve ("", Material, "_MainTex.offset.x", AnimationCurve.Linear(0, 1, 2, 3)); anim.AddClip (clip, clip.name); anim.Play(clip.name); } @script RequireComponent(Animation)
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animation))] public class ExampleClass : MonoBehaviour { public Animation anim; void Start() { anim = GetComponent<Animation>(); AnimationClip clip = new AnimationClip(); clip.legacy = true; clip.SetCurve("", typeof(Material), "_Color.a", new AnimationCurve(new Keyframe(0, 0, 0, 0), new Keyframe(1, 1, 0, 0))); clip.SetCurve("", typeof(Material), "_MainTex.offset.x", AnimationCurve.Linear(0, 1, 2, 3)); anim.AddClip(clip, clip.name); anim.Play(clip.name); } }

Property names can be looked up by setting Asset Serialization to Force Text mode in the Editor settings. 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.