UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
AnimationClip.SetCurve
# [AnimationClip](AnimationClip.html).SetCurve public void **SetCurve** (string **relativePath**, Type **type**, string **propertyName**, [AnimationCurve](AnimationCurve.html) **curve**); ## 参数 | relativePath | 通向该曲线应用于的游戏对象的路径。`relativePath` 已进行了类似于路径名的格式调整,例如“root/spine/leftArm”。如果 `relativePath` 为空,它指的是将该动画剪辑附加到的游戏对象。 | | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | type | 该动画组件的类类型。 | | propertyName | 正在生成动画的属性的名称或路径。 | | curve | 动画曲线。 | ## 描述 分配该曲线,以便为指定属性生成动画。 如果 `curve` 为 null,则该曲线将被移除。如果 对于该属性已经存在一条曲线,则其将被替换。 **注意:** `SetCurve` 将仅在运行时为旧版动画剪辑工作。对于非旧版动画剪辑, 这是一个仅限编辑器的函数。 以下脚本示例显示了如何能够使用 动画剪辑为 `GameObject` 位置生成动画。使用 `SetCurve()` 将一条动画曲线设置到了 [AnimationClip](AnimationClip.html) 上。 该示例将 x 偏移量从 1.0 向下移至 0.0。 可使用 [SetCurve](AnimationClip.SetCurve.html) API 为各种各样的 参数生成动画。[Transform](Transform.html) 和 [Material](Material.html) 等一些典型组件具有 易于访问的变量。例如,[Transform](Transform.html) 具有 [Transform.localPosition](Transform-localPosition.html) 等变量。可使用 [AnimationClip](AnimationClip.html) API 为 `localPosition` 的 x、y 和 z 值 生成动画。查看 [Transform](Transform.html) 文档,以便 了解这些变量以及了解如何能够为它们生成动画。 [Material](Material.html) 类还链接 到可生成动画的变量。这些来自用于 渲染的着色器。使用材质下拉选单中的“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](AnimationClip.ClearCurves.html) 函数和 [AnimationCurve](AnimationCurve.html) 类。
da
2022年5月23日 22:43
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码