public static string AsTimeStringFromSecondsShort(this CastSource <long> source) { var s = source.value; var m = Math.Floor((float)s / 60); var h = Math.Floor((float)m / 60); var d = Math.Floor((float)h / 24); var w = Math.Floor((float)d / 7); string result; if (s < 60) { result = string.Format("{0}s", s); } else if (m < 60) { result = string.Format("{1}m {0}s", s % 60, m); } else if (h < 48) { result = string.Format("{1}h {0}m", m % 60, h); } else if (d < 7) { result = string.Format("{2}d {1}h {0}m", m % 60, h % 24, d % 7); } else { result = string.Format("{3}w {2}d {1}h {0}m", m % 60, h % 24, d % 7, w); } return(result); }
public static Color AsColor(this CastSource <string> source, Color defaultValue = default(Color)) { var value = source.value; if (source.value.Length <= 8 && !source.value.StartsWith("#")) { value = "#" + source.value; } return((TryParseColor(value, out Color result)) ? result : defaultValue); }
public static UnityEngine.Color AsUnityColor(this CastSource <string> source, UnityEngine.Color defaultValue = default(UnityEngine.Color)) { var value = source.value; if (source.value.Length <= 8 && !source.value.StartsWith("#")) { value = "#" + source.value; } return((ColorUtility.TryParseHtmlString(value, out UnityEngine.Color result)) ? result : defaultValue); }
public static Vector3Int AsVector3Int(this CastSource <string> source, Vector3Int defaultValue = default(Vector3Int)) { var result = defaultValue; var splittedValue = source.value.Split(vectorStringDelimiters, StringSplitOptions.RemoveEmptyEntries); if (splittedValue.Length == 3 && int.TryParse(splittedValue[0], out int x) && int.TryParse(splittedValue[1], out int y) && int.TryParse(splittedValue[2], out int z)) { result = new Vector3Int(x, y, z); } return(result); }
public static Vector4 AsVector4(this CastSource <string> source, Vector4 defaultValue = default(Vector4)) { var result = defaultValue; var splittedValue = source.value.Split(vectorStringDelimiters, StringSplitOptions.RemoveEmptyEntries); if (splittedValue.Length == 4 && float.TryParse(splittedValue[0], out float x) && float.TryParse(splittedValue[1], out float y) && float.TryParse(splittedValue[2], out float z) && float.TryParse(splittedValue[3], out float w)) { result = new Vector4(x, y, z, w); } return(result); }
public static Guid AsGuid(this CastSource <string> source, Guid defaultValue = default(Guid)) { var result = defaultValue; try { result = new Guid(source.value); } catch { } return(result); }
public static string AsFormattedBytesString <T>(this CastSource <T> source, bool round = false) where T : struct, IComparable, IComparable <T>, IConvertible, IEquatable <T>, IFormattable { var input = (ulong)Convert.ChangeType(source.value, typeof(ulong)); double result; string suffix; if (input >= 0x10000000000) { suffix = "TB"; result = (input >> 30); } else if (input >= 0x40000000) { suffix = "GB"; result = (input >> 20); } else if (input >= 0x100000) { suffix = "MB"; result = (input >> 10); } else if (input >= 0x400) { suffix = "KB"; result = input; } else { suffix = "B"; result = input; result *= 1024; } result /= 1024; return(result.ToString(round ? "0" : "0.00") + suffix); }
public static long AsLong(this CastSource <string> source, long defaultValue = default(long)) { return((long.TryParse(source.value, out long result)) ? result : defaultValue); }
public static long AsInt64(this CastSource <string> source, long defaultValue = default(long)) { return(source.AsLong(defaultValue)); }
public static int AsInt(this CastSource <string> source, int defaultValue = default(int)) { return((int.TryParse(source.value, out int result)) ? result : defaultValue); }
public static int AsInt32(this CastSource <string> source, int defaultValue = default(int)) { return(source.AsInt(defaultValue)); }
public static string AsString(this CastSource <int> source) { return(source.value.ToString()); }
public static T AsEnum <T>(this CastSource <string> source, T defaultValue = default(T)) where T : struct, Enum { return((Enum.TryParse(source.value, true, out T result)) ? result : defaultValue); }
public static string AsString(this CastSource <Vector2Int> source) { return($"{Cast.This(source.value.x).AsString()}, {Cast.This(source.value.y).AsString()}"); }
public static string AsString <T>(this CastSource <T> source) where T : Enum { return(source.value.ToString()); }
public static float AsSingle(this CastSource <string> source, float defaultValue = default(float)) { return((float.TryParse(source.value, out float result)) ? result : defaultValue); }
public static float AsFloat(this CastSource <string> source, float defaultValue = default(float)) { return(source.AsSingle(defaultValue)); }
public static char AsChar(this CastSource <string> source, char defaultValue = default(char)) { return((char.TryParse(source.value, out char result)) ? result : defaultValue); }
public static sbyte AsSByte(this CastSource <string> source, sbyte defaultValue = default(sbyte)) { return((sbyte.TryParse(source.value, out sbyte result)) ? result : defaultValue); }
public static bool AsBool(this CastSource <string> source, bool defaultValue = default(bool)) { return((bool.TryParse(source.value, out bool result)) ? result : defaultValue); }
public static ushort AsUShort(this CastSource <string> source, ushort defaultValue = default(ushort)) { return((ushort.TryParse(source.value, out ushort result)) ? result : defaultValue); }
public static ulong AsUInt64(this CastSource <string> source, ulong defaultValue = default(ulong)) { return(source.AsULong(defaultValue)); }
public static ushort AsUInt16(this CastSource <string> source, ushort defaultValue = default(ushort)) { return(source.AsUShort(defaultValue)); }
public static decimal AsDecimal(this CastSource <string> source, decimal defaultValue = default(decimal)) { return((decimal.TryParse(source.value, out decimal result)) ? result : defaultValue); }
public static Color32 AsUnityColor32(this CastSource <string> source, Color32 defaultValue = default(Color32)) { return(source.AsUnityColor(defaultValue)); }
public static decimal AsFloat128(this CastSource <string> source, decimal defaultValue = default(decimal)) { return(source.AsDecimal(defaultValue)); }
public static double AsDouble(this CastSource <string> source, double defaultValue = default(double)) { return((double.TryParse(source.value, out double result)) ? result : defaultValue); }
public static DateTime AsDateTimeFromUnixTimeStamp(this CastSource <string> source, DateTime defaultValue = default(DateTime)) { var defaultValueDouble = Cast.This(defaultValue).AsUnixTimeStamp(); return(Cast.This(source.AsDouble(defaultValueDouble)).AsDateTimeFromUnixTimeStamp()); }
public static string AsString(this CastSource <Color32> source, bool prependHashChar = true, bool toUpper = true) { return($"{(prependHashChar ? "#" : "")}{ConditionalCasing(ColorUtility.ToHtmlStringRGBA(source.value), toUpper)}"); }
public static double AsFloat64(this CastSource <string> source, double defaultValue = default(double)) { return(source.AsDouble(defaultValue)); }