Version: 5.6

AnimationMode

class in UnityEditor

切换到手册

描述

AnimationWindow 使用 AnimationMode 来存储 由 AnimationClip 播放修改的属性。

When exiting AnimationMode all properties are reverted to their default state. Animated properties are also highlighted by the inspector. Use StartAnimationMode to enter animation mode. In Animation mode the editor is tinted to show that it is animating. Properties can be animated via SampleAnimationClip.

The following script example shows how a GameObject can be animated. AnimationMode has functions which support this. The demo can be launched from the "Examples/AnimationMode demo" menu. Once this demo starts it requires a GameObject to be selected in the Scene window. (This example requires the game to not be running in the Game view.) After a GameObject has been selected the example window will change. Choose a created animation clip for the chosen GameObject. Once the animation clip has been loaded the animation can be applied to the GameObject. Clicking the Animate button adds a slider to the window. Using the slider will apply the animation to the selected GameObject. The Lock button prevents the animation to be applied to a different GameObject.

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 &amp;&amp; animator.runtimeAnimatorController == null) return;

// Animate the GameObject if (!EditorApplication.isPlaying &amp;&amp; AnimationMode.InAnimationMode()) { AnimationMode.BeginSampling(); AnimationMode.SampleAnimationClip(go, animationClip, time); AnimationMode.EndSampling();

SceneView.RepaintAll(); } }

void ToggleAnimationMode() { if (AnimationMode.InAnimationMode()) AnimationMode.StopAnimationMode(); else AnimationMode.StartAnimationMode(); } }

静态变量

animatedPropertyColor用于表明属性当前正在动画化的颜色。

静态函数

AddPropertyModification将属性标记为当前正在动画化。
BeginSampling初始化动画剪辑采样的开头。
EndSampling完成动画剪辑的采样。
InAnimationMode当前是否处于 AnimationMode?
IsPropertyAnimated指定的属性当前是否处于动画模式且要进行动画化?
SampleAnimationClip对对象上的 AnimationClip进行采样,并记录 AnimationMode 中任何经过修改的属性。
StartAnimationMode启动动画模式。
StopAnimationMode停止动画模式,还原在动画模式下动画化的所有属性。
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961