void DestroyAnimationInspector()
 {
     if (mEditor_SelectedAnim != null)
     {
         mEditor_SelectedAnim.OnDestroy();
     }
     mEditor_SelectedAnim = null;
 }
        public void OnEnable()
        {
            mProp_SerializedData = serializedObject.FindProperty("mSerializedData");

            mInspector = new SE_Animation_Inspector((target as SE_AnimationPreset).CreateAnimation(), null);
        }
        public void OnGUI_AnimationSlot()
        {
            var prop_Slot          = mSerialized_AnimationSlots.GetArrayElementAtIndex(mAnimationsReorderableList.index);
            var prop_SlotPreset    = prop_Slot.FindPropertyRelative("_Preset");
            var prop_SlotLocalData = prop_Slot.FindPropertyRelative("_LocalSerializedData");

            if (mEditor_SelectedAnim == null || iCurrentAnimationSlot != mAnimationsReorderableList.index)
            {
                iCurrentAnimationSlot = mAnimationsReorderableList.index;

                if (prop_SlotPreset.objectReferenceValue != null)
                {
                    mSerializedObject_SelectedAnim = new SerializedObject(prop_SlotPreset.objectReferenceValue);
                }
                else
                {
                    mSerializedObject_SelectedAnim = null;
                }

                DestroyAnimationInspector();

                mEditor_SelectedAnim = new SE_Animation_Inspector(mTarget._AnimationSlots[iCurrentAnimationSlot]._Animation /*CreateAnimation()*/, mTarget);
            }

            if (prop_SlotPreset.objectReferenceValue != null && mSerializedObject_SelectedAnim != null)
            {
                                #if UNITY_5_6_OR_NEWER
                mSerializedObject_SelectedAnim.UpdateIfRequiredOrScript();
                                #else
                mSerializedObject_SelectedAnim.UpdateIfDirtyOrScript();
                                #endif
            }

            GUILayout.Space(10);


            //GUITools.DrawHeader("Animation", true);
            var boxArea = new GUIStyle("Box");
            boxArea.overflow = new RectOffset(2, 2, 0, 0);
            bool deselectAnimation = false;
            GUILayout.BeginHorizontal(boxArea);
            GUILayout.Toggle(true, "Animation", EditorStyles.foldout);
            GUILayout.FlexibleSpace();
            deselectAnimation = GUILayout.Button("X", EditorStyles.miniButton);
            GUILayout.EndHorizontal();

            GUILayout.Space(-4);


            GUI.backgroundColor = GUITools.DarkGray;
            GUILayout.BeginVertical(/*EditorStyles.textArea, */ GUILayout.Height(1));
            GUI.backgroundColor = Color.white;

            EditorGUI.BeginChangeCheck();
            mEditor_SelectedAnim.OnGUI_Animation();

            if (EditorGUI.EndChangeCheck() || SE_AnimationPreset_Inspector.mDirty)
            {
                SE_AnimationPreset_Inspector.mDirty = false;

                var data = SE_Animation.SaveSerializedData(mEditor_SelectedAnim.mAnimation);
                if (prop_SlotLocalData.stringValue != data)
                {
                    prop_SlotLocalData.stringValue = data;
                    //Debug.Log(data);
                }

                if (prop_SlotPreset.objectReferenceValue != null)
                {
                    var prop_PresetData = mSerializedObject_SelectedAnim.FindProperty("mSerializedData");
                    if (prop_PresetData.stringValue != data)
                    {
                        prop_PresetData.stringValue = data;
                        mSerializedObject_SelectedAnim.ApplyModifiedProperties();
                        //mTarget._AnimationSlots[iCurrentAnimationSlot].CreateAnimation();
                    }
                }
            }
            if (mEditor_SelectedAnim.mAnimation.IsPlaying)
            {
                mMakeMaterialDirty = mMakeVerticesDirty = true;
            }


            GUITools.CloseHeader();

            if (deselectAnimation)
            {
                DeselectAnimation();
            }
        }