rabidus-test/Assets/DeviceDisconnectController.cs

58 lines
1.3 KiB
C#
Raw Normal View History

2023-09-04 12:48:24 +03:00
using BNG;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.XR;
public class DeviceDisconnectController : MonoBehaviour
{
public UnityEvent OnTotalDisconnect;
private Coroutine coroutine;
private InputBridge inputBridge;
private void Awake()
{
inputBridge = GetComponent<InputBridge>();
}
private void OnEnable()
{
inputBridge.OnConnected.AddListener(OnDeviceConnected);
inputBridge.OnDisconnected.AddListener(OnDeviceDisconnected);
}
private void OnDisable()
{
inputBridge.OnConnected.RemoveListener(OnDeviceConnected);
inputBridge.OnDisconnected.RemoveListener(OnDeviceDisconnected);
}
private void OnDeviceConnected()
{
if (coroutine != null)
{
StopCoroutine(coroutine);
coroutine = null;
Debug.Log("Reconnected..");
}
}
private void OnDeviceDisconnected()
{
if (coroutine != null)
return;
coroutine = StartCoroutine(DisconnectTime());
}
private IEnumerator DisconnectTime()
{
Debug.Log("Disconnected. Wait for Reconnect..");
yield return new WaitForSeconds(15);
OnTotalDisconnect?.Invoke();
}
}