예제 #1
0
        public static void Set(this EasyDictionary <string, EasyObject> writer, string key, object value, Type type)
        {
            if (value == null)
            {
                Debug.LogError("NullReferenceException: Object reference not set to an instance of an object");
            }
            else if (type == typeof(string))
            {
                SetObject(writer, key, value);
            }
            else if (type.IsArray)
            {
                Debug.LogError("Sorry, we can not auto convert array type to string type, " +
                               "But you can use SetArray<T> (string key, object value) method replaced.");
            }
            else if (type.IsSerializable && type.IsPrimitive)
            {
                SetObject(writer, key, value);
            }
            else if (type.IsSerializable && type.IsEnum)
            {
            }
            else if (type.IsSerializable && type == typeof(Nullable))
            {
            }
            else if (type.IsClass || (type.IsSerializable && type.IsValueType))
            {
#if UNITY_EDITOR
                SetObject(writer, key, JsonUtility.ToJson(value, true));
#else
                SetObject(writer, key, JsonUtility.ToJson(value));
#endif
            }
        }
예제 #2
0
 public static void Get(this EasyDictionary <string, EasyObject> writer, string key, object target, Type type)
 {
     if (type.IsSubclassOf(typeof(UnityEngine.Object)))
     {
         JsonUtility.FromJsonOverwrite(GetObject(writer, key).ToString(), target);
     }
 }
예제 #3
0
 public static object GetObject(this EasyDictionary <string, EasyObject> writer, string key)
 {
     if (writer.ToDictionary().ContainsKey(key))
     {
         return(writer.ToDictionary()[key].GetObject());
     }
     return(default(object));
 }
예제 #4
0
        public static T[] GetArray <T>(this EasyDictionary <string, EasyObject> writer, string key)
        {
            var content = Get <string>(writer, key);

            if (!string.IsNullOrEmpty(content))
            {
                return(content.ToArray <T>());
            }
            return(default(T[]));
        }
예제 #5
0
        public static void Clear(this EasyDictionary <string, EasyObject> writer)
        {
            var dic = writer.ToDictionary();

            if (dic != null)
            {
                dic.Clear();
                writer = new EasyDictionary <string, EasyObject>(dic);
            }
        }
예제 #6
0
        public static void Remove(this EasyDictionary <string, EasyObject> writer, string key)
        {
            var dic = writer.ToDictionary();

            if (dic != null && dic.ContainsKey(key))
            {
                dic.Remove(key);
                writer = new EasyDictionary <string, EasyObject>(dic);
            }
        }
예제 #7
0
 public static void SetObject(this EasyDictionary <string, EasyObject> writer, string key, object value)
 {
     if (HasKey(writer, key))
     {
         writer.ToDictionary()[key] = new EasyObject(value);
     }
     else
     {
         writer.ToDictionary().Add(key, new EasyObject(value));
     }
 }
예제 #8
0
        public static void GetArray <T>(this EasyDictionary <string, EasyObject> writer, string key, T[] target)
        {
            var array = GetArray <string>(writer, key);

            for (int i = 0; i < array.Length && i < target.Length; i++)
            {
                var type = target[i].GetType();
                if (type.IsSubclassOf(typeof(UnityEngine.Object)))
                {
                    JsonUtility.FromJsonOverwrite(array[i], target[i]);
                }
            }
        }
예제 #9
0
        void Initialize(string path, EasyDictionary <string, EasyObject> setter)
        {
            FilePath = path;
            writer   = setter ?? new EasyDictionary <string, EasyObject>();

            if (Application.isPlaying)
            {
                Observable.OnceApplicationQuit().Subscribe(_ =>
                {
                    Dispose();
                });
            }
        }
예제 #10
0
        public static void SetArray <T>(this EasyDictionary <string, EasyObject> writer, string key, object value)
        {
            var content = default(string);

            if (!value.GetType().IsArray)
            {
                T[] o = new object[] { value } as T[];
                content = o.ToString <T>();
            }
            else
            {
                content = value.ToString <T>();
            }
            Set <string>(writer, key, content);
        }
예제 #11
0
        public static T Get <T>(this EasyDictionary <string, EasyObject> writer, string key)
        {
            var type = typeof(T);

            if (type == typeof(string))
            {
                return((T)GetObject(writer, key));
            }
            else if (type.IsArray)
            {
                Debug.LogError("Sorry, we can not auto convert string type to array type, " +
                               "But you can use GetArray<T> (string key) method replaced.");
            }
            else if (type.IsSerializable && type.IsPrimitive)
            {
                return((T)GetObject(writer, key));
            }
            else if (type.IsSerializable && type.IsEnum)
            {
            }
            else if (type.IsSerializable && type == typeof(Nullable))
            {
            }
            else if (type.IsSerializable && (type.IsClass || type.IsValueType))
            {
                if (type.IsSubclassOf(typeof(UnityEngine.Object)))
                {
                    Debug.LogError("Sorry, we can not overwrite UnityEngine.Object Type(such as MonoBehaviour or ScriptableObject), " +
                                   "But you can use Get<T> (string key, T target) method replaced.");
                }
                else
                {
                    return(JsonUtility.FromJson <T>(GetObject(writer, key).ToString()));
                }
            }
            return(default(T));
        }
예제 #12
0
 public static void Get <T>(this EasyDictionary <string, EasyObject> writer, string key, T target)
 {
     Get(writer, key, target, target.GetType());
 }
예제 #13
0
 public static bool HasKey(this EasyDictionary <string, EasyObject> writer, string key)
 {
     return(writer.ToDictionary().ContainsKey(key));
 }
예제 #14
0
 public static void Set <T>(this EasyDictionary <string, EasyObject> writer, string key, T value)
 {
     Set(writer, key, value, typeof(T));
 }
예제 #15
0
 public ReactiveWriter(string path, EasyDictionary <string, EasyObject> setter)
 {
     Initialize(path, setter);
 }
예제 #16
0
 public ReactiveWriter(EasyDictionary <string, EasyObject> setter)
 {
     Initialize(null, setter);
 }