コード例 #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));
            }
        }