public static string Serialize(T value)
        {
            var obj = new ValueWrapper <T>()
            {
                m_Value = value
            };

            return(EditorJsonUtility.ToJson(obj, k_PrettyPrintJson));
        }
Exemple #2
0
        /// <summary>
        /// Delete the saved setting. Does not clear the current value.
        /// </summary>
        /// <see cref="M:UnityEditor.SettingsManagement.UserSetting`1.Reset(System.Boolean)" />
        /// <param name="saveProjectSettingsImmediately">True to immediately re-serialize project settings.</param>
        /// <inheritdoc cref="IUserSetting.Delete"/>
        public void Delete(bool saveProjectSettingsImmediately = false)
        {
            settings.DeleteKey <T>(key, scope);
            // Don't Init() because that will set the key again. We just want to reset the m_Value with default and
            // pretend that this field hasn't been initialised yet.
            m_Value = ValueWrapper <T> .DeepCopy(m_DefaultValue);

            m_Initialized = false;
        }
        public void Set <T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            var type = typeof(T).AssemblyQualifiedName;

            SetJson(type, key, ValueWrapper <T> .Serialize(value));
        }
        public T Get <T>(string key, T fallback = default(T))
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            Dictionary <string, string> entries;

            if (dictionary.TryGetValue(typeof(T), out entries) && entries.ContainsKey(key))
            {
                try
                {
                    return(ValueWrapper <T> .Deserialize(entries[key]));
                }
                catch
                {
                    return(fallback);
                }
            }

            return(fallback);
        }
Exemple #5
0
        void Init()
        {
            if (!m_Initialized)
            {
                if (m_Scope == SettingsScope.Project && settings == null)
                {
                    throw new Exception("UserSetting \"" + m_Key + "\" is attempting to access SettingsScope.Project setting with no Settings instance!");
                }

                m_Initialized = true;

                // DeepCopy uses EditorJsonUtility which is not permitted during construction
                m_DefaultValue = ValueWrapper <T> .DeepCopy(m_Value);

                if (settings.ContainsKey <T>(m_Key, m_Scope))
                {
                    m_Value = settings.Get <T>(m_Key, m_Scope);
                }
                else
                {
                    settings.Set <T>(m_Key, m_Value, m_Scope);
                }
            }
        }