Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Reflection;
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct TimingSetting {
public float TrialStart;
public float TargetOnset;
public float TargetOnsetDev;
public float Go;
public float MoveMax;
public float HoldTime;
};
public class ExperimentController : MonoBehaviour
{
public string TimingSettingFile = @"\Tables\Timings.csv";
private TimingSetting mTimingSetting;
private string[] TableHeader;
private float[] TableData;
// Start is called before the first frame update
void Start()
{
int length = Marshal.SizeOf (typeof(TimingSetting));
TableHeader = GetTableHeader(TimingSettingFile);
TableData = GetTableFloat(TimingSettingFile);
mTimingSetting = FloatArrayToStruct(TableData,mTimingSetting);
}
// Update is called once per frame
void Update()
{
}
private string[] GetTableHeader(string filename)
{
string[] header;
string table_file = @".\Assets\Scripts";
table_file = table_file + filename;
using (var reader = new StreamReader(table_file)) {
var line = reader.ReadLine();
header = line.Split(',');
}
return header;
}
private float[] GetTableFloat(string filename)
{
float[] data = new float[6];
string table_file = @".\Assets\Scripts";
table_file = table_file + filename;
using (var reader = new StreamReader(table_file)) {
reader.ReadLine();
var line = reader.ReadLine();
string []values = line.Split(',');
for (int i = 0; i < values.Length; i++) {
data[i] = Single.Parse(values[i]);
}
}
return data;
}
private TimingSetting FloatArrayToStruct(float[] floatarray, TimingSetting structureObj) {
int length = floatarray.Length;
IntPtr ptr = Marshal.AllocHGlobal (length);
Marshal.Copy (floatarray, 0, ptr, length);
structureObj = (TimingSetting)Marshal.PtrToStructure (ptr, structureObj.GetType());
Marshal.FreeHGlobal (ptr);
return structureObj;
}
}