Search Unity

Time Card Extension

Discussion in 'Immediate Mode GUI (IMGUI)' started by DChristy87, Feb 6, 2016.

  1. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    I wanted a way to log my overall time working on a project in Unity. I couldn't find anything out there so I started working on this code this morning. It works for the most part but somethings just aren't all that great. First of all, when the editor first starts up and creates a new file (can be tested by deleting the .txt file that holds the total time), the session time isn't synced up with the total time. There is a gap of about 7 to 9 seconds or so and I cant figure out what would cause such a large gap.
    EDIT: Also, the time doesn't stop if you have Unity minimized. So if Unity is running in the background all day, it'll log that time.

    Other than that, I'm sure it's not best practice to continuously open and close files every second :oops:

    Here is the code if anyone wants to use/fix/optimize it:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.IO;
    5.  
    6. [InitializeOnLoad]
    7. public class TimeCard
    8. {
    9.     static int nextSave = 0;
    10.     static int saveTime = 1;
    11.     static string logFile = "TimeCard.txt";
    12.     public static int timeSinceStartup;
    13.     public static int initTime;
    14.  
    15.     static TimeCard()
    16.     {      
    17.         if (!File.Exists(logFile))
    18.         {
    19.             Debug.LogWarning("Creating File");
    20.             File.WriteAllText(logFile, 0.ToString());
    21.         }
    22.         initTime = ReadFile();
    23.         EditorApplication.update += Update;
    24.         Debug.Log("Clocking In");
    25.     }
    26.  
    27.     static void Update()
    28.     {
    29.         timeSinceStartup = Mathf.RoundToInt((float)EditorApplication.timeSinceStartup);
    30.         if (EditorApplication.timeSinceStartup > nextSave)
    31.         {
    32.             initTime += saveTime;          
    33.             nextSave = timeSinceStartup + saveTime;
    34.             WriteFile();
    35.         }
    36.     }
    37.  
    38.     static void WriteFile()
    39.     {
    40.         StreamWriter file = new StreamWriter(logFile);
    41.         file.WriteLine(initTime);
    42.         file.Close();
    43.     }
    44.  
    45.     static int ReadFile()
    46.     {
    47.         StreamReader file = new StreamReader(logFile);
    48.         int time = int.Parse(file.ReadLine());
    49.         file.Close();
    50.         return time;
    51.     }
    52. }
    53.  
    54. class TimeCardWindow : EditorWindow
    55. {
    56.     [MenuItem("Window/Time Card")]
    57.  
    58.     public static void ShowWindow()
    59.     {
    60.         EditorWindow.GetWindow(typeof(TimeCardWindow));
    61.     }
    62.  
    63.     void OnGUI()
    64.     {
    65.         TimeSpan t = TimeSpan.FromSeconds(TimeCard.initTime);
    66.         TimeSpan s = TimeSpan.FromSeconds(TimeCard.timeSinceStartup);
    67.  
    68.         string totalTime = string.Format("{0:D2}d:{1:D2}h:{2:D2}m:{3:D2}s",
    69.                t.Days,
    70.                t.Hours,
    71.                t.Minutes,
    72.                t.Seconds);
    73.         string sessionTime = string.Format("{0:D2}d:{1:D2}h:{2:D2}m:{3:D2}s",
    74.                s.Days,
    75.                s.Hours,
    76.                s.Minutes,
    77.                s.Seconds);
    78.         GUILayout.Label("Total Time: " + totalTime);
    79.         GUILayout.Label("Session Time: " + sessionTime);
    80.     }
    81.  
    82.     void OnInspectorUpdate()
    83.     {
    84.         Repaint();
    85.     }
    86. }
    Hope someone can help make it sync up. If someone figures it out, please share your results!
     
    Last edited: Feb 7, 2016
  2. Sarmie

    Sarmie

    Joined:
    Jun 12, 2015
    Posts:
    1
    Code (CSharp):
    1. public class TimeCard
    2. {
    3.     static double nextSave = 0;
    4.     readonly static double saveTime = 1;
    5.     static string logFile = "TimeCard.txt";
    6.     public static double timeSinceStartup;
    7.     public static double initTime;
    8.     public static double timeMove;
    9.  
    10.     static TimeCard()
    11.     {
    12.         if (!File.Exists(logFile))
    13.         {
    14.             Debug.LogWarning("Creating File");
    15.             File.WriteAllText(logFile, 0.ToString());
    16.         }
    17.         initTime = Convert.ToDouble(ReadFile());
    18.         timeMove = EditorApplication.timeSinceStartup;
    19.         EditorApplication.update += Update;
    20.         Debug.Log("Clocking In");
    21.     }
    22.  
    23.     static void Update()
    24.     {
    25.         timeSinceStartup = EditorApplication.timeSinceStartup - timeMove;
    26.         if (EditorApplication.timeSinceStartup > nextSave)
    27.         {
    28.             initTime += saveTime + (timeSinceStartup - nextSave);
    29.             nextSave = timeSinceStartup + saveTime;
    30.             WriteFile();
    31.         }
    32.     }
    33.  
    34.     static void WriteFile()
    35.     {
    36.         StreamWriter file = new StreamWriter(logFile);
    37.         file.WriteLine(Convert.ToInt32(initTime));
    38.         file.Close();
    39.     }
    40.     //Other code...
    Code (CSharp):
    1.     void OnGUI()
    2.     {
    3.         TimeSpan t = TimeSpan.FromSeconds(Convert.ToInt32(TimeCard.initTime));
    4.         TimeSpan s = TimeSpan.FromSeconds(Convert.ToInt32(TimeCard.timeSinceStartup));
    5.         //Other code...
    Fixed syncing problem
     
    DChristy87 likes this.
  3. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    Very cool Sarmie, thanks for the help! :)