示例#1
0
        public static void Save(string key)
        {
            key += "InventorySystem";
            key += " [" + UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + "]";

            string savedKeys = PlayerPrefs.GetString("SavedKeys");

            PlayerPrefs.SetString("SavedKeys", savedKeys + ";" + key);

            List <MonoBehaviour> results = new List <MonoBehaviour>();

            UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren <MonoBehaviour>(true)));
            //DowntDestroyOnLoad GameObjects
            SingleInstance.GetInstanceObjects().ForEach(g => results.AddRange(g.GetComponentsInChildren <MonoBehaviour>(true)));

            IJsonSerializable[] serializables = results.OfType <ItemCollection>().ToArray();

            string data = JsonSerializer.Serialize(serializables);

            PlayerPrefs.SetString(key, data);

            if (InventoryManager.current != null && InventoryManager.current.onSceneSaved != null)
            {
                InventoryManager.current.onSceneSaved.Invoke();
            }
            if (InventoryManager.DefaultSettings.debugMessages)
            {
                Debug.Log("[Inventory System] Data saved: " + data);
            }
        }
示例#2
0
        public static void Save(string key)
        {
            List <MonoBehaviour> results = new List <MonoBehaviour>();

            UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren <MonoBehaviour>(true)));
            //DontDestroyOnLoad GameObjects
            SingleInstance.GetInstanceObjects().ForEach(g => results.AddRange(g.GetComponentsInChildren <MonoBehaviour>(true)));

            ItemCollection[] serializables = results.OfType <ItemCollection>().Where(x => x.saveable).ToArray();

            IJsonSerializable[] ui    = serializables.Where(x => x.GetComponent <ItemContainer>() != null).ToArray();
            IJsonSerializable[] world = serializables.Except(ui).ToArray();

            string uiData       = JsonSerializer.Serialize(ui);
            string worldData    = JsonSerializer.Serialize(world);
            string currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;

            PlayerPrefs.SetString(key + ".UI", uiData);
            PlayerPrefs.SetString(key + "." + currentScene, worldData);
            List <string> scenes = PlayerPrefs.GetString(key + ".Scenes").Split(';').ToList();

            scenes.RemoveAll(x => string.IsNullOrEmpty(x));
            if (!scenes.Contains(currentScene))
            {
                scenes.Add(currentScene);
            }
            PlayerPrefs.SetString(key + ".Scenes", string.Join(";", scenes));
            List <string> keys = PlayerPrefs.GetString("InventorySystemSavedKeys").Split(';').ToList();

            keys.RemoveAll(x => string.IsNullOrEmpty(x));
            if (!keys.Contains(key))
            {
                keys.Add(key);
            }
            PlayerPrefs.SetString("InventorySystemSavedKeys", string.Join(";", keys));


            if (InventoryManager.current != null && InventoryManager.current.onDataSaved != null)
            {
                InventoryManager.current.onDataSaved.Invoke();
            }

            if (InventoryManager.DefaultSettings.debugMessages)
            {
                Debug.Log("[Inventory System] UI Saved: " + uiData);
                Debug.Log("[Inventory System] Scene Saved: " + worldData);
            }
        }