You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
13 KiB

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<ChaKong>();
if (chaKong != null)
{
Vector2 pos = Camera.main.WorldToScreenPoint(chakongPoints[i].transform.position);
GameObject ui = Resources.Load<GameObject>("Prefab/chakong");
GameObject go = GameObject.Instantiate(ui, chakongParent);
go.GetComponent<CheckDarg>().ChaKongPos = chakongPoints[i].transform.position;
go.GetComponent<CheckDarg>().ChaKongType = chaKong.SheBeiType;
go.transform.position = pos;
checkDargs[i] = go.GetComponent<CheckDarg>();
}
}
}
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<GameObject>("Prefab/Item"));
DragItemH dragItem = go.AddComponent<DragItemH>();
dragItem.SheBeiType = sheBeiType;
//UI图片设置
go.transform.SetParent(SheBeiParent);
go.GetComponent<Image>().sprite = Resources.Load<Sprite>("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<GameObject>("Prefab/Item"));
DragItemH dragItem = go.AddComponent<DragItemH>();
dragItem.SheBeiType = sheBeiType;
//物体UI图片设置
switch (dragItem.SheBeiType)
{
case SheBeiType.:
go.transform.SetParent(ShouQiBangParent);
go.GetComponent<Image>().sprite = Resources.Load<Sprite>("image/baibang");
dragItem.ChaRuItemPath = "Prefab/Upan";
break;
case SheBeiType.:
go.transform.SetParent(KaiJiBangParent);
go.GetComponent<Image>().sprite = Resources.Load<Sprite>("image/baibang");
dragItem.ChaRuItemPath = "Prefab/Upan";
break;
case SheBeiType.:
go.transform.SetParent(ZhuRuQiParent);
go.GetComponent<Image>().sprite = Resources.Load<Sprite>("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<DragItemH> list = new List<DragItemH>();
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);
}
}