public void SetCurve (string relativePath, Type type, string propertyName, AnimationCurve curve);

参数

relativePath通向该曲线应用于的游戏对象的路径。relativePath 已进行了类似于路径名的格式调整,例如“root/spine/leftArm”。如果 relativePath 为空,它指的是将该动画剪辑附加到的游戏对象。
type该动画组件的类类型。
propertyName正在生成动画的属性的名称或路径。
curve动画曲线。

描述

分配该曲线,以便为指定属性生成动画。

如果 curve 为 null,则该曲线将被移除。如果 对于该属性已经存在一条曲线,则其将被替换。

注意: SetCurve 将仅在运行时为旧版动画剪辑工作。对于非旧版动画剪辑, 这是一个仅限编辑器的函数。

以下脚本示例显示了如何能够使用 动画剪辑为 GameObject 位置生成动画。使用 SetCurve() 将一条动画曲线设置到了 AnimationClip 上。 该示例将 x 偏移量从 1.0 向下移至 0.0。

可使用 SetCurve API 为各种各样的 参数生成动画。TransformMaterial 等一些典型组件具有 易于访问的变量。例如,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” 作为属性名称。

另一个示例是访问 Light 设置。scene.unity 文件 (假设某个场景称为 scene)将具有光源颜色的字符串。 脚本可通过访问 m_Color 来访问光源颜色。对于该示例,此场景将需要 一个光源才能工作。

另请参阅:ClearCurves 函数和 AnimationCurve 类。

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961