1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/////////////////////////////////////////////////////////////////////////////////
//
// author: hanjun
// date: 2018/12/03 16:33:35
// desc: 某类
//
//
//
//
//
/////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
using System;
using LitJson;
namespace WK
{
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();
}
}
}
|