Exemplo n.º 1
0
        /// <summary>
        /// Save the variables contained in the specified <see cref="VariablesContainer"/> on the disk in order to retrieve them later,
        /// using the <see cref="GetVariablesFromDisk"/> function.
        /// </summary>
        /// <param name="currentContainer"></param>
        public static void SaveVariables(VariablesContainer currentContainer)
        {
            // string varsJson = JsonUtility.ToJson(currentContainer);

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            };

            string varsJson = JsonConvert.SerializeObject(currentContainer, settings);


            PlayerPrefs.SetString(VariablesPrefKeyCst, varsJson);
            PlayerPrefs.Save();
#if (ENABLE_DEBUG_DIALOG)
            StringBuilder sb = new StringBuilder("Saved the Wired Dialog Engine variables. Variables are:" + Environment.NewLine);
            foreach (Variable var in currentContainer.IndependantVariables)
            {
                if (!(((object)var) == null) && !var.Equals(null))
                {
                    sb.Append(var.VariableName + ": '" + var.Value + "'" + Environment.NewLine);
                }
            }
            Debug.Log(sb.ToString());
#endif
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the <see cref="VariablesContainer"/> containing the dialog variables from the disk. These variables can be modified at
        /// runtime, unlike the dialog constants.
        /// </summary>
        /// <returns>Returns the <see cref="VariablesContainer"/> containing the dialog variables from the disk. These variables can be modified at
        /// runtime, unlike the dialog constants.</returns>
        /// <exception cref="T:System.Exception">Throw an exception when the <see cref="PlayerPrefs"/> key used
        /// to save the variables hasn't been found</exception>
        public static VariablesContainer GetVariablesFromDisk()
        {
            if (!PlayerPrefs.HasKey(VariablesPrefKeyCst))
            {
                throw new Exception("The PlayersPref key used by the DialogEngine hasn't been found. Unable to get the variables from the disk. \n" +
                                    " Make sure you have already saved the variables at least one time before use the GetVariablesFromDisk() function.");
            }

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            };

            string varsJson = PlayerPrefs.GetString(VariablesPrefKeyCst);

            VariablesContainer vars = JsonConvert.DeserializeObject <VariablesContainer>(varsJson, settings);

            //VariablesContainer vars = JsonUtility.FromJson<VariablesContainer>(varsJson);
            return(vars);
        }