public class myfirstgame : MonoBehaviour
public TextAsset csvdata;
int counter = 0;
if (counter > 500)
string records = csvdata.text.Split('
float.TryParse(records[counter], out float line1);
if (line1 >= 2)
temp = transform.localScale;
temp.x += 0.005f;
temp.y += 0.005f;
temp.z += 0.005f;
transform.localScale = temp;
I want to refresh my CSV file which is generated from the outside source and save in the asset folder of Unity. My question is that How I can refresh the CSV file in Run-Time Unity because my CSV file is updated means every second outside source append the data to the CSV file, in real-time I want to use that data in Unity and control the sphere object radius which depends on the upcoming DATA in CSV file.
I attached my code here, please help me that How I can use the appended CSV file in real-time or run time in Unity.
TextAssets are read only hardcoded text data that is compiled into your game. Of course those do not change and can not be changed at runtime. You would need to read an external file through usual System.IO.File operations like File.ReadAllText. However having two applications accessing the same file at the same time isn’t really possible. When an application opens a file it gets locked by the OS until it’s closed again.
While it is possible to use files for inter process communications, it’s not really a great option for realtime communication. If the creating process creates a new file every second then Unity could read it once it has been written and closed by the other process. This is usually done by creating the file in a different folder, write the data and close the file and once closed you could move it into the target folder. That should provide the least problematic solution. The Unity process, once the file is read, could then delete the file. Of course the files that are generated could have a file name with an increasing number in the file name so there can be multiple pending files waiting. However the creating process could check if the old file has been deleted and replace it with the new one, once the old one is gone.
As I said the file system isn’t really made for this kind of thing. On Windows you could use named pipes which are a bit like “virtual files” that can be written by one process and read by another process. Though nowadays it’s more common to just use socket communication between applications, even when they run on the same machine.
ps: You do know that your csv file essentially contains just zeros, right?
Dear @Bunny83 ,
My system doesn’t generate a new file for every second it is appended the data to the same CSV file for every second. But on run time unity just read the given data on the CSV, I want to read that updated or appended CSV file in unity run time.
I hope you understand my point, I’m new to Unity and C# coding.
Something like this could be used to just read newly appended data from the file, but I don’t know if you can do a read so that only part of a line is read, then you have to skip the first line and last line to be in sync. It’s only possible if the file is opened with sharing in mind by the other process. But it’s also true that it’s not a good way of doing communication between processes. (You may have to close and reopen the file for each access).
//done once in Start()
FileStream fs = File.OpenRead(Application.persistentDataPath + "/" + "csv_file.txt");
byte data = new byte;
fs.Position = fs.Length;
//done every 500ms in Update()
int numread = fs.Read(data, 0, data.Length);
string fileText = System.Text.Encoding.UTF8.GetString(data, 0, numread);