12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class JsonManager : MonoBehaviour
- {
- public const string TAG = "JsonManager";
- private static JsonManager m_Instance;
- public static JsonManager Instance
- {
- get
- {
- if (null == JsonManager.m_Instance)
- {
- GameObject gameObject = GameObject.Find("JsonManager");
- if (null == gameObject)
- {
- gameObject = new GameObject("JsonManager");
- gameObject.transform.localPosition = new Vector3(0f, 0f, 0f);
- DontDestroyOnLoad(gameObject);
- }
- m_Instance = gameObject.AddComponent<JsonManager>();
- }
- return m_Instance;
- }
- }
- //public UdpClient UdpClient
- //{
- // get
- // {
- // if (null == m_UdpClient)
- // {
- // m_UdpClient = GameObject.Find("Socket").GetComponent<UdpClient>();
- // }
- // return m_UdpClient;
- // }
- // set { m_UdpClient = value; }
- //}
- /// <summary>
- /// 解析从服务器接收到的数据
- /// 根据解析的数据有多个泛型解析方法
- /// </summary>
- /// <param name="jsonData">要解析的数据</param>
- /// <returns>是否解析成功</returns>
- public bool AnalysisJson(string jsonData)
- {
- if (jsonData == "" || jsonData == "Message")
- {
- Debug.LogError("未接受到json 数据");
- return false;
- }
- try
- {
- //根据泛型进行解析
- QuestionInfor infor = JsonMapper.ToObject<QuestionInfor>(jsonData);
- return true;
- }
- catch (Exception)
- {
- Debug.LogError("Json 传入格式不正确 无法解析");
- return false;
- }
- }
- }
|