using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.Networking; using Newtonsoft.Json; using UnityEngine.UIElements; //using UnityEngine.Analytics; public struct TagRecord { public String Time; public String BlockNamesAliases; public Vector3 LocationsMO; public Vector3 LocationsRTLS; } public class ThanksStASSForTheNiceMethod : MonoBehaviour { [Header("Ссылка на метод и авторизация")] public string URL; public string Auth; [Header("Листы с данными")] public SortedDictionary mapDateTimeRecords = new SortedDictionary(); public List MarksTime = new List(); public List MarksHronology; //public List LocationsMO; //Точки МО //public List LocationsRTLS; //Точки РТЛС //public List BlockNamesAliases; //Название отметок [Header("UIDocument")] public UIDocument UI; [Header("Экран с перемещением")] public VisualTreeAsset GameUIPrefab; //Экран с перещением [Header("ИИ")] public PersonScript Personchikn; // ИИ [Header("Скрипт выполняющий запрос")] public Uiika UIka; public void Request() { GenerateRequest(); } public void ClearList() //Очистка данных { mapDateTimeRecords.Clear(); } void GenerateRequest() //Генерация запроса { //StartCoroutine(SpawnTestItems()); StartCoroutine(ProcessRequest(URL)); } private IEnumerator SpawnTestItems() { string uri = "http://localhost:8001/api/v2/marks/getMarksLabelsByArrPoints"; using (UnityWebRequest request = UnityWebRequest.Get(uri)) { //request.SetRequestHeader("Authorization", Auth); //request.SetRequestHeader("Access-Control-Allow-Origin", "*"); request.timeout = 60; yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { var responseObject = JsonConvert.DeserializeObject(request.downloadHandler.text); try { foreach (var item in responseObject.respose.items) { Vector3 pos = new Vector3(item.center_point.x, item.center_point.z, item.center_point.y); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = pos; // Создание материала Material redMaterial = new Material(Shader.Find("Standard")); redMaterial.color = Color.red; // Применение материала к сфере sphere.GetComponent().material = redMaterial; } } catch (Exception e) { Debug.LogException(e); } } else { Debug.Log("Received: " + request.downloadHandler.text); } } } private IEnumerator ProcessRequest(string uri) { //Analytics.CustomEvent("check_uri", new Dictionary //{ // { "level", 1 }, // { "player_name", "Player1" } //}); using (UnityWebRequest request = UnityWebRequest.Get(uri)) { //request.SetRequestHeader("Authorization", Auth); //request.SetRequestHeader("Access-Control-Allow-Origin", "*"); request.timeout = 60; yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { Debug.Log("Error: " + request.error); UIka.DoActionsOnRequestFailed(request.error); } else { Debug.Log("Received: " + request.downloadHandler.text); var MethodJSON = JsonConvert.DeserializeObject(request.downloadHandler.text); try { foreach (Item tagItem in MethodJSON.respose.items) //Парсинг данных { TagRecord record = new TagRecord(); record.LocationsMO = new Vector3(tagItem.x_mo, tagItem.z_mo, tagItem.y_mo); record.LocationsRTLS = new Vector3(tagItem.x, tagItem.z, tagItem.y); record.Time = tagItem.time.ToLocalTime().ToString(); if (tagItem.block_names_aliases.Count > 1) { record.BlockNamesAliases = tagItem.block_names_aliases[0] + " " + tagItem.block_names_aliases[1]; } else { record.BlockNamesAliases = tagItem.block_names_aliases[0]; } mapDateTimeRecords.Add(tagItem.time, record); } UIka.SetResponseCode(request.responseCode.ToString()); } catch (NullReferenceException) { Debug.Log("ItemsNull"); //UIka.loadimg.visible = false; UIka.DoActionsOnRequestFailed("Данных за период нет"); } try { foreach (MarksTime mark in MethodJSON.respose.marks_time) { MarksTime.Add(mark.name + "!" + mark.count_seconds); } } catch (NullReferenceException) { Debug.Log("MarksTimeNull"); UIka.DoActionsOnRequestFailed("Ошибка получения данных"); } MarksHronology = MethodJSON.respose.marks_hronology; if (mapDateTimeRecords.Count > 1) { Debug.Log("No problemo"); UIka.SetResponseCode(request.responseCode.ToString()); //Установка кода Personchikn.InitMePlease(); //Вызов инициализация в ИИ UIka.DoActionsOnRequestSuccess(); Debug.Log("Got data, here go"); } else { UIka.DoActionsOnRequestFailed("Нет данных за запрашиваемый период"); } } } } }