void Add(object obj)
        {
            UnitsDeadCondition t = (UnitsDeadCondition)target;

            int id = (int)obj;

            if (!t.DeadCharacters.Contains(id))
            {
                Undo.RegisterCompleteObjectUndo(t, "Modify Condition");
                t.DeadCharacters.Add(id);
            }

            EditorUtility.SetDirty(t);
        }
        public static void ConditionSelector(this Mission mission, string label, ref bool fold, ref MissionCondition cond, ref MissionCondition.ConditionType type)
        {
            bool foldChanged = false;

            EditorGUILayout.BeginHorizontal();

            EditorGUI.BeginChangeCheck();

            fold = EditorGUILayout.Foldout(fold, new GUIContent(label));

            if (EditorGUI.EndChangeCheck())
            {
                foldChanged = true;
            }

            EditorGUI.BeginChangeCheck();
            type = (MissionCondition.ConditionType)EditorGUILayout.EnumPopup(type);
            if (EditorGUI.EndChangeCheck())
            {
                if (cond != null)
                {
                    if (AssetDatabase.GetAssetPath(cond) == AssetDatabase.GetAssetPath(mission))
                    {
                        GameObject.DestroyImmediate(cond, true);
                    }
                    cond = null;
                    fold = false;
                }

                switch (type)
                {
                case MissionCondition.ConditionType.TeamsDead:
                    cond = TeamsDeadCondition.CreateInstance <TeamsDeadCondition>();
                    break;

                case MissionCondition.ConditionType.UnitsDead:
                    cond = UnitsDeadCondition.CreateInstance <UnitsDeadCondition>();
                    break;

                case MissionCondition.ConditionType.UnitsDeadAmount:
                    cond = UnitsAliveCondition.CreateInstance <UnitsAliveCondition>();
                    break;

                case MissionCondition.ConditionType.PointReached:
                    cond = PointReachedCondition.CreateInstance <PointReachedCondition>();
                    break;

                case MissionCondition.ConditionType.Custom:
                    break;

                case MissionCondition.ConditionType.TurnsPassed:
                    cond = TurnsPassedCondition.CreateInstance <TurnsPassedCondition>();
                    break;

                default:
                    break;
                }
                if (cond)
                {
                    fold = true;
                    cond.conditionType = type;
                    cond.name          = cond.GetType().ToString();
                    //Hide the file
                    cond.hideFlags = HideFlags.HideInHierarchy;
                    //Attatch file to the Mission ScriptableObject
                    AssetDatabase.AddObjectToAsset(cond, mission);
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(cond));
                }
                AssetDatabase.SaveAssets();
            }
            EditorGUILayout.EndHorizontal();
            MissionCondition condition = cond;

            if (fold)
            {
                if (cond != null)
                {
                    if (MissionConditionEditor.cachedEditors.ContainsKey(cond) && MissionConditionEditor.cachedEditors[cond] != null)
                    {
                        MissionConditionEditor.cachedEditors[cond].OnInspectorGUI();
                    }
                    else
                    {
                        //Create editor for the condition
                        Editor ConditionEditor = Editor.CreateEditor(condition);

                        //See if the Inpector can be casted to MissionConditionEditor
                        if (ConditionEditor as MissionConditionEditor != null)
                        {
                            //Initialize custom condition editor
                            ((MissionConditionEditor)ConditionEditor).Init(mission);
                        }
                        //Draw the editor
                        ConditionEditor.OnInspectorGUI();
                    }
                }
                else
                {
                    EditorGUI.indentLevel++;
                    cond = EditorGUILayout.ObjectField("Custom Condition", cond, typeof(MissionCondition), false) as MissionCondition;
                    EditorGUI.indentLevel--;
                }
            }

            if (foldChanged && cond != null && MissionConditionEditor.cachedEditors.ContainsKey(cond) && MissionConditionEditor.cachedEditors[cond] != null)
            {
                if (MissionConditionEditor.cachedEditors[cond] as MissionConditionEditor != null)
                {
                    if (fold)
                    {
                        MissionConditionEditor.cachedEditors[cond].Show();
                    }
                    else
                    {
                        MissionConditionEditor.cachedEditors[cond].Hide();
                    }
                }
            }
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            UnitsDeadCondition t = target as UnitsDeadCondition;

            if (missionBeingEdited == null)
            {
                return;
            }
            if (t == null)
            {
                return;
            }
            serializedObject.Update();
            EditorGUI.indentLevel++;
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Dead Characters");
            if (GUILayout.Button("+", GUILayout.Width(25)))
            {
                GenericMenu CreateMenu = new GenericMenu();
                int         _i         = 0;
                for (int i = 0; i < missionBeingEdited.Teams.Count; i++)
                {
                    int _q = 0;
                    for (int q = 0; q < missionBeingEdited.Teams[i].members.Count; q++)
                    {
                        _i = i;
                        _q = q;
                        CreateMenu.AddItem(new GUIContent("Team " + i + "/" + missionBeingEdited.Teams[_i].members[_q]._Name), false, Add, missionBeingEdited.Teams[_i].members[_q].id.ToInt());
                    }
                }
                CreateMenu.ShowAsContext();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUI.indentLevel++;
            for (int i = t.DeadCharacters.Count - 1; i >= 0; i--)
            {
                Character chara = missionBeingEdited.FindCharacter(t.DeadCharacters[i]);
                if (chara != null)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(chara._Name);
                    if (GUILayout.Button("X", GUILayout.Width(25)))
                    {
                        Undo.RegisterCompleteObjectUndo(t, "Modify Condition");
                        t.DeadCharacters.RemoveAt(i);
                    }
                    EditorGUILayout.EndHorizontal();
                }
                else
                {
                    Undo.RegisterCompleteObjectUndo(t, "Modify Condition");
                    t.DeadCharacters.RemoveAt(i);
                }
            }
            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(missionBeingEdited);
            }
        }