Pick next value from string on mouse click
This might be a newbie question, but I really need some advice. For the project I'm working on, I need to export some parameters into csv file. On mouse click, I need to get: display x coordinate, display y coordinate, first number from string, second number from string. I'm fine with getting this so far.
The question is, how can I get {x, y, 3rd number from string, 4th number from string}; {x, y, 5th number from string, 6th number from string} and so on with every next mouse click? I was trying to do something with [index-1] [index+1]
but without success so far.
Would be happy to get any advice. Thank you in advance!
public class SendToCsv : MonoBehaviour
{
private List<string[]> rowData = new List<string[]>();
public List<string> UV = new List<string>(); //String with values from which to pick
void Start()
{
UV.Add("2");
UV.Add("4");
UV.Add("6"); //Here I add more numbers of course
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
string[] rowDataTemp = new string[4];
rowDataTemp[0] = Input.mousePosition.x.ToString("F4"); // X
rowDataTemp[1] = Input.mousePosition.y.ToString("F4"); // Y
rowDataTemp[2] = UV[1]; //This value must change with each click
rowDataTemp[3] = UV[2]; //This value must change with each click
rowData.Add(rowDataTemp);
string[][] output = new string[rowData.Count][];
for (int i = 0; i < output.Length; i++)
{
output[i] = rowData[i];
}
int length = output.GetLength(0);
string delimiter = ",";
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
string filePath = getPath();
StreamWriter outStream = System.IO.File.CreateText(filePath);
outStream.WriteLine(sb);
outStream.Close();
}
}
}
Answer by Hellium · Jul 19, 2019 at 11:08 AM
public class SendToCsv : MonoBehaviour
{
private List<string[]> rowData = new List<string[]>();
public List<string> UV = new List<string>(); //String with values from which to pick
private int uvIndex = 0;
void Start()
{
UV.Add("2");
UV.Add("4");
UV.Add("6"); //Here I add more numbers of course
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
string[] rowDataTemp = new string[4];
rowDataTemp[0] = Input.mousePosition.x.ToString("F4"); // X
rowDataTemp[1] = Input.mousePosition.y.ToString("F4"); // Y
rowDataTemp[2] = UV[uvIndex];
rowDataTemp[3] = UV[uvIndex+1];
rowData.Add(rowDataTemp);
uvIndex += 2;
string[][] output = new string[rowData.Count][];
for (int i = 0; i < output.Length; i++)
{
output[i] = rowData[i];
}
int length = output.GetLength(0);
string delimiter = ",";
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
string filePath = getPath();
StreamWriter outStream = System.IO.File.CreateText(filePath);
outStream.WriteLine(sb);
outStream.Close();
}
}
}
But, be carefull, if you click too many times, uvIndex
will be creater than UV.Count
and you will have an ArgumentOutOfRangeException
.
I don't know how you want to handle the case. You could "loop" and replace
uvIndex += 2;
By
uvIndex = (uvIndex + 2) % UV.Count;
Or clamp the value of uvIndex and replace
uvIndex += 2;
By
uvIndex = Mathf.Min( uvIndex + 2, UV.Count - 2);
Thank you very much, Hellium! Will go testing right away.
Your answer
Follow this Question
Related Questions
C# Count duplicates in List 2 Answers
How to output a List of Strings to canvas text? 0 Answers
Insert string into empty list at a specific index 0 Answers