using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System; using UnityEngine.SceneManagement; public class NetWorkManager : MonoBehaviour { private List queue; private static bool init; public static NetWorkManager instance; void Awake() { if (!init) { init = true; instance = this; queue = new List (); DontDestroyOnLoad (this.gameObject); } else { Destroy (this.gameObject); return; } } // Use this for initialization void Start () { // HotPool.getHotList (); // List i = new List (); // 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) { //Debug.Log ("networkPost"); net.active = true; string url = net.action; // string url = NetworkConfiguration.url + "?s=index/index1/index"; WWWForm 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 success = null) { StartCoroutine (LoadTextureFromLocal(action,data,success)); } //读取纹理 IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action 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); } /// /// Adds the network. /// /// Action. /// Data. /// Type. /// Success fun. /// Failed fun. public void addNetwork(string action, WWWForm data,Action successFun = null ,Action failedFun = null) { Network net = new Network (); net.init (action,data,successFun,failedFun); queue.Add (net); checkQueue (); } public void netWorkComplete() { netWorkComplete (); } public static void GetUser(string page, string limit, Action success = null, Action failed = null) { } public static void getYuanChenToken(string token, Action success = null, Action failed = null) { } public static void SearchUser(string searchID, Action success = null, Action failed = null) { } /// /// 登录 /// /// Identifier. /// Pas. /// Success. /// Failed. public static void login(string userName,string passWord,string code,string session,Action success = null,Action failed = null) { } public static void getPicList(string token, Action success = null, Action failed = null) { } public static void getSession(Action success = null,Action failed = null) { } public static void TextureLoad(string session,Action success = null) { } public static void login_with_token(string token,Action success = null,Action failed = null) { } public static void updateXMl(string xmlName, byte[] info, Action success = null, Action failed = null) { WWWForm data = new WWWForm(); data.AddField("file_name", xmlName); data.AddBinaryData("file_body", info); string action = "api/upload_file"; NetWorkManager.instance.addNetwork(action, data, success, failed); } }