AnimationWindow 使用 AnimationMode 来存储 由 AnimationClip 播放修改的属性。
当退出 AnimationMode 时,所有属性都将 还原为其默认状态。动画属性也会在 Inspector 中突出显示。使用 StartAnimationMode 进入动画模式。在动画模式中, 编辑器会着色以显示它正在动画化。属性可以通过 SampleAnimationClip 进行动画化。\ \ 以下脚本示例显示了如何对游戏对象进行动画化。AnimationMode 具有 支持此功能的函数。演示可从“Examples/AnimationMode demo” 菜单启动。此演示开始后,它需要在 Scene 窗口中选择一个游戏对象。 (此示例要求不在 Game 视图中运行游戏。)选择 游戏对象后,示例窗口将会更改。为所选游戏对象 选择一个已创建的动画剪辑。在动画剪辑加载完成后, 动画便可应用于游戏对象。单击 Animate 按钮,将滑动条添加到 窗口。使用滑动条会将动画应用于所选的游戏对象。Lock 按钮可防止动画应用于其他游戏对象。
using UnityEngine; using UnityEditor;
public class ExampleClass : EditorWindow { protected GameObject go; protected AnimationClip animationClip; protected float time = 0.0f; protected bool lockSelection = false; protected bool animationMode = false;
[MenuItem("Examples/AnimationMode demo", false, 2000)] public static void DoWindow() { var window = GetWindowWithRect<ExampleClass>(new Rect(0, 0, 300, 80)); window.Show(); }
// Has a GameObject been selection? public void OnSelectionChange() { if (!lockSelection) { go = Selection.activeGameObject; Repaint(); } }
// Main editor window public void OnGUI() { // Wait for user to select a GameObject if (go == null) { EditorGUILayout.HelpBox("Please select a GameObject", MessageType.Info); return; }
// Animate and Lock buttons. Check if Animate has changed GUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); GUILayout.Toggle(AnimationMode.InAnimationMode(), "Animate"); if (EditorGUI.EndChangeCheck()) ToggleAnimationMode();
GUILayout.FlexibleSpace(); lockSelection = GUILayout.Toggle(lockSelection, "Lock"); GUILayout.EndHorizontal();
// Slider to use when Animate has been ticked EditorGUILayout.BeginVertical(); animationClip = EditorGUILayout.ObjectField(animationClip, typeof(AnimationClip), false) as AnimationClip; if (animationClip != null) { float startTime = 0.0f; float stopTime = animationClip.length; time = EditorGUILayout.Slider(time, startTime, stopTime); } else if (AnimationMode.InAnimationMode()) AnimationMode.StopAnimationMode(); EditorGUILayout.EndVertical(); }
void Update() { if (go == null) return;
if (animationClip == null) return;
// There is a bug in AnimationMode.SampleAnimationClip which crashes // Unity if there is no valid controller attached Animator animator = go.GetComponent<Animator>(); if (animator != null && animator.runtimeAnimatorController == null) return;
// Animate the GameObject if (!EditorApplication.isPlaying && AnimationMode.InAnimationMode()) { AnimationMode.BeginSampling(); AnimationMode.SampleAnimationClip(go, animationClip, time); AnimationMode.EndSampling();
SceneView.RepaintAll(); } }
void ToggleAnimationMode() { if (AnimationMode.InAnimationMode()) AnimationMode.StopAnimationMode(); else AnimationMode.StartAnimationMode(); } }
animatedPropertyColor | 用于表明属性当前正在动画化的颜色。 |
candidatePropertyColor | 用于表明动画属性已被修改的颜色。 |
recordedPropertyColor | 用于表明动画属性会自动记录动画剪辑中的更改的颜色。 |
AddEditorCurveBinding | 将 EditorCurveBinding 定义的属性标记为当前正在动画化。 |
AddPropertyModification | 将属性标记为当前正在动画化。 |
BeginSampling | 初始化动画剪辑采样的开头。 |
EndSampling | 完成动画剪辑的采样。 |
InAnimationMode | 当前是否处于 AnimationMode? |
IsPropertyAnimated | 指定的属性当前是否处于动画模式且要进行动画化? |
SampleAnimationClip | 对对象上的 AnimationClip进行采样,并记录 AnimationMode 中任何经过修改的属性。 |
StartAnimationMode | 启动动画模式。 |
StopAnimationMode | 停止动画模式,还原在动画模式下动画化的所有属性。 |
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.