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 文档,以便
了解这些变量以及了解如何能够为它们生成动画。
Material 类还链接
到可生成动画的变量。这些来自用于
渲染的着色器。使用材质下拉选单中的“Edit Shader...”选项显示
可生成动画的所有参数。可生成动画的参数均以
下划线开头,因此可为例如颜色 (_Color
) 和比例 (_BumpScale
)
生成动画。
要索引到同一渲染器上的多个材质中,您可以为属性添加前缀,
例如:\
“[1]._MainTex.offset.y”
。
以下示例脚本显示如何能够
同时通过两种方式为游戏对象生成动画。在该示例中为游戏对象的位置生成了动画,
并且还随时间更改了材质颜色。
// 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
这显示 FOV 参数的名称为“field of view”。如果您要
创建一个动画剪辑,以便为摄像机可视角度生成动画,您将传输“field of view”
作为 propertyName。
另一个示例是访问 Light
设置。scene.unity
文件
(假设某个场景称为 scene
)将具有光源颜色的字符串。
脚本可通过访问 m_Color
来访问光源颜色。对于该示例,此场景将需要
一个光源才能工作。
另请参阅:ClearCurves 函数和 AnimationCurve 类。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.