暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleJSONUnity.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #if UNITY_5_3_OR_NEWER
  2. #region License and information
  3. /* * * * *
  4. *
  5. * Unity extension for the SimpleJSON framework. It does only work together with
  6. * the SimpleJSON.cs
  7. * It provides several helpers and conversion operators to serialize/deserialize
  8. * common Unity types such as Vector2/3/4, Rect, RectOffset, Quaternion and
  9. * Matrix4x4 as JSONObject or JSONArray.
  10. * This extension will add 3 static settings to the JSONNode class:
  11. * ( VectorContainerType, QuaternionContainerType, RectContainerType ) which
  12. * control what node type should be used for serializing the given type. So a
  13. * Vector3 as array would look like [12,32,24] and {"x":12, "y":32, "z":24} as
  14. * object.
  15. *
  16. *
  17. * The MIT License (MIT)
  18. *
  19. * Copyright (c) 2012-2017 Markus Göbel (Bunny83)
  20. *
  21. * Permission is hereby granted, free of charge, to any person obtaining a copy
  22. * of this software and associated documentation files (the "Software"), to deal
  23. * in the Software without restriction, including without limitation the rights
  24. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. * copies of the Software, and to permit persons to whom the Software is
  26. * furnished to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in all
  29. * copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  37. * SOFTWARE.
  38. *
  39. * * * * */
  40. #endregion License and information
  41. using UnityEngine;
  42. namespace SimpleJSON
  43. {
  44. public enum JSONContainerType { Array, Object }
  45. public partial class JSONNode
  46. {
  47. public static byte Color32DefaultAlpha = 255;
  48. public static float ColorDefaultAlpha = 1f;
  49. public static JSONContainerType VectorContainerType = JSONContainerType.Array;
  50. public static JSONContainerType QuaternionContainerType = JSONContainerType.Array;
  51. public static JSONContainerType RectContainerType = JSONContainerType.Array;
  52. public static JSONContainerType ColorContainerType = JSONContainerType.Array;
  53. private static JSONNode GetContainer(JSONContainerType aType)
  54. {
  55. if (aType == JSONContainerType.Array)
  56. return new JSONArray();
  57. return new JSONObject();
  58. }
  59. #region implicit conversion operators
  60. public static implicit operator JSONNode(Vector2 aVec)
  61. {
  62. JSONNode n = GetContainer(VectorContainerType);
  63. n.WriteVector2(aVec);
  64. return n;
  65. }
  66. public static implicit operator JSONNode(Vector3 aVec)
  67. {
  68. JSONNode n = GetContainer(VectorContainerType);
  69. n.WriteVector3(aVec);
  70. return n;
  71. }
  72. public static implicit operator JSONNode(Vector4 aVec)
  73. {
  74. JSONNode n = GetContainer(VectorContainerType);
  75. n.WriteVector4(aVec);
  76. return n;
  77. }
  78. public static implicit operator JSONNode(Color aCol)
  79. {
  80. JSONNode n = GetContainer(ColorContainerType);
  81. n.WriteColor(aCol);
  82. return n;
  83. }
  84. public static implicit operator JSONNode(Color32 aCol)
  85. {
  86. JSONNode n = GetContainer(ColorContainerType);
  87. n.WriteColor32(aCol);
  88. return n;
  89. }
  90. public static implicit operator JSONNode(Quaternion aRot)
  91. {
  92. JSONNode n = GetContainer(QuaternionContainerType);
  93. n.WriteQuaternion(aRot);
  94. return n;
  95. }
  96. public static implicit operator JSONNode(Rect aRect)
  97. {
  98. JSONNode n = GetContainer(RectContainerType);
  99. n.WriteRect(aRect);
  100. return n;
  101. }
  102. public static implicit operator JSONNode(RectOffset aRect)
  103. {
  104. JSONNode n = GetContainer(RectContainerType);
  105. n.WriteRectOffset(aRect);
  106. return n;
  107. }
  108. public static implicit operator Vector2(JSONNode aNode)
  109. {
  110. return aNode.ReadVector2();
  111. }
  112. public static implicit operator Vector3(JSONNode aNode)
  113. {
  114. return aNode.ReadVector3();
  115. }
  116. public static implicit operator Vector4(JSONNode aNode)
  117. {
  118. return aNode.ReadVector4();
  119. }
  120. public static implicit operator Color(JSONNode aNode)
  121. {
  122. return aNode.ReadColor();
  123. }
  124. public static implicit operator Color32(JSONNode aNode)
  125. {
  126. return aNode.ReadColor32();
  127. }
  128. public static implicit operator Quaternion(JSONNode aNode)
  129. {
  130. return aNode.ReadQuaternion();
  131. }
  132. public static implicit operator Rect(JSONNode aNode)
  133. {
  134. return aNode.ReadRect();
  135. }
  136. public static implicit operator RectOffset(JSONNode aNode)
  137. {
  138. return aNode.ReadRectOffset();
  139. }
  140. #endregion implicit conversion operators
  141. #region Vector2
  142. public Vector2 ReadVector2(Vector2 aDefault)
  143. {
  144. if (IsObject)
  145. return new Vector2(this["x"].AsFloat, this["y"].AsFloat);
  146. if (IsArray)
  147. return new Vector2(this[0].AsFloat, this[1].AsFloat);
  148. return aDefault;
  149. }
  150. public Vector2 ReadVector2(string aXName, string aYName)
  151. {
  152. if (IsObject)
  153. {
  154. return new Vector2(this[aXName].AsFloat, this[aYName].AsFloat);
  155. }
  156. return Vector2.zero;
  157. }
  158. public Vector2 ReadVector2()
  159. {
  160. return ReadVector2(Vector2.zero);
  161. }
  162. public JSONNode WriteVector2(Vector2 aVec, string aXName = "x", string aYName = "y")
  163. {
  164. if (IsObject)
  165. {
  166. Inline = true;
  167. this[aXName].AsFloat = aVec.x;
  168. this[aYName].AsFloat = aVec.y;
  169. }
  170. else if (IsArray)
  171. {
  172. Inline = true;
  173. this[0].AsFloat = aVec.x;
  174. this[1].AsFloat = aVec.y;
  175. }
  176. return this;
  177. }
  178. #endregion Vector2
  179. #region Vector3
  180. public Vector3 ReadVector3(Vector3 aDefault)
  181. {
  182. if (IsObject)
  183. return new Vector3(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat);
  184. if (IsArray)
  185. return new Vector3(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat);
  186. return aDefault;
  187. }
  188. public Vector3 ReadVector3(string aXName, string aYName, string aZName)
  189. {
  190. if (IsObject)
  191. return new Vector3(this[aXName].AsFloat, this[aYName].AsFloat, this[aZName].AsFloat);
  192. return Vector3.zero;
  193. }
  194. public Vector3 ReadVector3()
  195. {
  196. return ReadVector3(Vector3.zero);
  197. }
  198. public JSONNode WriteVector3(Vector3 aVec, string aXName = "x", string aYName = "y", string aZName = "z")
  199. {
  200. if (IsObject)
  201. {
  202. Inline = true;
  203. this[aXName].AsFloat = aVec.x;
  204. this[aYName].AsFloat = aVec.y;
  205. this[aZName].AsFloat = aVec.z;
  206. }
  207. else if (IsArray)
  208. {
  209. Inline = true;
  210. this[0].AsFloat = aVec.x;
  211. this[1].AsFloat = aVec.y;
  212. this[2].AsFloat = aVec.z;
  213. }
  214. return this;
  215. }
  216. #endregion Vector3
  217. #region Vector4
  218. public Vector4 ReadVector4(Vector4 aDefault)
  219. {
  220. if (IsObject)
  221. return new Vector4(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  222. if (IsArray)
  223. return new Vector4(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  224. return aDefault;
  225. }
  226. public Vector4 ReadVector4()
  227. {
  228. return ReadVector4(Vector4.zero);
  229. }
  230. public JSONNode WriteVector4(Vector4 aVec)
  231. {
  232. if (IsObject)
  233. {
  234. Inline = true;
  235. this["x"].AsFloat = aVec.x;
  236. this["y"].AsFloat = aVec.y;
  237. this["z"].AsFloat = aVec.z;
  238. this["w"].AsFloat = aVec.w;
  239. }
  240. else if (IsArray)
  241. {
  242. Inline = true;
  243. this[0].AsFloat = aVec.x;
  244. this[1].AsFloat = aVec.y;
  245. this[2].AsFloat = aVec.z;
  246. this[3].AsFloat = aVec.w;
  247. }
  248. return this;
  249. }
  250. #endregion Vector4
  251. #region Color / Color32
  252. public Color ReadColor(Color aDefault)
  253. {
  254. if (IsObject)
  255. return new Color(this["r"].AsFloat, this["g"].AsFloat, this["b"].AsFloat, HasKey("a")?this["a"].AsFloat:ColorDefaultAlpha);
  256. if (IsArray)
  257. return new Color(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, (Count>3)?this[3].AsFloat:ColorDefaultAlpha);
  258. return aDefault;
  259. }
  260. public Color ReadColor()
  261. {
  262. return ReadColor(Color.clear);
  263. }
  264. public JSONNode WriteColor(Color aCol)
  265. {
  266. if (IsObject)
  267. {
  268. Inline = true;
  269. this["r"].AsFloat = aCol.r;
  270. this["g"].AsFloat = aCol.g;
  271. this["b"].AsFloat = aCol.b;
  272. this["a"].AsFloat = aCol.a;
  273. }
  274. else if (IsArray)
  275. {
  276. Inline = true;
  277. this[0].AsFloat = aCol.r;
  278. this[1].AsFloat = aCol.g;
  279. this[2].AsFloat = aCol.b;
  280. this[3].AsFloat = aCol.a;
  281. }
  282. return this;
  283. }
  284. public Color32 ReadColor32(Color32 aDefault)
  285. {
  286. if (IsObject)
  287. return new Color32((byte)this["r"].AsInt, (byte)this["g"].AsInt, (byte)this["b"].AsInt, (byte)(HasKey("a")?this["a"].AsInt:Color32DefaultAlpha));
  288. if (IsArray)
  289. return new Color32((byte)this[0].AsInt, (byte)this[1].AsInt, (byte)this[2].AsInt, (byte)((Count>3)?this[3].AsInt:Color32DefaultAlpha));
  290. return aDefault;
  291. }
  292. public Color32 ReadColor32()
  293. {
  294. return ReadColor32(new Color32());
  295. }
  296. public JSONNode WriteColor32(Color32 aCol)
  297. {
  298. if (IsObject)
  299. {
  300. Inline = true;
  301. this["r"].AsInt = aCol.r;
  302. this["g"].AsInt = aCol.g;
  303. this["b"].AsInt = aCol.b;
  304. this["a"].AsInt = aCol.a;
  305. }
  306. else if (IsArray)
  307. {
  308. Inline = true;
  309. this[0].AsInt = aCol.r;
  310. this[1].AsInt = aCol.g;
  311. this[2].AsInt = aCol.b;
  312. this[3].AsInt = aCol.a;
  313. }
  314. return this;
  315. }
  316. #endregion Color / Color32
  317. #region Quaternion
  318. public Quaternion ReadQuaternion(Quaternion aDefault)
  319. {
  320. if (IsObject)
  321. return new Quaternion(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
  322. if (IsArray)
  323. return new Quaternion(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  324. return aDefault;
  325. }
  326. public Quaternion ReadQuaternion()
  327. {
  328. return ReadQuaternion(Quaternion.identity);
  329. }
  330. public JSONNode WriteQuaternion(Quaternion aRot)
  331. {
  332. if (IsObject)
  333. {
  334. Inline = true;
  335. this["x"].AsFloat = aRot.x;
  336. this["y"].AsFloat = aRot.y;
  337. this["z"].AsFloat = aRot.z;
  338. this["w"].AsFloat = aRot.w;
  339. }
  340. else if (IsArray)
  341. {
  342. Inline = true;
  343. this[0].AsFloat = aRot.x;
  344. this[1].AsFloat = aRot.y;
  345. this[2].AsFloat = aRot.z;
  346. this[3].AsFloat = aRot.w;
  347. }
  348. return this;
  349. }
  350. #endregion Quaternion
  351. #region Rect
  352. public Rect ReadRect(Rect aDefault)
  353. {
  354. if (IsObject)
  355. return new Rect(this["x"].AsFloat, this["y"].AsFloat, this["width"].AsFloat, this["height"].AsFloat);
  356. if (IsArray)
  357. return new Rect(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
  358. return aDefault;
  359. }
  360. public Rect ReadRect()
  361. {
  362. return ReadRect(new Rect());
  363. }
  364. public JSONNode WriteRect(Rect aRect)
  365. {
  366. if (IsObject)
  367. {
  368. Inline = true;
  369. this["x"].AsFloat = aRect.x;
  370. this["y"].AsFloat = aRect.y;
  371. this["width"].AsFloat = aRect.width;
  372. this["height"].AsFloat = aRect.height;
  373. }
  374. else if (IsArray)
  375. {
  376. Inline = true;
  377. this[0].AsFloat = aRect.x;
  378. this[1].AsFloat = aRect.y;
  379. this[2].AsFloat = aRect.width;
  380. this[3].AsFloat = aRect.height;
  381. }
  382. return this;
  383. }
  384. #endregion Rect
  385. #region RectOffset
  386. public RectOffset ReadRectOffset(RectOffset aDefault)
  387. {
  388. if (this is JSONObject)
  389. return new RectOffset(this["left"].AsInt, this["right"].AsInt, this["top"].AsInt, this["bottom"].AsInt);
  390. if (this is JSONArray)
  391. return new RectOffset(this[0].AsInt, this[1].AsInt, this[2].AsInt, this[3].AsInt);
  392. return aDefault;
  393. }
  394. public RectOffset ReadRectOffset()
  395. {
  396. return ReadRectOffset(new RectOffset());
  397. }
  398. public JSONNode WriteRectOffset(RectOffset aRect)
  399. {
  400. if (IsObject)
  401. {
  402. Inline = true;
  403. this["left"].AsInt = aRect.left;
  404. this["right"].AsInt = aRect.right;
  405. this["top"].AsInt = aRect.top;
  406. this["bottom"].AsInt = aRect.bottom;
  407. }
  408. else if (IsArray)
  409. {
  410. Inline = true;
  411. this[0].AsInt = aRect.left;
  412. this[1].AsInt = aRect.right;
  413. this[2].AsInt = aRect.top;
  414. this[3].AsInt = aRect.bottom;
  415. }
  416. return this;
  417. }
  418. #endregion RectOffset
  419. #region Matrix4x4
  420. public Matrix4x4 ReadMatrix()
  421. {
  422. Matrix4x4 result = Matrix4x4.identity;
  423. if (IsArray)
  424. {
  425. for (int i = 0; i < 16; i++)
  426. {
  427. result[i] = this[i].AsFloat;
  428. }
  429. }
  430. return result;
  431. }
  432. public JSONNode WriteMatrix(Matrix4x4 aMatrix)
  433. {
  434. if (IsArray)
  435. {
  436. Inline = true;
  437. for (int i = 0; i < 16; i++)
  438. {
  439. this[i].AsFloat = aMatrix[i];
  440. }
  441. }
  442. return this;
  443. }
  444. #endregion Matrix4x4
  445. }
  446. }
  447. #endif