relativePath | 通向该曲线应用于的游戏对象的路径。relativePath
已进行了类似于路径名的格式调整,例如“root/spine/leftArm”。如果 relativePath
为空,它指的是将该动画剪辑附加到的游戏对象。 |
type | 该动画组件的类类型。 |
propertyName | 正在生成动画的属性的名称或路径。 |
curve | 动画曲线。 |
分配该曲线,以便为指定属性生成动画。
如果 curve
为 null,则该曲线将被移除。如果
对于该属性已经存在一条曲线,则其将被替换。
注意: SetCurve
将仅在运行时为旧版动画剪辑工作。对于非旧版动画剪辑,
这是一个仅限编辑器的函数。
以下脚本示例显示了如何能够使用
动画剪辑为 GameObject
位置生成动画。使用 SetCurve()
将一条动画曲线设置到了 AnimationClip 上。
该示例将 x 偏移量从 1.0 向下移至 0.0。
可使用 SetCurve API 为各种各样的
参数生成动画。Transform 和 Material 等一些典型组件具有
易于访问的变量。例如,Transform 具有
Transform.localPosition 等变量。可使用 AnimationClip API 为 localPosition
的 x、y 和 z 值
生成动画。查看 Transform 文档,以便
了解这些变量以及了解如何能够为它们生成动画。
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.
// 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); } }
可通过在 Editor 设置中将 Asset Serialization 设置为
Force Text 模式来查找属性名称。使用 Edit > Project Settings > Editor
来启用此模式。之后由编辑器编写的文本文件
将包含这些属性的名称。例如,为场景对象编写的 yaml
文件将包含 Camera 设置。查看此
yaml 文件将显示: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.