Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

Motion

class in UnityEngine

/

Inherits from:Object

/

Implemented in:UnityEngine.AnimationModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Stores a reference to an animation asset associated with a State Machine state.

The Motion class acts as an abstraction for APIs that accept either the AnimationClip or BlendTree animation classes.

Additional resources: AnimatorState.motion, AnimatorController.AddMotion, BlendTree.AddChild.

This example demonstrates how to create a BlendTree from a selection of AnimationClips. This example also demonstrates how a BlendTree is composed of two or more child Motions, and how to use a BlendTree to instantiate an AnimatorState.

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

static class MotionClassExample
{
    [MenuItem("Example/Create Animator Controller From Selected Clips")]
    static void CreateAnimatorControllerFromClips()
    {
        // Get the Motion-derived assets in the selection. You can only do this with AnimationClip assets.
        var motions = new List<Motion>();
        foreach (var obj in Selection.objects)
        {
            if (obj is Motion clip)
            {
                motions.Add(clip);
            }
        }

        if (motions.Count == 0)
        {
            Debug.LogWarning("No clips selected");
            return;
        }

        // Create an AnimationController in the project.
        var animatorController =
            AnimatorController.CreateAnimatorControllerAtPath(
                AssetDatabase.GenerateUniqueAssetPath("Assets/MotionExample.controller"));

        // Create a BlendTree that blends between the selected Motion objects.
        var blendTree = new BlendTree { name = "Default" };
        for (var i = 0; i < motions.Count; i++)
        {
            var clip = motions[i];
            blendTree.AddChild(clip, (float)i / motions.Count);
        }

        // Create a new state from the BlendTree (which is itself a Motion).
        animatorController.AddMotion(blendTree);

        // Make sure the BlendTree is persisted.
        AssetDatabase.AddObjectToAsset(blendTree, animatorController);

        // Show the new  AnimatorController in the project window.
        Selection.activeObject = animatorController;
        EditorGUIUtility.PingObject(animatorController);
    }
}

Inherited Members

Properties

hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameThe name of the object.

Public Methods

GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static Methods

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
InstantiateClones the object original and returns the clone.
InstantiateAsyncCaptures a snapshot of the original object (that must be related to some GameObject) and returns the AsyncInstantiateOperation.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.