Skip to content

Commit d1ce342

Browse files
authored
Merge pull request #18 from BRomans/unity-examples
📝 updated README-Unity.md code examples with new naming convention
2 parents bb0f510 + 048aaa2 commit d1ce342

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

README-Unity.md

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LSL.cs includes a Unity interface to liblsl as a [Unity native plug-in](https://
2020
1. When the cube is selected, in the Inspector click on "Add Component", and create a new script called LSLInput.
2121
1. In the Project viewer, double click on LSLInput.cs. This should launch Visual Studio or another IDE.
2222
1. Fill in the script. Use [LSL4Unity AInlet](https://github.com/labstreaminglayer/LSL4Unity/blob/master/Scripts/AInlet.cs) for inspiration.
23-
* There is [currently a bug](https://github.com/sccn/liblsl/issues/29) that prevents liblsl in Unity from resolving streams from _other_ computers while running in editor, and also the built product but only when using `ContinuousResolver`. For this reason we recommend using `liblsl.resolve_stream` instead.
23+
* There is [currently a bug](https://github.com/sccn/liblsl/issues/29) that prevents liblsl in Unity from resolving streams from _other_ computers while running in editor, and also the built product but only when using `ContinuousResolver`. For this reason we recommend using `LSL.resolve_stream` instead.
2424

2525
```cs
2626
using System.Collections;
@@ -32,19 +32,22 @@ public class LSLInput : MonoBehaviour
3232
{
3333
public string StreamType = "EEG";
3434
public float scaleInput = 0.1f;
35-
LSL.StreamInfo[] streamInfos;
36-
LSL.StreamInlet streamInlet;
35+
36+
StreamInfo[] streamInfos;
37+
StreamInlet streamInlet;
38+
3739
float[] sample;
3840
private int channelCount = 0;
3941

4042
void Update()
4143
{
4244
if (streamInlet == null)
4345
{
46+
4447
streamInfos = LSL.resolve_stream("type", StreamType, 1, 0.0);
4548
if (streamInfos.Length > 0)
4649
{
47-
streamInlet = new LSL.StreamInlet(streamInfos[0]);
50+
streamInlet = new StreamInlet(streamInfos[0]);
4851
channelCount = streamInlet.info().channel_count();
4952
streamInlet.open_stream();
5053
}
@@ -79,41 +82,43 @@ public class LSLInput : MonoBehaviour
7982

8083
1. Attach a new component called LSLPosOutput to the cube.
8184
1. Edit it as follows:
82-
```cs
83-
using System.Collections;
84-
using System.Collections.Generic;
85-
using UnityEngine;
86-
using LSL;
85+
```cs
86+
using System.Collections;
87+
using System.Collections.Generic;
88+
using UnityEngine;
89+
using LSL;
8790

88-
public class LSLOutput : MonoBehaviour
89-
{
90-
private LSL.StreamOutlet outlet;
91-
private float[] currentSample;
91+
public class LSLOutput : MonoBehaviour
92+
{
93+
private StreamOutlet outlet;
94+
private float[] currentSample;
9295

93-
public string StreamName = "Unity.ExampleStream";
94-
public string StreamType = "Unity.StreamType";
95-
public string StreamId = "MyStreamID-Unity1234";
9696

97-
// Start is called before the first frame update
98-
void Start()
99-
{
100-
LSL.StreamInfo streamInfo = new LSL.StreamInfo(StreamName, StreamType, 3, Time.fixedDeltaTime * 1000, liblsl.channel_format_t.cf_float32);
101-
LSL.XMLElement chans = streamInfo.desc().append_child("channels");
102-
chans.append_child("channel").append_child_value("label", "X");
103-
chans.append_child("channel").append_child_value("label", "Y");
104-
chans.append_child("channel").append_child_value("label", "Z");
105-
outlet = new LSL.StreamOutlet(streamInfo);
106-
currentSample = new float[3];
107-
}
97+
public string StreamName = "Unity.ExampleStream";
98+
public string StreamType = "Unity.StreamType";
99+
public string StreamId = "MyStreamID-Unity1234";
108100

109-
// Update is called once per frame
110-
void FixedUpdate()
111-
{
112-
Vector3 pos = gameObject.transform.position;
113-
currentSample[0] = pos.x;
114-
currentSample[1] = pos.y;
115-
currentSample[2] = pos.z;
116-
outlet.push_sample(currentSample);
117-
}
101+
// Start is called before the first frame update
102+
void Start()
103+
{
104+
StreamInfo streamInfo = new StreamInfo(StreamName, StreamType, 3, Time.fixedDeltaTime * 1000, LSL.channel_format_t.cf_float32);
105+
XMLElement chans = streamInfo.desc().append_child("channels");
106+
chans.append_child("channel").append_child_value("label", "X");
107+
chans.append_child("channel").append_child_value("label", "Y");
108+
chans.append_child("channel").append_child_value("label", "Z");
109+
outlet = new StreamOutlet(streamInfo);
110+
currentSample = new float[3];
118111
}
119-
```
112+
113+
114+
// Update is called once per frame
115+
void FixedUpdate()
116+
{
117+
Vector3 pos = gameObject.transform.position;
118+
currentSample[0] = pos.x;
119+
currentSample[1] = pos.y;
120+
currentSample[2] = pos.z;
121+
outlet.push_sample(currentSample);
122+
}
123+
}
124+
```

0 commit comments

Comments
 (0)