예제 #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="message"></param>
 public static void Warn(this Transform obj, string message)
 {
     CLog.Err(obj, message);
 }
예제 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="message"></param>
 public static void Log(this Transform obj, string message)
 {
     CLog.Log(obj, message);
 }
예제 #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="message"></param>
 public static void Log(this MonoBehaviour obj, string message)
 {
     CLog.Log(obj, message);
 }
예제 #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="message"></param>
 public static void Warn(this MonoBehaviour obj, string message)
 {
     CLog.Err(obj, message);
 }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetData <T>()
        {
            //lấy dữ liệu thuộc kiểu T
            var text = PlayerPrefs.GetString(GetKey <T>(), null);

            if (text != null)
            {
                var resObj = (T)Activator.CreateInstance(typeof(T), new object[] { });
                //lấy dữ liệu cho vào dict
                Dictionary <string, string> dict = new Dictionary <string, string>();
                string[] lines = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    if (line.Length > 0 && line.Contains("="))
                    {
                        var temps = line.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                        if (temps.Length == 2)
                        {
                            dict.Add(temps[0], temps[1]);
                        }
                    }
                }
                //đổ dữ liệu từ dict vào obj
                var fields = typeof(T).GetFields();
                foreach (var field in fields)
                {
                    if (dict.ContainsKey(field.Name))
                    {
                        try
                        {
                            if (field.FieldType == typeof(bool) || field.FieldType == typeof(bool?))
                            {
                                field.SetValue(resObj, bool.Parse(dict[field.Name]));
                            }
                            else if (field.FieldType == typeof(string))
                            {
                                field.SetValue(resObj, dict[field.Name]);
                            }
                            else if (field.FieldType == typeof(float) || field.FieldType == typeof(float?))
                            {
                                field.SetValue(resObj, float.Parse(dict[field.Name]));
                            }
                            else if (field.FieldType == typeof(int) || field.FieldType == typeof(int?))
                            {
                                field.SetValue(resObj, int.Parse(dict[field.Name]));
                            }
                            else
                            {
                                CLog.Log(this, "Has not data for " + field.Name);
                            }
                        }
                        catch (Exception ex) { CLog.Log(this, ex.Message); }
                    }
                }
                return(resObj);
            }
            else
            {
                return(default(T));
            }
        }