Unity编辑器拓展Scene下播放动画

在这里插入图片描述

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

[RequireComponent(typeof(Animator))] //这个脚本作用就是为了加载游戏模型上
public class PlayAnimInScene : MonoBehaviour //什么都没有,方便继承
{
}

#if UNITY_EDITOR
[CustomEditor(typeof(PlayAnimInScene),true)] 
public class AnimUtility:Editor //这里可以override onInspector
{
    PlayAnimInScene _Script;
    private AnimationClip[] clips;
    private float len;
    private int curIndex;
    private void OnEnable()
    {
        _Script = target as PlayAnimInScene;
        Animator anim = _Script.GetComponent<Animator>();
        clips = anim.runtimeAnimatorController.animationClips;
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        //从这里开始检查 
        EditorGUI.BeginChangeCheck();
        GUILayout.Label("当前剪辑:");
        curIndex = EditorGUILayout.Popup(curIndex, clips.Select(p => p.name).ToArray()); //还原clip状态
        GUILayout.Label("播放长度:");
        len = EditorGUILayout.Slider(len, 0, 1);
        if(EditorGUI.EndChangeCheck())
        {
            AnimationClip clip = clips[curIndex];
            clip.SampleAnimation(_Script.gameObject, clip.length * len);
        }
    }
}
#endif
发布了67 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Icecoldless/article/details/103744134