SetCurve(string, Type, string, AnimationCurve)
Assigns the curve to animate a specific property.
Read time 4 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.AnimationModule
public void SetCurve(string relativePath, Type type, string propertyName, AnimationCurve curve)
Parameters
Path to the game object this curve applies to. The is formatted similar to a pathname, e.g. "root/spine/leftArm". If is empty it refers to the game object the animation clip is attached to.
relativePathrelativePathThe class type of the component that is animated.
The name or path to the property being animated.
The animation curve.
Remarks
If is null the curve will be removed. If a curve already exists for that property, it will be replaced.
curveNote: will only work at runtime for legacy animation clips. For non-legacy AnimationClips it is an editor-only function.
SetCurveThe following script example shows how a position can be animated using an animation clip. An animated curve is set onto the AnimationClip using . This example moves the x offset from 1.0 down to 0.0.
GameObjectSetCurve()The AnimationClip.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 can be animated using the AnimationClip API. View the Transform documentation to see the variables and how they can be animated.
localPositionThe 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 material parameters are animated through the Renderer class that references them. All animatable material parameters start with the prefix followed by the property name starting with an underscore. For example, color () and scale () can be animated.
materialmaterial._Colormaterial._BumpScaleThe 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.
// This script example shows how SetCurve() can be usedusing 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(Renderer), "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 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:
Edit->Project Settings->Editorm_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438}m_NormalizedViewPortRect: serializedVersion: 2 x: 0 y: 0 width: 1 height: 1near clip plane: .300000012far clip plane: 1000field of view: 60orthographic: 0orthographic size: 5m_Depth: -1This 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 settings. The file (assuming a Scene called ) will have a string for the light color. Script can access the light color by accessing . The Scene will need to have a light for this example to work.
Lightscene.unityscenem_ColorAdditional Resources: AnimationClip.ClearCurves function, and the AnimationCurve class.