LanguageEnglish
  • C#

ObjectReferenceKeyframe

struct in UnityEditor

Description

Keyframe to an Object reference.

Use an object reference keyframe to create an array of Object references. An AnimationClip uses an array of Object references to animate properties.

Use the AnimationUtility.SetObjectReferenceCurve and AnimationUtility.SetObjectReferenceCurves methods to assign ObjectReferenceKeyframe arrays of Object reference properties to an AnimationClip. Use the AnimationUtility.GetObjectReferenceCurve method to retrieve an ObjectReferenceKeyframe array for an Object reference property.

Additional resources: AnimationClip AnimationUtility.SetObjectReferenceCurve AnimationUtility.SetObjectReferenceCurves AnimationUtility.GetObjectReferenceCurve.

using UnityEditor;
using UnityEngine;

static class AnimationClipWithObjectReferenceKeyframesExample
{
    // This example creates an AnimationClip of a sequence of sprites selected
    // in the project view. The clip is saved as an asset in the project.
    [MenuItem("Example/Create Flip Book Animation Clip From Sprites")]
    static void CreateFlipBookAnimationClipFromSprites()
    {
        var selectedSprites = Selection.GetFiltered<Sprite>(SelectionMode.Unfiltered);
        if (selectedSprites == null || selectedSprites.Length == 0)
        {
            Debug.LogError("Please select sprites in the project view to create a clip for.");
            return;
        }

        AnimationClip clip = new AnimationClip();

        var spriteCurve = new ObjectReferenceKeyframe[selectedSprites.Length];
        for (int i = 0; i < selectedSprites.Length; ++i)
        {
            var sprite = selectedSprites[i];
            spriteCurve[i] = new ObjectReferenceKeyframe
            {
                time = i/clip.frameRate,
                value = sprite
            };
        }

        var spriteBinding = EditorCurveBinding.PPtrCurve("", typeof(SpriteRenderer), "m_Sprite");
        AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, spriteCurve);

        AssetDatabase.CreateAsset(clip, AssetDatabase.GenerateUniqueAssetPath($"Assets/FlipBookClip.anim"));
    }
}

Properties

timeThe time of the keyframe.
valueThe Object reference value of the keyframe.

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961