using LitJson; using System; using System.Collections; using System.Collections.Generic; using TestHwnd; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class GameManager : MonoBehaviour { public static GameManager Instance = null; public Transform chakongParent; private GameObject[] chakongPoints; [Header("外网获取地址")] public string GetUrl; public string GetMiMaUrl; [Header("本地获取地址")] public string GetLocalUrl; public string GetMiMaLocalUrl; public CheckDarg[] checkDargs; public bool isLocal = false; public Transform KaiJiBangParent, ZhuRuQiParent, ShouQiBangParent, SheBeiParent; private void Awake() { if (Instance == null) { Instance = this; } } private void Start() { CreatChaKongUI(); CreatItem(SheBeiType.设备); CreatItem(SheBeiType.注入器); CreatItem(SheBeiType.开机棒); CreatItem(SheBeiType.受启棒); // StartCoroutine( Get()); } private void CreatChaKongUI() { chakongPoints = GameObject.FindGameObjectsWithTag("ChaKong"); checkDargs = new CheckDarg[chakongPoints.Length]; for (int i = 0; i < chakongPoints.Length; i++) { ChaKong chaKong = chakongPoints[i].GetComponent(); if (chaKong != null) { Vector2 pos = Camera.main.WorldToScreenPoint(chakongPoints[i].transform.position); GameObject ui = Resources.Load("Prefab/chakong"); GameObject go = GameObject.Instantiate(ui, chakongParent); go.GetComponent().ChaKongPos = chakongPoints[i].transform.position; go.GetComponent().ChaKongType = chaKong.SheBeiType; go.transform.position = pos; checkDargs[i] = go.GetComponent(); } } } public void CreatItem(SheBeiType sheBeiType) { StartCoroutine( InitItem(sheBeiType)); } public IEnumerator InitItem(SheBeiType sheBeiType) { int type=1; switch (sheBeiType) { case SheBeiType.受启棒: //item.SheBeiType = SheBeiType.受启棒; type = 3; break; case SheBeiType.开机棒: //item.SheBeiType = SheBeiType.开机棒; type = 2; break; case SheBeiType.注入器: //item.SheBeiType = SheBeiType.注入器; type = 1; break; case SheBeiType.设备: // item.SheBeiType = SheBeiType.设备; type = 1; break; } UnityWebRequest request; if (sheBeiType == SheBeiType.设备) { if (isLocal) { request = UnityWebRequest.Get(GetMiMaLocalUrl); } else { request = UnityWebRequest.Get(GetMiMaUrl); } } else { if (isLocal) { request = UnityWebRequest.Get(GetLocalUrl + type.ToString() + "?u3dType="); //request = UnityWebRequest.Get(GetLocalUrl + dragItem.type.ToString() + "{u3dType}"); } else { request = UnityWebRequest.Get(GetUrl + type.ToString() + "?u3dType="); //request = UnityWebRequest.Get(GetUrl + dragItem.type.ToString() + "{u3dType}"); } } yield return request.SendWebRequest(); if (request.isHttpError || request.isNetworkError) { Debug.LogError(request.error); } else { //获取设备deviceId string receiveContent = request.downloadHandler.text; JsonData jsonData = JsonMapper.ToObject(receiveContent); JsonData arrdata; if (sheBeiType == SheBeiType.设备) { arrdata = jsonData["data"][0]; } else { arrdata = jsonData["data"][0]["list"]; } string str; for (int i = 0; i < arrdata.Count; i++) { if (sheBeiType == SheBeiType.设备) { //根据数据数量生成设备 GameObject go = GameObject.Instantiate(Resources.Load("Prefab/Item")); DragItemH dragItem = go.AddComponent(); dragItem.SheBeiType = sheBeiType; //UI图片设置 go.transform.SetParent(SheBeiParent); go.GetComponent().sprite = Resources.Load("image/yuankong"); //数据获取 dragItem.type = type; string ID = arrdata[i]["id"].ToString(); dragItem.id = ID; str = arrdata[i]["deviceId"].ToString(); dragItem.SheBeiHao = str; string name = arrdata[i]["name"].ToString(); dragItem.Name = name; //获取设备的Json数据 JsonWriter writer = new JsonWriter(); writer.PrettyPrint = true; jsonData["data"][0][i].ToJson(writer); dragItem.jsonData = writer.TextWriter.ToString(); //初始化Item dragItem.ChaRuItemPath = "Prefab/yuankong"; dragItem.Init(); } else { //根据数据数量生成设备 GameObject go = GameObject.Instantiate(Resources.Load("Prefab/Item")); DragItemH dragItem = go.AddComponent(); dragItem.SheBeiType = sheBeiType; //物体UI图片设置 switch (dragItem.SheBeiType) { case SheBeiType.受启棒: go.transform.SetParent(ShouQiBangParent); go.GetComponent().sprite = Resources.Load("image/baibang"); dragItem.ChaRuItemPath = "Prefab/Upan"; break; case SheBeiType.开机棒: go.transform.SetParent(KaiJiBangParent); go.GetComponent().sprite = Resources.Load("image/baibang"); dragItem.ChaRuItemPath = "Prefab/Upan"; break; case SheBeiType.注入器: go.transform.SetParent(ZhuRuQiParent); go.GetComponent().sprite = Resources.Load("image/yuankong"); dragItem.ChaRuItemPath = "Prefab/yuankong"; break; } //数据获取 dragItem.type = type; string ID = arrdata[i]["id"].ToString(); Debug.Log(ID); dragItem.id = ID; str = arrdata[i]["deviceId"].ToString(); dragItem.SheBeiHao = str; string name = arrdata[i]["name"].ToString(); dragItem.Name = name; //获取设备的Json数据 JsonWriter writer = new JsonWriter(); writer.PrettyPrint = true; jsonData["data"][0]["list"][i].ToJson(writer); dragItem.jsonData = writer.TextWriter.ToString(); //初始化Item dragItem.Init(); } } } } public void CraertChaRuItem(DragItemH item) { item.Show3DItem(); } public void MoveCamera(float speed) { Vector3 pos = Camera.main.transform.position; float Xmove= speed * Time.deltaTime; Xmove += pos.x; Xmove = Mathf.Clamp(Xmove, -4f, -0.9f); Camera.main.transform.position = new Vector3(Xmove, pos.y, pos.z); } public void GetItemType(DragItemH dragItem) { StartCoroutine(Get(dragItem)); } IEnumerator Get(DragItemH dragItem) { // if (dragItem.SheBeiHao != "") yield break; UnityWebRequest request; if (dragItem.SheBeiType == SheBeiType.设备) { if (isLocal) { request = UnityWebRequest.Get(GetMiMaLocalUrl); } else { request = UnityWebRequest.Get(GetMiMaUrl); } } else { if (isLocal) { request = UnityWebRequest.Get(GetLocalUrl + dragItem.type.ToString() + "?u3dType="); //request = UnityWebRequest.Get(GetLocalUrl + dragItem.type.ToString() + "{u3dType}"); } else { request = UnityWebRequest.Get(GetUrl + dragItem.type.ToString() + "?u3dType="); //request = UnityWebRequest.Get(GetUrl + dragItem.type.ToString() + "{u3dType}"); } } yield return request.SendWebRequest(); if (request.isHttpError || request.isNetworkError) { Debug.LogError(request.error); } else { //获取设备deviceId string receiveContent = request.downloadHandler.text; JsonData jsonData = JsonMapper.ToObject(receiveContent); JsonData arrdata; if (dragItem.SheBeiType == SheBeiType.设备) { arrdata = jsonData["data"][0]; } else { arrdata = jsonData["data"][0]["list"]; } string str; for (int i = 0; i < arrdata.Count; i++) { if (dragItem.SheBeiType == SheBeiType.设备)//获取密码设备 { if ((arrdata[i]["type"]).ToString() == dragItem.type.ToString() && arrdata[i]["id"].ToString() == dragItem.id.ToString()) { str = arrdata[i]["deviceId"].ToString(); dragItem.SheBeiHao = str; string name = arrdata[i]["name"].ToString(); dragItem.Name = name; //获取设备的Json数据 JsonWriter writer = new JsonWriter(); writer.PrettyPrint = true; jsonData["data"][0][i].ToJson(writer); dragItem.jsonData = writer.TextWriter.ToString(); } } else { //获取其他默认设备 if (arrdata[i]["u3dType"].ToString() == dragItem.type.ToString() && arrdata[i]["id"].ToString() == dragItem.id.ToString()) { str = arrdata[i]["deviceId"].ToString(); dragItem.SheBeiHao = str; string name = arrdata[i]["name"].ToString(); dragItem.Name = name; //获取设备的Json数据 JsonWriter writer = new JsonWriter(); writer.PrettyPrint = true; jsonData["data"][0]["list"][i].ToJson(writer); dragItem.jsonData = writer.TextWriter.ToString(); } } } //发送设备的Json数据 //Debug.Log(request.downloadHandler.text); //Debug.Log(request.downloadHandler.text); //Debug.Log("deviceId:" + arrdata); } SendItemData(); } public void SendItemData() { string data = "["; List list = new List(); for (int i = 0; i < checkDargs.Length; i++) { if (checkDargs[i].dragItem != null) { list.Add(checkDargs[i].dragItem); } } for (int i = 0; i < list.Count; i++) { data += list[i].jsonData; //Debug.Log(list[i].jsonData); if (list[i].jsonData != "") { if (i < list.Count - 1) { data += ','; } } } data += "]"; EduCoderTool.WebConnecter.Singleton.SendDataToWeb(data); Debug.Log(data); } }