Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Saving frames per second to CSV file

Discussion in 'Scripting' started by felt9099, Mar 8, 2020.

  1. felt9099

    felt9099

    Joined:
    Feb 1, 2018
    Posts:
    36
    Hey -

    I'm looking to record the frame rate of my game (e.g., recording the frame rate once per second) which automatically saves to a CSV file with different trial names. For example, the CSV file would have data with a header (e.g., Play 1) with row of the game FPS recorded each second, for example "60, 55, 60..." Then on the next play, "Play 2" with numbers of "55, 60, 65..." (or whatever the frame rate is).

    Is there any way to do this? I've been looking through the forums/Google to try to answer this, but I can't make heads or tails of much of what I'm reading.

    Thank you!
     
  2. joemane22

    joemane22

    Joined:
    Dec 16, 2013
    Posts:
    4
    CSV file stands for comma separated values.

    https://www.computerhope.com/issues/ch001356.htm
    This link explains it as well but in short for each value is separated by a comma and each row is separated by a new line. So it would look like this.

    Play1, 50, 51, 55, 60
    Play2, 52, 54, 58, 49

    So you need a class that holds the data as a float array which should be self explanatory.

    Then you need a way to generate the CSV file from that data. The easiest way is to loop through the data array and append onto a string the string representation of the float value and a comma. Then after they were all added remove the last comma and add a new line to finish the entry.

    Now you need to open or create the CVS file. Just use the System.IO namespace to accomplish this. StreamWriter is the text version of the system file streams.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=netframework-4.8

    That should do it!