public override void OnInspectorGUI()
    {
        PersistentDataSystem persistentData = ((PersistentDataSystem)target);

        persistentData.dataVersion = EditorGUILayout.TextField("DataVersion", persistentData.dataVersion);
        persistentData.autoSave    = EditorGUILayout.Toggle("AutoSave", persistentData.autoSave);

        persistentData.saveMode = (PersistentDataSystem.SaveMode)(EditorGUILayout.EnumPopup("SaveMode", persistentData.saveMode));

        EditorGUILayout.Space();
        PersistentDataSystem.LoadMode persistentDataSystemLoadMode = ((PersistentDataSystem.LoadMode)(serializedObject.FindProperty("loadAwakeMode").enumValueIndex));

        serializedObject.FindProperty("loadAwakeMode").enumValueIndex = (int)(PersistentDataSystem.LoadMode)(EditorGUILayout.EnumPopup("LoadAwakeMode", persistentDataSystemLoadMode));

        if (persistentDataSystemLoadMode == PersistentDataSystem.LoadMode.SPECIFIC_CLASS)
        {
            persistentData.classToLoad = (string[])EditorUtils.GenericField("Class to load", persistentData.classToLoad, typeof(string[]));
        }
        EditorGUILayout.Space();

        if (!persistentData.IsInit)
        {
            persistentData.Init();
        }

        GUILayout.Space(10);

        if (persistentData.savedDataDictionnary != null)
        {
            foreach (List <SavedData> sdList in persistentData.savedDataDictionnary.Values)
            {
                if (sdList != null && sdList.Count > 0)
                {
                    GUILayout.BeginVertical(EditorStyles.textArea);
                    GUILayout.Space(2);
                    GUIStyle option = new GUIStyle();
                    option.alignment = TextAnchor.MiddleCenter;
                    option.fontSize  = 15;
                    option.fontStyle = FontStyle.Bold;
                    GUILayout.Label(sdList[0].GetType().Name, option);
                    GUILayout.Space(5);

                    foreach (SavedData sd in sdList)
                    {
                        sd.DisplayInspector();
                        GUILayout.Space(2);
                    }

                    GUILayout.EndVertical();
                    GUILayout.Space(2);
                }
            }

            if (persistentData.savedDataDictionnary.Count > 0 && GUILayout.Button("Save Data"))
            {
                persistentData.SaveAllData();
                Debug.Log("Data Saved in the Directory : " + persistentData.AutomaticSavedDataDirectoryPath);
            }
        }

        GUILayout.Space(2);
        if (persistentDataSystemLoadMode == PersistentDataSystem.LoadMode.SPECIFIC_CLASS && GUILayout.Button("Load specific class data"))
        {
            persistentData.LoadClass(persistentData.classToLoad);
        }

        GUILayout.Space(2);
        if (GUILayout.Button("Load all saved data"))
        {
            persistentData.LoadAllSavedData();
        }

        GUILayout.Space(2);
        if (GUILayout.Button("Unload saved data"))
        {
            persistentData.UnloadAllSavedData();
        }

        GUILayout.Space(2);
        if (GUILayout.Button("Erase all saved data"))
        {
            persistentData.EraseAllSavedData();
        }

        serializedObject.ApplyModifiedProperties();
    }
Exemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            EditorGUI.BeginChangeCheck();
            PersistentDataSystem persistentData = ((PersistentDataSystem)target);

            EditorGUILayout.PropertyField(serializedObject.FindProperty("dataVersion"), true);

            GUILayout.Space(5);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("autoSave"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("saveMode"), true);

            EditorGUILayout.Space();

            EditorGUILayout.PropertyField(serializedObject.FindProperty("awakeLoadMode"), true);


            if (persistentData.awakeLoadMode == PersistentDataSystem.AwakeLoadMode.SPECIFIC_CLASS)
            {
                //FIND ALL SAVEDDATA BY REFLECTION
                List <string> options = new List <string>();

                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (type.IsSubclassOf(typeof(SavedData)))
                        {
                            options.Add(type.FullName);
                        }
                    }
                }

                foldout = EditorGUILayout.Foldout(foldout, "Class To Load");

                if (foldout)
                {
                    GUILayout.Space(5);
                    GUILayout.BeginVertical();

                    if (GUILayout.Button("Add Element"))
                    {
                        if (persistentData.classToLoad == null)
                        {
                            persistentData.classToLoad = new List <string>();
                        }
                        persistentData.classToLoad.Add("");
                    }

                    if (persistentData.classToLoad != null)
                    {
                        EditorGUI.indentLevel++;
                        for (var i = 0; i < persistentData.classToLoad.Count; i++)
                        {
                            GUILayout.BeginHorizontal();

                            int selectedType = 0;

                            selectedType = options.FindIndex((x) => { return(x.Equals(persistentData.classToLoad[i])); });

                            if (selectedType == -1)
                            {
                                selectedType = 0;
                            }

                            selectedType = EditorGUILayout.Popup("Type", selectedType, options.ToArray());

                            persistentData.classToLoad[i] = options[selectedType];

                            if (GUILayout.Button("X", GUILayout.Width(18)))
                            {
                                persistentData.classToLoad.RemoveAt(i);
                            }

                            GUILayout.EndHorizontal();
                        }
                    }

                    EditorGUI.indentLevel--;
                    GUILayout.EndVertical();
                }

                EditorGUILayout.Space();
            }

            persistentData.Init();

            GUILayout.Space(10);

            if (persistentData.savedDataDictionnary != null)
            {
                foreach (List <SavedData> sdList in persistentData.savedDataDictionnary.Values)
                {
                    if (sdList != null && sdList.Count > 0)
                    {
                        GUILayout.BeginVertical(EditorStyles.textArea);
                        GUILayout.Space(2);
                        GUIStyle option = new GUIStyle();
                        option.alignment = TextAnchor.MiddleCenter;
                        option.fontSize  = 15;
                        option.fontStyle = FontStyle.Bold;
                        GUILayout.Label(sdList[0].GetType().Name, option);
                        GUILayout.Space(5);

                        foreach (SavedData sd in sdList)
                        {
                            AutoClassInspectorExtension.ShowAutoEditorGUI(sd);
                            GUILayout.Space(2);
                        }

                        GUILayout.EndVertical();
                        GUILayout.Space(2);
                    }
                }

                SaveDataEditor();
            }

            GUILayout.Space(10);
            LoadDataEditor();

            GUILayout.Space(10);
            if (GUILayout.Button("Unload saved data"))
            {
                persistentData.UnloadAllSavedData();
            }

            GUILayout.Space(10);

            serializedObject.ApplyModifiedProperties();
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(target, "Persisdent Data System");
            }
        }