Exemple #1
0
        public static void WriteConfig(SaveConfig config)
        {
            string savePath = $"{Application.persistentDataPath}/{configFileName}{fileExtentionName}";

            Log($"Saving game configuration to : {savePath}");

            using (var writer = new BinaryWriter(File.Open(savePath, FileMode.Create)))
            {
                writer.Write(JsonUtility.ToJson(config));
            }

#if UNITY_WEBGL && !UNITY_EDITOR
            SyncFiles();
#endif
        }
Exemple #2
0
        public static SaveConfig LoadOrCreateConfig()
        {
#if UNITY_WEBGL && !UNITY_EDITOR
            SyncFiles();
#endif

            if (cachedSaveConfig != null)
            {
                return(cachedSaveConfig);
            }

            string[] filePaths = Directory.GetFiles(Application.persistentDataPath);

            string configPath = filePaths.FirstOrDefault(path => path.EndsWith($"{configFileName}{fileExtentionName}"));

            if (!string.IsNullOrEmpty(configPath))
            {
                using (var reader = new BinaryReader(File.Open(configPath, FileMode.Open)))
                {
                    SaveConfig getSaveConfig = JsonUtility.FromJson <SaveConfig>(reader.ReadString());

                    if (getSaveConfig != null)
                    {
                        Log($"Loaded configuration from {configPath}");
                        cachedSaveConfig = getSaveConfig;
                    }
                    else
                    {
                        getSaveConfig = new SaveConfig();
                    }

                    return(getSaveConfig);
                }
            }
            else
            {
                cachedSaveConfig = new SaveConfig();

                return(cachedSaveConfig);
            }
        }