Version: 2022.3
public static AnimationCurve CurveField (Rect position, AnimationCurve value);
public static AnimationCurve CurveField (Rect position, string label, AnimationCurve value);
public static AnimationCurve CurveField (Rect position, GUIContent label, AnimationCurve value);
public static AnimationCurve CurveField (Rect position, AnimationCurve value, Color color, Rect ranges);
public static AnimationCurve CurveField (Rect position, string label, AnimationCurve value, Color color, Rect ranges);
public static AnimationCurve CurveField (Rect position, GUIContent label, AnimationCurve value, Color color, Rect ranges);

参数

position 屏幕上用于字段的矩形。
label (可选)显示在字段前的标签。
value 要编辑的曲线。
color 显示曲线时使用的颜色。
ranges 约束曲线范围的可选矩形。

返回

AnimationCurve 用户编辑的曲线。

描述

生成一个用于编辑 AnimationCurve 的字段。


编辑器窗口中的 Curve 字段。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class EditorGUICurveField : EditorWindow { AnimationCurve curveX = AnimationCurve.Linear(0, 0, 1, 0); AnimationCurve curveY = AnimationCurve.Linear(0, 0, 1, 1); AnimationCurve curveZ = AnimationCurve.Linear(0, 0, 1, 0);

[MenuItem("Examples/Curve Field demo")] static void Init() { EditorWindow window = GetWindow(typeof(EditorGUICurveField)); window.position = new Rect(0, 0, 400, 199); window.Show(); }

void OnGUI() { curveX = EditorGUI.CurveField( new Rect(3, 3, position.width - 6, 50), "Animation on X", curveX); curveY = EditorGUI.CurveField( new Rect(3, 56, position.width - 6, 50), "Animation on Y", curveY); curveZ = EditorGUI.CurveField( new Rect(3, 109, position.width - 6, 50), "Animation on Z", curveZ);

if (GUI.Button(new Rect(3, 163, position.width - 6, 30), "Generate Curve")) AddCurveToSelectedGameObject(); }

// A GameObject with the FollowAnimationCurve script must be selected void AddCurveToSelectedGameObject() { if (Selection.activeGameObject) { FollowAnimationCurve comp = Selection.activeGameObject.GetComponent<FollowAnimationCurve>(); comp.SetCurves(curveX, curveY, curveZ); } else { Debug.LogError("No Game Object selected for adding an animation curve"); } } }

这是可对附加游戏对象 进行动画化的运行时脚本:

// Note that this must be FollowAnimationCurve.cs

using UnityEngine; using System.Collections;

public class FollowAnimationCurve : MonoBehaviour { public AnimationCurve curveX; public AnimationCurve curveY; public AnimationCurve curveZ;

public void SetCurves(AnimationCurve xC, AnimationCurve yC, AnimationCurve zC) { curveX = xC; curveY = yC; curveZ = zC; }

void Update() { transform.position = new Vector3( curveX.Evaluate(Time.time), curveY.Evaluate(Time.time), curveZ.Evaluate(Time.time)); } }

public static void CurveField (Rect position, SerializedProperty property, Color color, Rect ranges);
public static void CurveField (Rect position, SerializedProperty property, Color color, Rect ranges, GUIContent label);

参数

position 屏幕上用于字段的矩形。
property 要编辑的曲线。
color 显示曲线时使用的颜色。
ranges 约束曲线范围的可选矩形。
label (可选)显示在字段前的标签。传递 [[GUIContent.none] 以隐藏标签。

描述

生成一个用于编辑 AnimationCurve 的字段。