NetWorkManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using System;
  6. using UnityEngine.SceneManagement;
  7. public class NetWorkManager : MonoBehaviour {
  8. private List<Network> queue;
  9. private static bool init;
  10. public static NetWorkManager instance;
  11. void Awake()
  12. {
  13. if (!init) {
  14. init = true;
  15. instance = this;
  16. queue = new List<Network> ();
  17. DontDestroyOnLoad (this.gameObject);
  18. } else {
  19. Destroy (this.gameObject);
  20. return;
  21. }
  22. }
  23. // Use this for initialization
  24. void Start () {
  25. // HotPool.getHotList ();
  26. // List<int> i = new List<int> ();
  27. // i.Add (1);
  28. // i.Add (2);
  29. // string s = JsonUtility.ToJson (i);
  30. // Debug.Log (s);
  31. }
  32. void checkQueue()
  33. {
  34. Debug.Log ("networkQueueCount: " + queue.Count);
  35. if (queue.Count == 0) {
  36. //Debug.Log ("networkQueue: " + 0);
  37. return;
  38. }
  39. if (queue [0].active) {
  40. //Debug.Log ("networkActive: " + queue[0].action);
  41. return;
  42. }
  43. StartCoroutine (post(queue[0]));
  44. }
  45. IEnumerator post(Network net)
  46. {
  47. yield return null;
  48. /*
  49. //Debug.Log ("networkPost");
  50. net.active = true;
  51. string url = net.action;
  52. // string url = NetworkConfiguration.url + "?s=index/index1/index";
  53. Dictionary<string, string> form = net.data;
  54. //UserInfo.Token = "f8009f01eb9b17e24aa57bf86aaadda9";
  55. //form.AddField ("user_token",UserInfo.Token);
  56. Debug.Log ("token===>"+UserInfo.Token);
  57. //form.AddField ("app_id", UserInfo.AppID);
  58. // form.AddField ("param", net.data);
  59. WWW www = new WWW (url,form);
  60. Debug.Log("url: " + www.url);
  61. Debug.Log("param: " + net.data);
  62. yield return www;//等待Web服务器的反应
  63. string result = string.Empty;
  64. if (www.error != null) {
  65. result = www.error;
  66. Debug.Log ("networkError: " + result);
  67. postError ("1009");
  68. } else {
  69. result = www.text;
  70. Debug.Log ("networkSuccess: " + result);
  71. //txt.text = result;
  72. postSuccess (result);
  73. }
  74. queue.RemoveAt (0);
  75. checkQueue ();*/
  76. }
  77. private TextMesh txt;
  78. public void getNetTexture(string action,WWWForm data,Action<Material> success = null)
  79. {
  80. StartCoroutine (LoadTextureFromLocal(action,data,success));
  81. }
  82. //读取纹理
  83. IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action<Material> success = null)
  84. {
  85. Debug.Log ("tupian123................"+action);
  86. WWW w;
  87. if(data!=null)
  88. w = new WWW (action,data);
  89. else
  90. w = new WWW (action);
  91. yield return w;
  92. if (w.isDone) {
  93. Material material = new Material (Shader.Find ("Unlit/Texture"));
  94. Debug.Log ("tupian321................");
  95. material.mainTexture = w.texture;
  96. if(success!=null)
  97. success (material);
  98. }
  99. }
  100. void postSuccess(string postData)
  101. {
  102. JsonData data = null;
  103. string code = "";
  104. try
  105. {
  106. data = JsonMapper.ToObject (postData);
  107. //UserInfo.Token = data["token"].ToString();
  108. code = data ["code"].ToString();
  109. }
  110. catch(Exception e)
  111. {
  112. Debug.Log (e.Data);
  113. postError ("1009");
  114. }
  115. switch (code) {
  116. case "200":
  117. queue [0].scuess (data);
  118. break;
  119. default:
  120. Debug.Log("22222222222222");
  121. if (data ["message"].ToString () == "vaild account") {
  122. PlayerPrefs.SetString (UserInfo.Account,"");
  123. }
  124. queue [0].failed (postData);
  125. break;
  126. }
  127. }
  128. void postError(string errorData)
  129. {
  130. queue [0].error (errorData);
  131. }
  132. /// <summary>
  133. /// Adds the network.
  134. /// </summary>
  135. /// <param name="action">Action.</param>
  136. /// <param name="data">Data.</param>
  137. /// <param name="type">Type.</param>
  138. /// <param name="successFun">Success fun.</param>
  139. /// <param name="failedFun">Failed fun.</param>
  140. public void addNetwork(string action, JsonData data,Action<JsonData> successFun = null ,Action<string> failedFun = null)
  141. {
  142. Network net = new Network ();
  143. net.init (action,data,successFun,failedFun);
  144. queue.Add (net);
  145. checkQueue ();
  146. }
  147. public void netWorkComplete()
  148. {
  149. netWorkComplete ();
  150. }
  151. public static void TextureLoad(string session,Action<Material> success = null)
  152. {
  153. WWWForm data = new WWWForm ();
  154. data.AddField("session_id", session);
  155. string action = NetworkConfiguration.get_code_png_url;
  156. NetWorkManager.instance.getNetTexture (action,data,success);
  157. }
  158. }