using UnityEditor;
using UnityEngine;
using System;
using System.Reflection;

public class CustomAnimationClipEditorWindow : EditorWindow
{
    private object animationClipEditorInstance;
    private MethodInfo onInspectorGUIMethod;
    private AnimationClip animationClip;

    [MenuItem("Window/Custom Animation Clip Editor")]
    public static void ShowWindow()
    {
        GetWindow<CustomAnimationClipEditorWindow>("Custom Animation Clip Editor");
    }

    private void OnEnable()
    {
        // ȡ AnimationClipEditor 
        Type animationClipEditorType = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationClipEditor");

        if (animationClipEditorType != null)
        {
            //  AnimationClipEditor ʵ
            animationClipEditorInstance = Activator.CreateInstance(animationClipEditorType);

            // ȡ OnInspectorGUI 
            onInspectorGUIMethod = animationClipEditorType.GetMethod("OnInspectorGUI", BindingFlags.Instance | BindingFlags.NonPublic);
        }
        else
        {
            Debug.LogError("Unable to find AnimationClipEditor type.");
        }
    }

    private void OnGUI()
    {
        // ѡ AnimationClip
        animationClip = (AnimationClip)EditorGUILayout.ObjectField("Animation Clip", animationClip, typeof(AnimationClip), false);

        if (animationClipEditorInstance != null && animationClip != null)
        {
            //  target 
            FieldInfo targetField = animationClipEditorInstance.GetType().GetField("m_Target", BindingFlags.Instance | BindingFlags.NonPublic);
            if (targetField != null)
            {
                targetField.SetValue(animationClipEditorInstance, animationClip);
            }

            //  OnInspectorGUI 
            onInspectorGUIMethod?.Invoke(animationClipEditorInstance, null);
        }
    }
}