123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LitJson;
- using System;
- using UnityEngine.SceneManagement;
- public class NetWorkManager : MonoBehaviour {
- private List<Network> queue;
- private static bool init;
- public static NetWorkManager instance;
- void Awake()
- {
- if (!init) {
- init = true;
- instance = this;
- queue = new List<Network> ();
- DontDestroyOnLoad (this.gameObject);
- } else {
- Destroy (this.gameObject);
- return;
- }
- }
- // Use this for initialization
- void Start () {
- // HotPool.getHotList ();
- // List<int> i = new List<int> ();
- // i.Add (1);
- // i.Add (2);
- // string s = JsonUtility.ToJson (i);
- // Debug.Log (s);
- }
- void checkQueue()
- {
- Debug.Log ("networkQueueCount: " + queue.Count);
- if (queue.Count == 0) {
- //Debug.Log ("networkQueue: " + 0);
- return;
- }
- if (queue [0].active) {
- //Debug.Log ("networkActive: " + queue[0].action);
- return;
- }
- StartCoroutine (post(queue[0]));
- }
- IEnumerator post(Network net)
- {
- yield return null;
- /*
- //Debug.Log ("networkPost");
- net.active = true;
- string url = net.action;
- // string url = NetworkConfiguration.url + "?s=index/index1/index";
- Dictionary<string, string> form = net.data;
- //UserInfo.Token = "f8009f01eb9b17e24aa57bf86aaadda9";
- //form.AddField ("user_token",UserInfo.Token);
- Debug.Log ("token===>"+UserInfo.Token);
- //form.AddField ("app_id", UserInfo.AppID);
- // form.AddField ("param", net.data);
- WWW www = new WWW (url,form);
- Debug.Log("url: " + www.url);
- Debug.Log("param: " + net.data);
- yield return www;//等待Web服务器的反应
- string result = string.Empty;
- if (www.error != null) {
- result = www.error;
- Debug.Log ("networkError: " + result);
- postError ("1009");
- } else {
- result = www.text;
- Debug.Log ("networkSuccess: " + result);
- //txt.text = result;
- postSuccess (result);
- }
- queue.RemoveAt (0);
- checkQueue ();*/
- }
- private TextMesh txt;
- public void getNetTexture(string action,WWWForm data,Action<Material> success = null)
- {
- StartCoroutine (LoadTextureFromLocal(action,data,success));
- }
- //读取纹理
- IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action<Material> success = null)
- {
- Debug.Log ("tupian123................"+action);
- WWW w;
- if(data!=null)
- w = new WWW (action,data);
- else
- w = new WWW (action);
- yield return w;
- if (w.isDone) {
- Material material = new Material (Shader.Find ("Unlit/Texture"));
- Debug.Log ("tupian321................");
- material.mainTexture = w.texture;
- if(success!=null)
- success (material);
- }
- }
- void postSuccess(string postData)
- {
- JsonData data = null;
- string code = "";
- try
- {
- data = JsonMapper.ToObject (postData);
- //UserInfo.Token = data["token"].ToString();
- code = data ["code"].ToString();
- }
- catch(Exception e)
- {
- Debug.Log (e.Data);
- postError ("1009");
- }
- switch (code) {
- case "200":
- queue [0].scuess (data);
- break;
- default:
- Debug.Log("22222222222222");
- if (data ["message"].ToString () == "vaild account") {
- PlayerPrefs.SetString (UserInfo.Account,"");
- }
- queue [0].failed (postData);
- break;
- }
- }
- void postError(string errorData)
- {
- queue [0].error (errorData);
- }
- /// <summary>
- /// Adds the network.
- /// </summary>
- /// <param name="action">Action.</param>
- /// <param name="data">Data.</param>
- /// <param name="type">Type.</param>
- /// <param name="successFun">Success fun.</param>
- /// <param name="failedFun">Failed fun.</param>
- public void addNetwork(string action, JsonData data,Action<JsonData> successFun = null ,Action<string> failedFun = null)
- {
- Network net = new Network ();
- net.init (action,data,successFun,failedFun);
- queue.Add (net);
- checkQueue ();
- }
- public void netWorkComplete()
- {
- netWorkComplete ();
- }
-
- public static void TextureLoad(string session,Action<Material> success = null)
- {
- WWWForm data = new WWWForm ();
- data.AddField("session_id", session);
- string action = NetworkConfiguration.get_code_png_url;
- NetWorkManager.instance.getNetTexture (action,data,success);
- }
-
- }
|