Example #1
0
        public void Delete()
        {
            KeyValuePair <string, List <JSONLevelSerializer.SaveEntry> > keyValuePair = JSONLevelSerializer.SavedGames.FirstOrDefault((KeyValuePair <string, List <JSONLevelSerializer.SaveEntry> > p) => p.Value.Contains(this));

            if (keyValuePair.Value != null)
            {
                keyValuePair.Value.Remove(this);
                JSONLevelSerializer.SaveDataToPlayerPrefs();
            }
        }
Example #2
0
    void OnGUI()
    {
        if (_assetStore.Count == 0)
        {
            BuildLocalAssetStore();
        }
        var bold     = new GUIStyle("label");
        var boldFold = new GUIStyle("foldout");

        bold.fontStyle     = FontStyle.Bold;
        bold.fontSize      = 14;
        boldFold.fontStyle = FontStyle.Bold;
        var someMatched = false;

        using (new Horizontal()) {
            if (GUILayout.Toggle(showMain, "Save Game", "toolbarbutton", GUILayout.ExpandWidth(false)))
            {
                showMain       = true;
                showReferences = false;
                showRoom       = false;
            }

            if (GUILayout.Toggle(showRoom, "Rooms", "toolbarbutton", GUILayout.ExpandWidth(false)))
            {
                showMain       = false;
                showRoom       = true;
                showReferences = false;
            }
            if (_assetStore.SelectMany(s => s.Value).All(s => s.Value.Count <= 1))
            {
                GUI.color = Color.white;
            }
            else
            {
                someMatched = true;
                GUI.color   = Color.yellow;
            }

            if (GUILayout.Toggle(showReferences, "References", "toolbarbutton", GUILayout.ExpandWidth(false)))
            {
                showMain       = false;
                showRoom       = false;
                showReferences = true;
            }
            GUILayout.Label("", "toolbarbutton");
            GUI.color = Color.white;
        }
        if (showReferences)
        {
            using (new ScrollView(ref scroll)) {
                using (new Vertical()) {
                    foreach (var tp in _assetStore)
                    {
                        if (!open.ContainsKey(tp.Key))
                        {
                            open [tp.Key] = false;
                        }

                        if (tp.Value.Any(v => v.Value.Count > 1))
                        {
                            GUI.color     = Color.yellow;
                            open [tp.Key] = EditorGUILayout.Foldout(open [tp.Key], tp.Key.ToString(), boldFold);
                            GUI.color     = Color.white;
                        }
                        else
                        {
                            open [tp.Key] = EditorGUILayout.Foldout(open [tp.Key], tp.Key.ToString(), "foldout");
                        }
                        if (open [tp.Key])
                        {
                            foreach (var n in tp.Value.OrderByDescending(q => q.Value.Count).ThenBy(q => q.Key))
                            {
                                using (new Horizontal()) {
                                    GUILayout.Space(10);
                                    if (!open.ContainsKey(tp.Key + n.Key))
                                    {
                                        open [tp.Key + n.Key] = false;
                                    }
                                    if (n.Value.Count <= 1)
                                    {
                                        open[tp.Key + n.Key] = false;
                                        if (GUILayout.Button(n.Key, "label"))
                                        {
                                            Selection.activeObject = n.Value[0] as UnityEngine.Object;
                                            EditorGUIUtility.PingObject(n.Value[0] as UnityEngine.Object);
                                        }
                                    }
                                    else
                                    {
                                        open [tp.Key + n.Key] = EditorGUILayout.Foldout(open [tp.Key + n.Key], n.Key + " (" + n.Value.Count + ")", boldFold);
                                    }
                                }
                                if (open [tp.Key + n.Key])
                                {
                                    using (new Vertical()) {
                                        foreach (var i in n.Value.Cast <UnityEngine.Object>())
                                        {
                                            using (new Horizontal()) {
                                                GUILayout.Space(20);
                                                var addOn = "";
                                                if (!AssetDatabase.IsMainAsset(i) && !AssetDatabase.IsSubAsset(i))
                                                {
                                                    GUI.color = Color.red;
                                                    addOn     = " (internal to Unity)";
                                                }
                                                else
                                                {
                                                    GUI.color = Color.white;
                                                }
                                                if (GUILayout.Button(i.name.ToString() + addOn, "label"))
                                                {
                                                    Selection.activeObject = i as UnityEngine.Object;
                                                    EditorGUIUtility.PingObject(i);
                                                }
                                                GUI.color = Color.white;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (someMatched)
            {
                var style = new GUIStyle("label");
                style.wordWrap = true;

                GUILayout.Space(5);
                GUI.color = Color.yellow;
                GUILayout.Label("Warning", bold);
                GUI.color = Color.white;
                GUILayout.Label("Some referencable items have the same name. This is only a problem if you actually refer to one of the items in a saved variable and then more get added with the same name (existing saved games may load the wrong instance), or if for some other reason Unity reorders the similarly named items.  If you use a highlighted item as a reference it may be worth renaming it to be a unique name if you can.", style);
                GUILayout.Space(5);
            }
        }

        if (showMain)
        {
            GUILayout.BeginVertical();
            if (Selection.gameObjects.Length == 1)
            {
                if (Selection.activeGameObject != null)
                {
                    GUILayout.Space(8);
                    using (new HorizontalCentered()) {
                        GUILayout.Label(Selection.activeGameObject.name, bold);
                    }
                    using (new HorizontalCentered()) {
                        GUI.color = new Color(0.8f, 1f, 0.8f, 1);

                        if (Has(Selection.activeGameObject, typeof(UniqueIdentifier)))
                        {
                            GUILayout.Label("Uniquely Identified");
                        }
                        else if (Has(Selection.activeGameObject, typeof(StoreInformation)))
                        {
                            GUILayout.Label("Storing Information");
                        }
                        else if (Has(Selection.activeGameObject, typeof(PrefabIdentifier)))
                        {
                            GUILayout.Label("Prefab Identifier");
                        }
                        else
                        {
                            GUI.color = Color.gray;
                            GUILayout.Label("Nothing stored");
                        }
                        if (Has(Selection.activeGameObject, typeof(StoreMaterials)))
                        {
                            GUILayout.Label(" + materials");
                        }
                        if (Has(Selection.activeGameObject, typeof(StoreMesh)))
                        {
                            GUILayout.Label(" + mesh");
                        }
                    }
                    GUILayout.Space(4);
                    GUI.color = Color.white;
                    using (new HorizontalCentered()) {
                        using (new Vertical()) {
                            using (new Horizontal()) {
                                if (!Has(Selection.activeGameObject, typeof(UniqueIdentifier)) && GUILayout.Button("Unique Identifier"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    Add(typeof(UniqueIdentifier), Selection.activeGameObject);
                                }
                                if (!Has(Selection.activeGameObject, typeof(StoreInformation)) && GUILayout.Button("Store Information"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    Add(typeof(StoreInformation), Selection.activeGameObject);
                                }
                                if (Selection.activeGameObject.GetComponent <StoreMaterials>() == null && Selection.activeGameObject.GetComponent <StoreInformation>() != null)
                                {
                                    if (GUILayout.Button("Store Materials"))
                                    {
                                        Undo.RegisterSceneUndo("Change Identifiers");
                                        Selection.activeGameObject.AddComponent <StoreMaterials>();
                                    }
                                }
                                if (Selection.activeGameObject.GetComponent <StoreMesh>() == null && Selection.activeGameObject.GetComponent <StoreInformation>() != null && (Selection.activeGameObject.GetComponent <MeshFilter>() != null || Selection.activeGameObject.GetComponent <SkinnedMeshRenderer>() != null))
                                {
                                    if (GUILayout.Button("Store Mesh"))
                                    {
                                        Undo.RegisterSceneUndo("Change Identifiers");
                                        Selection.activeGameObject.AddComponent <StoreMesh>();
                                    }
                                }
                            }
                            using (new Horizontal()) {
                                if (!Has(Selection.activeGameObject, typeof(PrefabIdentifier)) && PrefabUtility.GetPrefabType(Selection.activeGameObject) != PrefabType.None && GUILayout.Button("Prefab Identifier"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    Add(typeof(PrefabIdentifier), Selection.activeGameObject);
                                    PrefabUtility.ReplacePrefab(Selection.activeGameObject, PrefabUtility.GetPrefabParent(Selection.activeGameObject), ReplacePrefabOptions.ConnectToPrefab);
                                }
                                GUI.color = new Color(0.9f, 0.5f, 0.5f);
                                if (GUILayout.Button("Remove"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    foreach (var c in Selection.activeGameObject.GetComponents <UniqueIdentifier>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                    foreach (var c in Selection.activeGameObject.GetComponents <StoreMaterials>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                    foreach (var c in Selection.activeGameObject.GetComponents <StoreMesh>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                }
                                GUI.color = Color.white;
                            }
                        }
                    }
                    GUILayout.Space(12);
                    if (Selection.activeGameObject.transform.Cast <Transform> ().Count() > 0)
                    {
                        using (new HorizontalCentered()) {
                            GUILayout.Label(Selection.activeGameObject.name, bold);
                            bold.fontStyle = FontStyle.Italic;
                            GUILayout.Label("+ Children", bold);
                        }
                        GUILayout.Space(6);
                        using (new HorizontalCentered()) {
                            using (new Vertical()) {
                                using (new Horizontal()) {
                                    if (GUILayout.Button("Unique Identifier"))
                                    {
                                        Undo.RegisterSceneUndo("Change Identifiers");
                                        foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <Transform>().Select(t => t.gameObject).Where(g => !Has(g, typeof(UniqueIdentifier))))
                                        {
                                            Add(typeof(UniqueIdentifier), c);
                                        }
                                    }
                                    if (GUILayout.Button("Store Information"))
                                    {
                                        Undo.RegisterSceneUndo("Change Identifiers");
                                        foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <Transform>().Select(t => t.gameObject).Where(g => !Has(g, typeof(StoreInformation))))
                                        {
                                            Add(typeof(StoreInformation), c);
                                        }
                                    }
                                }
                                GUI.color = new Color(0.9f, 0.5f, 0.5f);
                                if (GUILayout.Button("Remove All"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <UniqueIdentifier>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                    foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <StoreMaterials>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                    foreach (var c in Selection.activeGameObject.GetComponents <StoreMesh>())
                                    {
                                        DestroyImmediate(c, true);
                                    }
                                }
                                GUI.color = Color.white;
                            }
                        }
                    }
                }
            }
            else if (Selection.gameObjects.Length > 0)
            {
                using (new HorizontalCentered()) {
                    GUILayout.Label("Selected Objects", bold);
                }
                GUILayout.Space(6);
                using (new HorizontalCentered()) {
                    using (new Vertical()) {
                        using (new Horizontal()) {
                            if (GUILayout.Button("Unique Identifier"))
                            {
                                Undo.RegisterSceneUndo("Change Identifiers");
                                foreach (var c in Selection.gameObjects)
                                {
                                    Add(typeof(UniqueIdentifier), c);
                                }
                            }
                            if (GUILayout.Button("Store Information"))
                            {
                                Undo.RegisterSceneUndo("Change Identifiers");
                                foreach (var c in Selection.gameObjects)
                                {
                                    Add(typeof(StoreInformation), c);
                                }
                            }
                        }
                        using (new Horizontal())
                        {
                            if (GUILayout.Button("Store Materials"))
                            {
                                foreach (var c in Selection.gameObjects.Where(g => g.GetComponent <StoreMaterials>() == null && g.GetComponent <Renderer>() != null))
                                {
                                    c.AddComponent <StoreMaterials>();
                                }
                            }
                            GUI.color = new Color(0.9f, 0.5f, 0.5f);
                            if (GUILayout.Button("Remove All"))
                            {
                                Undo.RegisterSceneUndo("Change Identifiers");
                                foreach (var c in Selection.gameObjects.Cast <GameObject>().SelectMany(s => s.GetAllComponentsInChildren <UniqueIdentifier>()))
                                {
                                    DestroyImmediate(c);
                                }
                            }
                            GUI.color = Color.white;
                        }
                    }
                }
            }
            GUILayout.FlexibleSpace();
            var items = GameObject.FindObjectsOfType(typeof(SaveGameManager));
            if (items == null || items.Length == 0)
            {
                GUI.color = new Color(1, 0.6f, 0.6f, 1f);
                var style = new GUIStyle("label");
                style.wordWrap = true;
                GUILayout.Label("You need to assign a Save Game Manager script to an object in this scene.  This object should be something that ISN'T loaded and saved with one of the scripts, and NOT set to DontDestroyOnLoad.", style);
                GUILayout.Space(6);
                GUI.color = Color.white;
                using (new Horizontal()) {
                    if (Selection.activeGameObject != null && GUILayout.Button("Assign Save Game Manager"))
                    {
                        Undo.RegisterSceneUndo("Assign save game manager");
                        Selection.activeGameObject.AddComponent <SaveGameManager> ();
                    }
                    if (GUILayout.Button("Create a new Save Game Manager"))
                    {
                        Undo.RegisterSceneUndo("Create save game manager");
                        var go = new GameObject();
                        go.name = "Save Game Manager";
                        go.AddComponent <SaveGameManager> ();
                    }
                }
                GUILayout.Space(15);
            }



            GUILayout.Label("Maintenance");
            using (new Horizontal()) {
                if (GUILayout.Button("Delete all saved games"))
                {
                    if (EditorUtility.DisplayDialog("Delete Saved Games",
                                                    "Are you sure you want to delete all of the saved games?", "Yes", "No"))
                    {
                        LevelSerializer.SavedGames.Clear();
                        LevelSerializer.SaveDataToPlayerPrefs();
                        JSONLevelSerializer.SavedGames.Clear();
                        JSONLevelSerializer.SaveDataToPlayerPrefs();
                    }
                }
                if (GameObject.FindObjectsOfType(typeof(SaveGameManager)).Length != 0 && GUILayout.Button("Capture All Objects"))
                {
                    EditorApplication.isPlaying = true;
                    stopWhenReady = 50;
                }
            }
            GUILayout.Space(8);
            using (new Horizontal()) {
                GUILayout.Label(string.Format("Tracked Scene Items: {0:#,0}", UniqueIdentifier.AllIdentifiers.Count));
                var old = drawInfo;
                drawInfo = GUILayout.Toggle(drawInfo, "Hierarchy markers");
                if (old != drawInfo)
                {
                    EditorApplication.RepaintHierarchyWindow();
                    EditorPrefs.SetBool("UnitySerializer.drawHierarchy", drawInfo);
                }
            }
            GUILayout.Space(4);
            GUILayout.EndVertical();
        }

        if (showRoom)
        {
            if (GameObject.FindObjectsOfType(typeof(Room)).Length > 0)
            {
                GUILayout.BeginVertical();
                if (Selection.gameObjects.Length == 1)
                {
                    if (Selection.activeGameObject != null)
                    {
                        GUILayout.Space(8);
                        using (new HorizontalCentered()) {
                            GUILayout.Label(Selection.activeGameObject.name, bold);
                        }
                        using (new HorizontalCentered()) {
                            GUI.color = new Color(0.8f, 1f, 0.8f, 1);


                            if (Selection.activeGameObject.GetComponent <DontStoreObjectInRoom>())
                            {
                                GUILayout.Label(Has(Selection.activeGameObject, typeof(StoreInformation)) ?  "Stored in the save game but not stored in the room state" : "Not saved but flagged for exclusion anyway.");
                            }
                            else if (Has(Selection.activeGameObject, typeof(StoreInformation)))
                            {
                                GUILayout.Label("Stored in the saved game and the room");
                            }
                        }
                        GUILayout.Space(4);
                        GUI.color = Color.white;
                        using (new HorizontalCentered()) {
                            using (new Vertical()) {
                                using (new Horizontal()) {
                                    if (!Selection.activeGameObject.GetComponent <DontStoreObjectInRoom>() && GUILayout.Button("Exclude from room"))
                                    {
                                        Undo.RegisterSceneUndo("Exclude");
                                        Add(typeof(DontStoreObjectInRoom), Selection.activeGameObject);
                                    }
                                }
                                GUI.color = new Color(0.9f, 0.5f, 0.5f);
                                if (GUILayout.Button("Remove"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    foreach (var c in Selection.activeGameObject.GetComponents <DontStoreObjectInRoom>())
                                    {
                                        DestroyImmediate(c);
                                    }
                                }
                                GUI.color = Color.white;
                            }
                        }
                        GUILayout.Space(12);
                        if (Selection.activeGameObject.transform.Cast <Transform> ().Count() > 0)
                        {
                            using (new HorizontalCentered()) {
                                GUILayout.Label(Selection.activeGameObject.name, bold);
                                bold.fontStyle = FontStyle.Italic;
                                GUILayout.Label("+ Children", bold);
                            }
                            GUILayout.Space(6);
                            using (new HorizontalCentered()) {
                                using (new Vertical()) {
                                    using (new Horizontal()) {
                                        if (GUILayout.Button("Exclude from room"))
                                        {
                                            Undo.RegisterSceneUndo("Change Identifiers");
                                            foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <Transform>().Select(t => t.gameObject).Where(g => !g.GetComponent <DontStoreObjectInRoom>()))
                                            {
                                                Add(typeof(DontStoreObjectInRoom), c);
                                            }
                                        }


                                        GUI.color = new Color(0.9f, 0.5f, 0.5f);
                                        if (GUILayout.Button("Remove"))
                                        {
                                            Undo.RegisterSceneUndo("Change Identifiers");
                                            foreach (var c in Selection.activeGameObject.GetAllComponentsInChildren <DontStoreObjectInRoom>())
                                            {
                                                DestroyImmediate(c);
                                            }
                                        }
                                        GUI.color = Color.white;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (Selection.gameObjects.Length > 0)
                {
                    using (new HorizontalCentered()) {
                        GUILayout.Label("Selected Objects", bold);
                    }
                    GUILayout.Space(6);
                    using (new HorizontalCentered()) {
                        using (new Vertical()) {
                            using (new Horizontal()) {
                                if (GUILayout.Button("Exlude from room"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    foreach (var c in Selection.gameObjects.Where(g => g.GetComponent <DontStoreObjectInRoom>()))
                                    {
                                        Add(typeof(DontStoreObjectInRoom), c);
                                    }
                                }
                            }
                            using (new Horizontal())
                            {
                                GUI.color = new Color(0.9f, 0.5f, 0.5f);
                                if (GUILayout.Button("Remove"))
                                {
                                    Undo.RegisterSceneUndo("Change Identifiers");
                                    foreach (var c in Selection.gameObjects.Cast <GameObject>().SelectMany(s => s.GetAllComponentsInChildren <DontStoreObjectInRoom>()))
                                    {
                                        DestroyImmediate(c);
                                    }
                                }
                                GUI.color = Color.white;
                            }
                        }
                    }
                }
            }
            GUILayout.FlexibleSpace();
            if (GameObject.FindObjectsOfType(typeof(Room)).Length == 0)
            {
                GUI.color = new Color(1, 0.6f, 0.6f, 1f);
                var style = new GUIStyle("label");
                style.wordWrap = true;
                GUILayout.Label("You need to assign a Room Manager script to an object in this scene.  This object should be something that IS loaded and saved with one of the scripts, and NOT set to DontDestroyOnLoad.", style);
                GUILayout.Space(6);
                GUI.color = Color.white;
                using (new Horizontal()) {
                    if (GUILayout.Button("Create a new Room Manager"))
                    {
                        Undo.RegisterSceneUndo("Create room manager");
                        var go = new GameObject();
                        go.name = "Room Manager";
                        go.AddComponent <StoreInformation> ();
                        go.AddComponent <RoomDataSaveGameStorage>();
                        go.AddComponent <Room>();
                    }
                }
                GUILayout.Space(15);
            }



            GUILayout.Label("Maintenance");
            using (new Horizontal()) {
                if (GUILayout.Button("Delete all saved games"))
                {
                    if (EditorUtility.DisplayDialog("Delete Saved Games",
                                                    "Are you sure you want to delete all of the saved games?", "Yes", "No"))
                    {
                        LevelSerializer.SavedGames.Clear();
                        LevelSerializer.SaveDataToPlayerPrefs();
                        JSONLevelSerializer.SavedGames.Clear();
                        JSONLevelSerializer.SaveDataToPlayerPrefs();
                    }
                }
                if (GameObject.FindObjectsOfType(typeof(SaveGameManager)).Length != 0 && GUILayout.Button("Capture All Objects"))
                {
                    EditorApplication.isPlaying = true;
                    stopWhenReady = 50;
                }
            }
            GUILayout.Space(8);
            using (new Horizontal()) {
                GUILayout.Label(string.Format("Tracked Scene Items: {0:#,0}", UniqueIdentifier.AllIdentifiers.Count));
                var old = drawInfo;
                drawInfo = GUILayout.Toggle(drawInfo, "Hierarchy markers");
                if (old != drawInfo)
                {
                    EditorApplication.RepaintHierarchyWindow();
                    EditorPrefs.SetBool("UnitySerializer.drawHierarchy", drawInfo);
                }
            }
            GUILayout.Space(4);
            GUILayout.EndVertical();
        }
        Repaint();
    }