public void SaveData()
    {
        MecanimEventData targetData = target as MecanimEventData;

        Undo.RecordObject(target, "Mecanim Event Data");

        List <MecanimEventDataEntry> entries = new List <MecanimEventDataEntry>();

        foreach (UnityEditor.Animations.AnimatorController controller in data.Keys)
        {
            foreach (int layer in data[controller].Keys)
            {
                foreach (int stateNameHash in data[controller][layer].Keys)
                {
                    if (data[controller][layer][stateNameHash].Count == 0)
                    {
                        continue;
                    }

                    if (!IsValidState(controller.GetInstanceID(), layer, stateNameHash))
                    {
                        continue;
                    }

                    MecanimEventDataEntry entry = new MecanimEventDataEntry();
                    entry.animatorController = controller;
                    entry.layer         = layer;
                    entry.stateNameHash = stateNameHash;
                    entry.events        = data[controller][layer][stateNameHash].ToArray();;

                    entries.Add(entry);
                }
            }
        }

        targetData.data = entries.ToArray();

        EditorUtility.SetDirty(target);
    }