Skip to content

Commit c977dd8

Browse files
committed
feat: Update StartServerProcessAndScan with editor and intensity slider
1 parent eb2288c commit c977dd8

File tree

6 files changed

+123
-35
lines changed

6 files changed

+123
-35
lines changed

Assets/Example/Example.unity

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ MonoBehaviour:
152152
m_Script: {fileID: 11500000, guid: 19284159f105daa479c003113a1ed815, type: 3}
153153
m_Name:
154154
m_EditorClassIdentifier:
155+
intensity: 0.5
155156
--- !u!4 &352825605
156157
Transform:
157158
m_ObjectHideFlags: 0
@@ -164,7 +165,7 @@ Transform:
164165
m_LocalScale: {x: 1, y: 1, z: 1}
165166
m_Children: []
166167
m_Father: {fileID: 0}
167-
m_RootOrder: 2
168+
m_RootOrder: 0
168169
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
169170
--- !u!1 &810695056
170171
GameObject:
@@ -257,7 +258,7 @@ Transform:
257258
m_LocalScale: {x: 1, y: 1, z: 1}
258259
m_Children: []
259260
m_Father: {fileID: 0}
260-
m_RootOrder: 1
261+
m_RootOrder: 2
261262
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
262263
--- !u!1 &1455282053
263264
GameObject:
@@ -340,5 +341,5 @@ Transform:
340341
m_LocalScale: {x: 1, y: 1, z: 1}
341342
m_Children: []
342343
m_Father: {fileID: 0}
343-
m_RootOrder: 0
344+
m_RootOrder: 1
344345
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Assets/Example/Scripts/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
5+
[CustomEditor(typeof(StartServerProcessAndScan))]
6+
public class StartServerProcessAndScanEditor : Editor
7+
{
8+
private StartServerProcessAndScan Target { get; set; }
9+
10+
private void OnEnable()
11+
{
12+
Target = (StartServerProcessAndScan) target;
13+
}
14+
15+
public override void OnInspectorGUI()
16+
{
17+
base.OnInspectorGUI();
18+
if (Application.isPlaying) {
19+
EditorGUILayout.Space();
20+
GUILayout.Label("Connected Devices", EditorStyles.boldLabel);
21+
Target.Devices.ForEach(d => GUILayout.Label(d.Name));
22+
}
23+
}
24+
}

Assets/Example/Scripts/Editor/StartServerProcessAndScanEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Example/Scripts/StartServerProcessAndScan.cs

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,101 @@
33
//
44
// This is just a generic behavior, so you can attach it to any active object in
55
// your scene and it'll run on scene load.
6+
67
using System;
7-
using UnityEngine;
8+
using System.Collections.Generic;
9+
using Buttplug;
810
using ButtplugUnity;
11+
using UnityEngine;
912

1013
public class StartServerProcessAndScan : MonoBehaviour
1114
{
15+
[SerializeField, Range(0, 1)] private float intensity = 0.5f;
1216

1317
private ButtplugUnityClient client;
1418

15-
// Start is called before the first frame update
16-
async void Start() {
19+
public List<ButtplugClientDevice> Devices { get; } = new List<ButtplugClientDevice>();
20+
21+
private async void Start()
22+
{
1723
client = new ButtplugUnityClient("Test Client");
18-
UnityEngine.Debug.Log("Trying to create client");
24+
Log("Trying to create client");
1925

2026
// Set up client event handlers before we connect.
21-
client.DeviceAdded += async (aObj, aDeviceEventArgs) => {
22-
UnityEngine.Debug.Log($"Device {aDeviceEventArgs.Device.Name} Connected!");
23-
await aDeviceEventArgs.Device.SendVibrateCmd(0.5);
24-
};
25-
26-
client.DeviceRemoved += (aObj, aDeviceEventArgs) =>
27-
UnityEngine.Debug.Log($"Device {aDeviceEventArgs.Device.Name} Removed!");
28-
29-
client.ScanningFinished += (aObj, aScanningFinishedArgs) =>
30-
UnityEngine.Debug.Log("Device scanning is finished!");
27+
client.DeviceAdded += AddDevice;
28+
client.DeviceRemoved += RemoveDevice;
29+
client.ScanningFinished += ScanFinished;
3130

3231
// Try to create the client.
3332
try {
34-
await ButtplugUnityHelper.StartProcessAndCreateClient(client, new ButtplugUnityOptions {
35-
// Since this is an example, we'll have the unity class output everything its doing to the logs.
36-
OutputDebugMessages = true,
37-
});
38-
} catch (ApplicationException e) {
39-
UnityEngine.Debug.Log("Got an error while starting client");
40-
UnityEngine.Debug.Log(e);
33+
await ButtplugUnityHelper.StartProcessAndCreateClient(client, new ButtplugUnityOptions {
34+
// Since this is an example, we'll have the unity class output everything its doing to the logs.
35+
OutputDebugMessages = true,
36+
});
37+
}
38+
catch (ApplicationException e) {
39+
Log("Got an error while starting client");
40+
Log(e);
4141
return;
4242
}
43-
43+
4444
await client.StartScanningAsync();
4545
}
4646

47-
// Update is called once per frame
48-
void Update()
47+
private async void OnDestroy()
4948
{
50-
}
49+
Devices.Clear();
5150

52-
async void OnDestroy()
53-
{
5451
// On object shutdown, disconnect the client and just kill the server
5552
// process. Server process shutdown will be cleaner in future builds.
56-
await this.client?.DisconnectAsync();
57-
this.client?.Dispose();
58-
this.client = null;
53+
if (client != null)
54+
{
55+
client.DeviceAdded -= AddDevice;
56+
client.DeviceRemoved -= RemoveDevice;
57+
client.ScanningFinished -= ScanFinished;
58+
await client.DisconnectAsync();
59+
client.Dispose();
60+
client = null;
61+
}
62+
5963
ButtplugUnityHelper.StopServer();
60-
UnityEngine.Debug.Log("I am destroyed now");
64+
Log("I am destroyed now");
65+
}
66+
67+
private void OnValidate()
68+
{
69+
UpdateDevices();
70+
}
71+
72+
private void UpdateDevices()
73+
{
74+
foreach (ButtplugClientDevice device in Devices)
75+
{
76+
device.SendVibrateCmd(intensity);
77+
}
78+
}
79+
80+
private void AddDevice(object sender, DeviceAddedEventArgs e)
81+
{
82+
Log($"Device {e.Device.Name} Connected!");
83+
Devices.Add(e.Device);
84+
UpdateDevices();
85+
}
86+
87+
private void RemoveDevice(object sender, DeviceRemovedEventArgs e)
88+
{
89+
Log($"Device {e.Device.Name} Removed!");
90+
Devices.Remove(e.Device);
91+
UpdateDevices();
92+
}
93+
94+
private void ScanFinished(object sender, EventArgs e)
95+
{
96+
Log("Device scanning is finished!");
97+
}
98+
99+
private void Log(object text)
100+
{
101+
Debug.Log("<color=red>Buttplug:</color> " + text, this);
61102
}
62-
}
103+
}

UserSettings/EditorUserSettings.asset

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ EditorUserSettings:
55
m_ObjectHideFlags: 0
66
serializedVersion: 4
77
m_ConfigSettings:
8+
RecentlyUsedScenePath-0:
9+
value: 224247031146467e150f01321c2610350e16153a202d68252320092a
10+
flags: 0
811
vcSharedLogLevel:
912
value: 0d5e400f0650
1013
flags: 0

0 commit comments

Comments
 (0)