示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        public static Guid AsGuid(this CastSource <string> source, Guid defaultValue = default(Guid))
        {
            var result = defaultValue;

            try
            {
                result = new Guid(source.value);
            }
            catch
            {
            }

            return(result);
        }
示例#7
0
        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);
        }
示例#8
0
 public static long AsLong(this CastSource <string> source, long defaultValue = default(long))
 {
     return((long.TryParse(source.value, out long result)) ? result : defaultValue);
 }
示例#9
0
 public static long AsInt64(this CastSource <string> source, long defaultValue = default(long))
 {
     return(source.AsLong(defaultValue));
 }
示例#10
0
 public static int AsInt(this CastSource <string> source, int defaultValue = default(int))
 {
     return((int.TryParse(source.value, out int result)) ? result : defaultValue);
 }
示例#11
0
 public static int AsInt32(this CastSource <string> source, int defaultValue = default(int))
 {
     return(source.AsInt(defaultValue));
 }
示例#12
0
 public static string AsString(this CastSource <int> source)
 {
     return(source.value.ToString());
 }
示例#13
0
 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);
 }
示例#14
0
 public static string AsString(this CastSource <Vector2Int> source)
 {
     return($"{Cast.This(source.value.x).AsString()}, {Cast.This(source.value.y).AsString()}");
 }
示例#15
0
 public static string AsString <T>(this CastSource <T> source) where T : Enum
 {
     return(source.value.ToString());
 }
示例#16
0
 public static float AsSingle(this CastSource <string> source, float defaultValue = default(float))
 {
     return((float.TryParse(source.value, out float result)) ? result : defaultValue);
 }
示例#17
0
 public static float AsFloat(this CastSource <string> source, float defaultValue = default(float))
 {
     return(source.AsSingle(defaultValue));
 }
示例#18
0
 public static char AsChar(this CastSource <string> source, char defaultValue = default(char))
 {
     return((char.TryParse(source.value, out char result)) ? result : defaultValue);
 }
示例#19
0
 public static sbyte AsSByte(this CastSource <string> source, sbyte defaultValue = default(sbyte))
 {
     return((sbyte.TryParse(source.value, out sbyte result)) ? result : defaultValue);
 }
示例#20
0
 public static bool AsBool(this CastSource <string> source, bool defaultValue = default(bool))
 {
     return((bool.TryParse(source.value, out bool result)) ? result : defaultValue);
 }
示例#21
0
 public static ushort AsUShort(this CastSource <string> source, ushort defaultValue = default(ushort))
 {
     return((ushort.TryParse(source.value, out ushort result)) ? result : defaultValue);
 }
示例#22
0
 public static ulong AsUInt64(this CastSource <string> source, ulong defaultValue = default(ulong))
 {
     return(source.AsULong(defaultValue));
 }
示例#23
0
 public static ushort AsUInt16(this CastSource <string> source, ushort defaultValue = default(ushort))
 {
     return(source.AsUShort(defaultValue));
 }
示例#24
0
 public static decimal AsDecimal(this CastSource <string> source, decimal defaultValue = default(decimal))
 {
     return((decimal.TryParse(source.value, out decimal result)) ? result : defaultValue);
 }
示例#25
0
 public static Color32 AsUnityColor32(this CastSource <string> source, Color32 defaultValue = default(Color32))
 {
     return(source.AsUnityColor(defaultValue));
 }
示例#26
0
 public static decimal AsFloat128(this CastSource <string> source, decimal defaultValue = default(decimal))
 {
     return(source.AsDecimal(defaultValue));
 }
示例#27
0
 public static double AsDouble(this CastSource <string> source, double defaultValue = default(double))
 {
     return((double.TryParse(source.value, out double result)) ? result : defaultValue);
 }
示例#28
0
        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());
        }
示例#29
0
 public static string AsString(this CastSource <Color32> source, bool prependHashChar = true, bool toUpper = true)
 {
     return($"{(prependHashChar ? "#" : "")}{ConditionalCasing(ColorUtility.ToHtmlStringRGBA(source.value), toUpper)}");
 }
示例#30
0
 public static double AsFloat64(this CastSource <string> source, double defaultValue = default(double))
 {
     return(source.AsDouble(defaultValue));
 }