using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.Tracing; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class SpawnObjects : MonoBehaviour { public class LocationData { public int location_id; public string name; public double x_coordinate; public double z_coordinate; public string imageUrl; } public class CarData { public int location_id; public int car_count; } private const string apiUrl = "https://school.petrovv.com/api/location/csv"; // Example API endpoint private const string apiCarData = "https://school.petrovv.com/api/data/year/2023/month/5/day/30/hour/17/csv"; private Camera mainCamera; public GameObject spherePrefab; // Materials public Material redMaterial; public Material greenMaterial; public Material yellowMaterial; public RawImage cameraImage; public Texture2D emptyPng; private List locationDataList = new List(); private List carDataList = new List(); List listIndex = new List(249); List totalSpheres = new List(); private bool spawned = false; private bool isCarDataDone = false; // Start is called before the first frame update void Start() { mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); for (int i = 0; i < listIndex.Count; i++) { listIndex[i] = 0; } cameraImage.texture = emptyPng; StartCoroutine(GetCarDataFromApi()); StartCoroutine(GetLocationDataFromApi()); } // Update is called once per frame void Update() { if (!spawned && isCarDataDone && locationDataList.Count > 0) { SpawnCameraSpheres(); spawned = true; } if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { // Cast a ray from the screen touch position Ray ray = mainCamera.ScreenPointToRay(Input.GetTouch(0).position); // Check if the ray hits a game element if (Physics.Raycast(ray, out RaycastHit hit)) { // Get the touched game element GameObject touchedElement = hit.collider.gameObject; int ind = totalSpheres.FindIndex(elem => { return elem.transform.position == touchedElement.transform.position; }); string url = locationDataList[ind].imageUrl; ChangeCameraImageTexture(url); } else { cameraImage.texture = emptyPng; } } } // private void ChangeCameraImageTexture(GameObject sphere) private void ChangeCameraImageTexture(string imageUrl) { // string imageUrl = locationDataList[0].imageUrl; // Replace with your sphere image URL StartCoroutine(LoadImage(imageUrl)); } IEnumerator GetLocationDataFromApi() { using (UnityWebRequest webRequest = UnityWebRequest.Get(apiUrl)) { // Send the request and wait for a response yield return webRequest.SendWebRequest(); // Error handling from the web request if (webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.LogError("Error: " + webRequest.error); } else { string webRequestResult = webRequest.downloadHandler.text; string[] lines = webRequestResult.Split('\n'); foreach (string line in lines) { if (!string.IsNullOrEmpty(line)) { string[] values = line.Split(','); LocationData locationData = new LocationData { location_id = int.Parse(values[0]), name = values[1], z_coordinate = double.Parse(values[2]), x_coordinate = double.Parse(values[3]), imageUrl = values[4] }; locationDataList.Add(locationData); } } isCarDataDone = true; } } } IEnumerator GetCarDataFromApi() { using (UnityWebRequest webRequest = UnityWebRequest.Get(apiCarData)) { // Send the request and wait for a response yield return webRequest.SendWebRequest(); // Error handling from the web request if (webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.LogError("Error: " + webRequest.error); } else { string webRequestResult = webRequest.downloadHandler.text; string[] lines = webRequestResult.Split('\n'); foreach (string line in lines) { if (!string.IsNullOrEmpty(line)) { string[] values = line.Split(','); int index = int.Parse(values[0]); int value = int.Parse(values[1]); listIndex.Insert(index, value); } } isCarDataDone = true; } } } // Add this method to handle loading images asynchronously private IEnumerator LoadImage(string url) { UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Error loading image: " + www.error); } else { Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture; cameraImage.texture = texture; } } private void SpawnCameraSpheres() { Vector3 coordinates = new Vector3(0f, 0f, 0f); double mappedX; double mappedZ; foreach (LocationData location in locationDataList) { mappedX = CalculateX((double)location.x_coordinate); mappedZ = CalculateZ((double)location.z_coordinate); coordinates.x = (float)mappedX; coordinates.z = (float)mappedZ; GameObject sphere = Instantiate(spherePrefab); sphere.transform.position = coordinates; sphere.transform.localScale = new Vector3(0.02f, 0.02f, 0.02f); Renderer sphereRenderer = sphere.GetComponent(); if (location.location_id == 999) { continue; } if (listIndex.ElementAt(location.location_id) <= 100) { sphereRenderer.material = greenMaterial; } else if (listIndex.ElementAt(location.location_id) <= 250) { sphereRenderer.material = yellowMaterial; } else { sphereRenderer.material = redMaterial; } totalSpheres.Add(sphere); } } // X is equivelant to longitude double CalculateX(double val) { return Map(val, 13.356836, 16.596429, 1, -1); } // Z is equivelant to latitude double CalculateZ(double val) { return Map(val, 45.411120, 46.875651, 0.6665, -0.6665); } // Mapping function double Map(double x, double inMin, double inMax, double outMin, double outMax) { return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } }