using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using System; using System.Globalization; public class DataFormatWorld { public string countryName; public float co2emissions; } public class DataManager : MonoBehaviour { public Dictionary> dataPerYear; //public TMP_Text dataCountryUI; //public TMP_Text dataCo2emissionsUI; void Start() { //ResetDataUI(); LoadCSVFiles(); } /*public void ResetDataUI() { dataCountryUI.text = ""; dataCo2emissionsUI.text = ""; } public void SetDataToUI(DataFormatWorld data) { dataCountryUI.text = data.countryName; dataCo2emissionsUI.text = data.co2emissions.ToString(); }*/ void LoadCSVFiles() { string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "co2Emission.csv"); string result = System.IO.File.ReadAllText(filePath); List> data = CSVReader.Read(result); OrganizeDataInYear(data); } void OrganizeDataInYear(List> data) { string colnameCountry = "Country"; string colnameYear = "Year"; string colnameCO2 = "CO2EmissionRate (mt)"; dataPerYear = new Dictionary>(); foreach (Dictionary row in data) { string rowCountry = Convert.ToString(row[colnameCountry]); int rowYear = Convert.ToInt32(row[colnameYear]); float rowCO2; try { rowCO2 = float.Parse(row[colnameCO2].ToString(), CultureInfo.InvariantCulture.NumberFormat); } catch (Exception) { rowCO2 = -1f; } DataFormatWorld dataRow = new DataFormatWorld(); dataRow.countryName = rowCountry; dataRow.co2emissions = rowCO2; if (!dataPerYear.ContainsKey(rowYear)) { dataPerYear[rowYear] = new List(); } dataPerYear[rowYear].Add(dataRow); } } public List GetDataForYear(int year) { if (dataPerYear.ContainsKey(year)) { return dataPerYear[year]; } return null; } }