// *********************************************************************** // Company : // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : 2016-03-30 // *********************************************************************** namespace G { using System.Collections; using UnityEngine; /// /// 转换方法集 /// public static class KUtils { #region CONST #endregion #region ENUM #endregion #region MODEL #endregion #region FIELD #endregion #region PROPERTY #endregion #region METHOD #endregion #region STATIC private static string _PersistentDataPath; public static string PersistentDataPath { get { if (_PersistentDataPath.IsNullOrEmpty()) { _PersistentDataPath = Application.persistentDataPath; if (_PersistentDataPath.IsNullOrEmpty()) { //_PersistentDataPath = KPlatform.InternalGetPersistentDataPath(); } } return _PersistentDataPath; } } /// /// add component if not have one /// /// Component /// target /// 是否从子物体中获取T public static T AddComponentIfNotHave(GameObject go, bool checkChild = false) where T : UnityEngine.Component { T t = default(T); if (checkChild) { t = go.GetComponentInChildren(); } else { t = go.GetComponent(); } if (t == null) { t = (T)go.AddComponent(typeof(T)); } return t; } /// /// add an element to array by index /// /// 默认加到数组尾部 public static void AddElementAt(ref T[] arr, T t, int index = -1) where T : new() { if (arr == null) { arr = new T[] { }; } T[] temp = new T[arr.Length + 1]; if (index == -1) { index = arr.Length; } if (index > temp.Length) { //KLog.LogWarning("insert index out of range"); return; } if (t == null) { t = new T(); } if (temp.Length > 1) { for (int i = 0; i < temp.Length; i++) { if (i < index) { temp[i] = arr[i]; } else if (i == index) { temp[index] = t; } else { temp[i] = arr[i - 1]; } } } arr = temp; } /// /// 检查特定DateTime和当前是不是差距特定天数 /// /// /// /// 如果是true,则认为checkDate在当前之后 /// public static bool CheckDate(System.DateTime checkDate, int day, bool afterNow = false) { System.DateTime now = System.DateTime.Now; double days; if (afterNow) { days = (checkDate - now).TotalDays; } else { days = (now - checkDate).TotalDays; } if (days >= day) { return true; } return false; } /// /// 检查特定DateTime和当前是不是差距特定秒数 /// /// /// /// 如果是true,则认为checkDate在当前之后 /// public static bool CheckSecond(System.DateTime checkMinute, int second, bool afterNow = false) { System.DateTime now = System.DateTime.Now; double seconds; if (afterNow) { seconds = (checkMinute - now).TotalSeconds; } else { seconds = (now - checkMinute).TotalSeconds; } if (seconds >= second) { return true; } return false; } /// /// 为空或者是"" /// /// /// public static bool IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } /// /// 为空或者全是占位符 /// /// /// public static bool IsNullOrWhiteSpace(this string str) { return string.IsNullOrEmpty(str) || str.Trim().Length == 0; } /// /// string转boolean /// /// /// public static bool ToBoolean(this string value) { bool result; bool.TryParse(value, out result); return result; } /// /// string转float /// /// /// public static float ToFloat(this string value) { float result; float.TryParse(value, out result); return result; } /// /// string转int /// /// /// public static int ToInt(this string value) { int result; int.TryParse(value, out result); return result; } /// Generates the unique identifier. /// public static string GenerateGUID() { return System.Guid.NewGuid().ToString(); } /// Generates the hash. /// The value. /// public static uint GenerateHash(string value) { uint b = 378551; uint a = 63689; uint hash = 0; foreach (var str in value) { hash = hash * a + str; a *= b; } return hash; } #endregion } }