diff options
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Common/CommonParse.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Common/CommonParse.cs | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Common/CommonParse.cs b/WorldlineKeepers/Assets/Scripts/Common/CommonParse.cs new file mode 100644 index 0000000..6d06eff --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Common/CommonParse.cs @@ -0,0 +1,317 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// author: hanjun +// date: 2018/12/03 16:33:35 +// desc: 某类 +// +// +// +// +// +///////////////////////////////////////////////////////////////////////////////// + +using UnityEngine; +using System.Collections; +using System; +using LitJson; + +namespace DNA +{ + public class CommonParse + { + public static float ParseFloat(string str) + { + float target = 0f; + ParseFloat(ref target, str, 0f); + return target; + } + public static void ParseFloat(ref float target, string str) + { + ParseFloat(ref target, str, 0f); + } + public static void ParseFloat(ref float target, string str, float defaultValue) + { + if (string.IsNullOrEmpty(str)) + { + target = defaultValue; + return; + } + str = str.Replace("(", ""); + str = str.Replace(")", ""); + float result = defaultValue;//使用默认值 + if (float.TryParse(str, out result)) + { + target = result; + } + else + { + LogHelper.LogEditorError("ParseFloat Error at field : " + str); + target = defaultValue; + } + } + public static int ParseInt(string str) + { + int target = 0; + ParseInt(ref target, str, 0); + return target; + } + public static void ParseInt(ref int target, string str) + { + ParseInt(ref target, str, 0); + } + public static void ParseInt(ref int target, string str, int defaultValue) + { + if (string.IsNullOrEmpty(str)) + { + target = defaultValue; + return; + } + + int result = defaultValue;//使用默认值 + if (int.TryParse(str, out result)) + { + target = result; + } + else + { + if (str.Contains("0x")) + { + target = Convert.ToInt32(str, 16); + } + else + { + LogHelper.LogEditorError("ParseInt Error at field : " + str); + target = defaultValue; + } + } + } + public static byte ParseByte(string str) + { + byte target = 0; + ParseByte(ref target, str, 0); + return target; + } + public static void ParseByte(ref byte target, string str) + { + ParseByte(ref target, str, 0); + } + public static void ParseByte(ref byte target, string str, byte defaultValue) + { + if (string.IsNullOrEmpty(str)) + { + target = defaultValue; + return; + } + + byte result = defaultValue;//使用默认值 + if (byte.TryParse(str, out result)) + { + target = result; + } + else + { + if (str.Contains("0x")) + { + target = Convert.ToByte(str, 16); + } + else + { + LogHelper.LogEditorError("ParseByte Error at field : " + str); + target = defaultValue; + } + } + } + + public static void ParseVector2(ref Vector2 v2, string str) + { + if (string.IsNullOrEmpty(str)) + { + v2 = Vector2.zero; + return; + } + + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseFloat(ref v2.x, array[0]); + if (array.Length >= 2) + ParseFloat(ref v2.y, array[1]); + + //if (array.Length == 2) + //{ + // ParseFloat(ref v2.x, array[0]); + // ParseFloat(ref v2.y, array[1]); + //} + //else + //{ + // LogHelper.LogError("解析ParseVector2失败," + str); + //} + } + public static void ParseVector3(ref Vector3 v3, string str) + { + if (string.IsNullOrEmpty(str)) + { + v3 = Vector3.zero; + return; + } + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseFloat(ref v3.x, array[0]); + if (array.Length >= 2) + ParseFloat(ref v3.y, array[1]); + if (array.Length >= 3) + ParseFloat(ref v3.z, array[2]); + + //if (array.Length == 3) + //{ + // ParseFloat(ref v3.x, array[0]); + // ParseFloat(ref v3.y, array[1]); + // ParseFloat(ref v3.z, array[2]); + //} + //else + //{ + // LogHelper.LogError("解析ParseVector3失败," + str); + //} + } + public static void ParseVector4(ref Vector4 v4, string str) + { + if (string.IsNullOrEmpty(str)) + { + v4 = Vector4.zero; + return; + } + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseFloat(ref v4.x, array[0]); + if (array.Length >= 2) + ParseFloat(ref v4.y, array[1]); + if (array.Length >= 3) + ParseFloat(ref v4.z, array[2]); + if (array.Length >= 4) + ParseFloat(ref v4.w, array[3]); + + //if (array.Length == 4) + //{ + // ParseFloat(ref v4.x, array[0]); + // ParseFloat(ref v4.y, array[1]); + // ParseFloat(ref v4.z, array[2]); + // ParseFloat(ref v4.w, array[3]); + //} + //else + //{ + // LogHelper.LogError("解析ParseVector4失败," + str); + //} + } + + #region Json序列化 + public static Vector3 ParseVector4(string str) + { + Vector4 v = Vector4.zero; + ParseVector4(ref v, str); + return v; + } + + public static Vector3 ParseVector3(string str) + { + Vector3 v = Vector3.zero; + ParseVector3(ref v, str); + return v; + } + + public static Vector2 ParseVector2(string str) + { + Vector2 v = Vector2.zero; + ParseVector2(ref v, str); + return v; + } + + public static IntVector2 ParseIntVector2(string str) + { + IntVector2 iv2 = IntVector2.Zero; + if (string.IsNullOrEmpty(str)) + { + return iv2; + } + + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseInt(ref iv2.x, array[0]); + if (array.Length >= 2) + ParseInt(ref iv2.y, array[1]); + + return iv2; + } + + public static IntVector3 ParseIntVector3(string str) + { + IntVector3 iv3 = IntVector3.Zero; + if (string.IsNullOrEmpty(str)) + { + return iv3; + } + + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseInt(ref iv3.x, array[0]); + if (array.Length >= 2) + ParseInt(ref iv3.y, array[1]); + if (array.Length >= 3) + ParseInt(ref iv3.z, array[2]); + + return iv3; + } + + public static IntVector4 ParseIntVector4(string str) + { + IntVector4 iv4 = IntVector4.Zero; + if (string.IsNullOrEmpty(str)) + { + return iv4; + } + + string[] array = str.Split(',', ';', '='); + if (array.Length >= 1) + ParseInt(ref iv4.x, array[0]); + if (array.Length >= 2) + ParseInt(ref iv4.y, array[1]); + if (array.Length >= 3) + ParseInt(ref iv4.z, array[2]); + if (array.Length >= 4) + ParseInt(ref iv4.w, array[3]); + return iv4; + } + + public static Color ParseColor(string str) + { + Color col = Color.white; + string[] array = str.Split(',', ';', '='); + int i = array.Length; + if (i > 0) col.r = ParseFloat(array[0]); + if (i > 1) col.g = ParseFloat(array[1]); + if (i > 2) col.b = ParseFloat(array[2]); + if (i > 3) col.a = ParseFloat(array[3]); + return col; + } + + public static Color32 ParseColor32(string str) + { + Color32 col = new Color32(255,255,255,255); + string[] array = str.Split(',', ';', '='); + int i = array.Length; + if (i > 0) col.r = ParseByte(array[0]); + if (i > 1) col.g = ParseByte(array[1]); + if (i > 2) col.b = ParseByte(array[2]); + if (i > 3) col.a = ParseByte(array[3]); + return col; + } + #endregion + + public static string ToJsonByFormat(System.Object obj) + { + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + JsonWriter jw = new JsonWriter(sb); + jw.PrettyPrint = true; + JsonMapper.ToJson(obj, jw); + return sb.ToString(); + } + } +}
\ No newline at end of file |