Reading Text Files in Unity - mklasinc/capstone GitHub Wiki

  • Unity has a TextAsset, which allows you to read in text files as single strings. You should then implement parsing logic to use parts of the text file.

You can produce a text file in Matlab before the beginning of the experiment. Or you just check the validity of trials in Matlab.

Parsing Strings in Unity

All incoming Osc messages a prefixed with a forward slash (/). That means all messages have to be split.
Example

string msgString = Osc.OscMessageToString(message);
Debug.log (msgString); 
------------
/trial_status:end
------------

String.Split

C# String method to split a string

string msgString = Osc.OscMessageToString(message);
msgString = msgString.Split("/"[0])[1]; //split into array of string upon matching a "/" character

Regexp matching

Creating regular expressions in C#. Make sure you have access to the Regexp namespace.

	private void ParseTrialStatus(string msg){

		string text = "One car red car blue car";
		string pat = "end";
		Regex r = new Regex(pat, RegexOptions.IgnoreCase);
		Match m = r.Match(msg);
		Debug.Log ("trial status match is successful:" + m.Success);
	
	}
⚠️ **GitHub.com Fallback** ⚠️