using System.Collections.Generic;
using UnityEngine.UI; // Only needed if you plan to use UI elements like Dropdown
public class TimeseriesCSVParser : MonoBehaviour
// Assign your CSV file (as a TextAsset) in the inspector.
public TextAsset csvFile;
// If using a UI dropdown for column selection, assign it in the Inspector.
public Dropdown columnDropdown;
// Store headers and data rows.
private string[] headers;
private List<string[]> dataRows = new List<string[]>();
Debug.LogError("CSV file not assigned!");
// Parse the CSV file into headers and data rows.
// Split by newlines (adjust for your CSV if needed)
string[] lines = csvFile.text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
headers = lines[0].Split(',');
// Load each data row (starting from line 1)
for (int i = 1; i < lines.Length; i++)
// Split each row by commas (you might need to adjust if your CSV contains quoted commas)
string[] row = lines[i].Split(',');
// Populate a UI Dropdown with the header names (except the date column if you wish).
private void SetupDropdown()
if (columnDropdown != null && headers != null)
// Clear existing options
columnDropdown.ClearOptions();
// Create list of options. If the first column is date, you might start at index 1.
List<string> options = new List<string>();
for (int i = 1; i < headers.Length; i++)
columnDropdown.AddOptions(options);
// Optionally, add a listener to trigger analysis when selection changes.
columnDropdown.onValueChanged.AddListener(delegate { AnalyzeSelectedColumn(); });
// Call this method when you want to analyze the currently selected column.
public void AnalyzeSelectedColumn()
// Adjust index: if your first column (index 0) is the date column, add 1.
int selectedColumn = columnDropdown != null ? columnDropdown.value + 1 : 1;
AnalyzeColumn(selectedColumn);
// Analyze the data for the given column index.
private void AnalyzeColumn(int columnIndex)
List<DateTime> dates = new List<DateTime>();
List<float> values = new List<float>();
// Loop through each data row.
foreach (var row in dataRows)
// Ensure the row has enough columns.
if (row.Length > columnIndex && row.Length > 0)
// Parse the date from the first column.
if (DateTime.TryParse(row[0].Trim(), out DateTime date))
Debug.LogWarning("Unable to parse date: " + row[0]);
// Parse the numeric value from the selected column.
if (float.TryParse(row[columnIndex].Trim(), out float value))
Debug.LogWarning("Unable to parse value in column " + headers[columnIndex] + ": " + row[columnIndex]);
if (dates.Count > 0 && values.Count > 0)
// Compute min and max dates.
DateTime minDate = dates[0], maxDate = dates[0];
// Compute min and max values.
float minValue = values[0], maxValue = values[0];
foreach (var v in values)
Debug.Log("Analyzing Column: " + headers[columnIndex]);
Debug.Log("Min Date: " + minDate.ToString());
Debug.Log("Max Date: " + maxDate.ToString());
Debug.Log("Min Value: " + minValue);
Debug.Log("Max Value: " + maxValue);
Debug.LogWarning("Insufficient valid data for analysis in the selected column.");