Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ibrarwhala · Oct 18, 2020 at 03:45 PM · sphererealtimecsv

Refreshing CSV file in Run-Time

 using UnityEngine;
 using System.Collections.Generic;
 using System;
 
 public class myfirstgame : MonoBehaviour
 {
     public TextAsset csvdata;
     int counter = 0;
     Vector3 temp;
 
     void Start()
     {
 
     }
 
     void Update()
     {
       
         if (counter > 500)
         {
             return;
         }
         else
         {
             string[] records = csvdata.text.Split('\n');
             counter++;
             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;
                 
             }
         }
     }
 }
 
 

Hi, 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. Thanks CSV data Script

csvdata.txt (20.2 kB)
script.txt (804 B)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 18, 2020 at 06:12 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ibrarwhala · Oct 19, 2020 at 03:21 AM

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.

Regards Ibrar

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ibrarwhala · Oct 24, 2020 at 01:51 PM 0
Share

Dear Sir, $$anonymous$$y outside source generates the CSV file and appends data 16 samples every second, so is it possible that UNITY read my CSV file line by line with some milliseconds delay, or in real-time, the dynamic file option will refresh the CSV file? Now this time when I run the Unity Application, that time for example outside source generates 100 samples so unity just read and operate only for 100 samples but in real-time my file is appended every second, I hope you understand my question.

Thanks for your consideration.

Best Regards

Ibrar

avatar image
0

Answer by rh_galaxy · Oct 24, 2020 at 01:58 PM

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[5000];
 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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parsing a CSV load from server 1 Answer

Outputting data to CSV file from multiple lists at specific Headers in the CSV? 0 Answers

Real time high quality interactive liquids 1 Answer

real time terrain texture based on height 0 Answers

Unity Pro water with a cubemap instead of real time reflections 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges