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+
67using System ;
7- using UnityEngine ;
8+ using System . Collections . Generic ;
9+ using Buttplug ;
810using ButtplugUnity ;
11+ using UnityEngine ;
912
1013public 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+ }
0 commit comments