Skip to content

Commit 6fcf533

Browse files
committed
Added C# event system for programatically hooking into session functions
1 parent 2f7fe1f commit 6fcf533

File tree

8 files changed

+60
-151
lines changed

8 files changed

+60
-151
lines changed

Assets/DevelopmentScene.unity

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,32 @@ Prefab:
370370
propertyPath: m_AnchorMax.y
371371
value: 1
372372
objectReference: {fileID: 0}
373-
m_RemovedComponents: []
373+
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
374+
type: 2}
375+
propertyPath: m_AnchorMin.y
376+
value: 1
377+
objectReference: {fileID: 0}
378+
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
379+
type: 2}
380+
propertyPath: m_AnchorMax.y
381+
value: 1
382+
objectReference: {fileID: 0}
383+
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
384+
type: 2}
385+
propertyPath: m_AnchoredPosition.x
386+
value: 164
387+
objectReference: {fileID: 0}
388+
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
389+
type: 2}
390+
propertyPath: m_SizeDelta.x
391+
value: 318
392+
objectReference: {fileID: 0}
393+
- target: {fileID: 224546371281550106, guid: 0a3b6392f04558844bd340e68ced1ff9,
394+
type: 2}
395+
propertyPath: m_SizeDelta.y
396+
value: 0
397+
objectReference: {fileID: 0}
398+
m_RemovedComponents:
399+
- {fileID: 114291432593817218, guid: 0a3b6392f04558844bd340e68ced1ff9, type: 2}
374400
m_ParentPrefab: {fileID: 100100000, guid: 0a3b6392f04558844bd340e68ced1ff9, type: 2}
375401
m_IsPrefabParent: 0

Assets/UXF/Scripts/Session.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ public class Session : MonoBehaviour
176176
/// Reference to the associated FileIOManager which deals with inputting and outputting files.
177177
/// </summary>
178178
private FileIOManager fileIOManager;
179-
180-
/// <summary>
181-
/// Reference to the associated SessionLogger which automatically stores all Debug.Log calls
182-
/// </summary>
183-
private SessionLogger logger;
184-
179+
185180
List<string> baseHeaders = new List<string> { "experiment", "ppid", "session_num", "trial_num", "block_num", "trial_num_in_block", "start_time", "end_time" };
186181

187182
/// <summary>
@@ -190,7 +185,6 @@ public class Session : MonoBehaviour
190185
/// <value></value>
191186
public string basePath { get; private set; }
192187

193-
194188
/// <summary>
195189
/// Path to the folder used for reading settings and storing the output.
196190
/// </summary>
@@ -226,24 +220,37 @@ public class Session : MonoBehaviour
226220
/// </summary>
227221
public Dictionary<string, object> participantDetails;
228222

223+
/// <summary>
224+
/// An event handler for a C# event containing the session as an argument.
225+
/// </summary>
226+
public delegate void EventHandler();
227+
228+
/// <summary>
229+
/// Event raised after session has been initialised, used for UXF functionality. Users should use similar OnSessionBegin UnityEvent.
230+
/// </summary>
231+
public event EventHandler onInitialise;
232+
233+
/// <summary>
234+
/// Event raised before session finished, used for UXF functionality. Users should use the similar OnSessionEnd UnityEvent.
235+
/// </summary>
236+
public event EventHandler cleanUp;
237+
238+
/// <summary>
239+
/// Provide references to other components
240+
/// </summary>
229241
void Awake()
230242
{
231243
// get components attached to this gameobject and store their references
232-
AttachReferences(
233-
GetComponent<FileIOManager>(),
234-
GetComponent<SessionLogger>()
235-
);
244+
AttachReferences(GetComponent<FileIOManager>());
236245
}
237246

238247
/// <summary>
239248
/// Provide references to other components
240249
/// </summary>
241250
/// <param name="newFileIOManager"></param>
242-
/// <param name="newSessionLogger"></param>
243-
public void AttachReferences(FileIOManager newFileIOManager = null, SessionLogger newSessionLogger = null)
251+
public void AttachReferences(FileIOManager newFileIOManager = null)
244252
{
245253
if (newFileIOManager != null) fileIOManager = newFileIOManager;
246-
if (newSessionLogger != null) logger = newSessionLogger;
247254
}
248255

249256
/// <summary>
@@ -376,16 +383,13 @@ public void Begin(string experimentName, string participantId, string baseFolder
376383

377384
// Initialise FileIOManager
378385
if (!fileIOManager.IsActive) fileIOManager.Begin();
379-
380-
// Initialise logger
381-
if (logger != null)
382-
logger.Initialise();
383-
384386
_hasInitialised = true;
387+
388+
// raise the session events
389+
if (onInitialise != null) onInitialise();
385390
onSessionBegin.Invoke(this);
386391

387392
// copy Settings to session folder
388-
389393
WriteDictToSessionFolder(
390394
new Dictionary<string, object>(settings.baseDict), // makes a copy
391395
"settings");
@@ -537,11 +541,10 @@ public void End()
537541
currentTrial.End();
538542
SaveResults();
539543

540-
// end logger
541-
if (logger != null)
542-
logger.Finalise();
544+
// raise cleanup event
545+
if (cleanUp != null) cleanUp();
543546

544-
// end FileIOManager
547+
// end FileIOManager - forces immediate writing of all files
545548
fileIOManager.End();
546549

547550
currentTrialNum = 0;
@@ -568,7 +571,6 @@ void SaveResults()
568571
fileName
569572
);
570573

571-
// in this case, write in main thread to block aborting
572574
fileIOManager.ManageInWorker(() => fileIOManager.WriteTrials(results, headers.ToArray(), fileInfo));
573575
}
574576

Assets/UXF/Scripts/SessionDebugger.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

Assets/UXF/Scripts/SessionDebugger.cs.meta

Lines changed: 0 additions & 13 deletions
This file was deleted.

Assets/UXF/Scripts/SessionLogger.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void Awake()
2323
newFileIOManager: GetComponent<FileIOManager>(),
2424
newSession: GetComponent<Session>()
2525
);
26+
Initialise();
2627
}
2728

2829
/// <summary>
@@ -53,6 +54,7 @@ public void Initialise()
5354
);
5455

5556
Application.logMessageReceived += HandleLog;
57+
session.cleanUp += Finalise; // finilise logger when cleaning up the session
5658
}
5759

5860
void HandleLog(string logString, string stackTrace, LogType type)
@@ -80,6 +82,7 @@ public void Finalise()
8082

8183
fileIOManager.ManageInWorker(() => fileIOManager.WriteCSV(table, fileInfo));
8284
Application.logMessageReceived -= HandleLog;
85+
session.cleanUp -= Finalise;
8386
}
8487

8588
}

Assets/UXF/Scripts/Tests/Editor/TestMultithreading.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public void SetUp()
2727
session = gameObject.AddComponent<Session>();
2828

2929
session.AttachReferences(
30-
fileIOManager,
31-
sessionLogger
30+
fileIOManager
3231
);
3332

3433
sessionLogger.AttachReferences(
@@ -39,8 +38,6 @@ public void SetUp()
3938
sessionLogger.Initialise();
4039

4140
fileIOManager.debug = true;
42-
fileIOManager.Begin();
43-
4441
}
4542

4643
[TearDown]

Assets/UXF/Scripts/Tests/Editor/TestTrackers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public void SetUp()
2727
session = gameObject.AddComponent<Session>();
2828

2929
session.AttachReferences(
30-
fileIOManager,
31-
sessionLogger
30+
fileIOManager
3231
);
3332

3433
sessionLogger.AttachReferences(

Assets/UXF/Scripts/Tests/Editor/TestTrials.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public void SetUp()
2727
session = gameObject.AddComponent<Session>();
2828

2929
session.AttachReferences(
30-
fileIOManager,
31-
sessionLogger
30+
fileIOManager
3231
);
3332

3433
sessionLogger.AttachReferences(

0 commit comments

Comments
 (0)