Skip to content
Jack Brookes edited this page Apr 15, 2020 ยท 10 revisions

Ideas for getting the most out of UXF

Backup your datafiles

You can hook a method into the On Write File Event in the FileIOManager component of UXF Rig that copies files to a secondary location.

Singleton access

Even though it can lead to bad programming practices, you may opt to use a "singleton" programming technique to make accessing the UXF Session easier. Here is an example - attach this script to your UXF Rig and it will persist between scenes and be globally accessible with PersistentRigAccess.uxfSession.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PersistentRigAccess : MonoBehaviour
{

    public static UXF.Session uxfSession;

    private void Awake()
    {
        // make sure only one
        PersistentRigAccess[] rigs = GameObject.FindObjectsOfType<PersistentRigAccess>();
        if (rigs.Length > 1) Destroy(this.gameObject);

        DontDestroyOnLoad(this);

        uxfSession = GetComponentInChildren<UXF.Session>();
    }
}

Record audio

You can record audio on each trial using Unity's microphone class and then convert to a WAV file. Then you can safely store the files after each trial using the FileIOManager worker:

        // method that converts audioclip to wav bytes
        byte[] data = WavUtility.FromAudioClip(newClip);
        string fname = string.Format("mic_T{0:000}.wav", trial.number);
        string outputLocation = Path.Combine(session.FullPath, fname);

        // store in results, easier to access later
        trial.result["mic_filename"] = fname;

        // reference to FileIOManager
        fileIOManager.ManageInWorker(() => 
        {
            File.WriteAllBytes(outputLocation, data);
        });

Custom file writing

You may want to write a custom file to the session folder on each trial. You could do this with a method like this, which can be added as an event on trial end:

        public void SaveData(UXF.Trial trial)
        {
            // array or list of strings you want to write to file
            string[] exampleOutputData = new string[]{ "line1", "line2" };

            string fname = string.Format("custom_output_T{0:000}.csv", trial.number);
            string outputLocation = Path.Combine(trial.session.FullPath, fname);

            // store in results, easier to access later
            trial.result["custom_output_filename"] = fname;

            var fileIO = trial.session.GetComponent<UXF.FileIOManager>();
            fileIO.ManageInWorker(() =>
            {
                File.WriteAllLines(outputLocation, exampleOutputData);
            });
         }

๐Ÿง  Core topics

โ“ More help


๐Ÿ‘ฉโ€๐Ÿ’ป Programming reference

Unit tests

Clone this wiki locally