public static IObservable <NamedControllerId> ActiveJoystick() { return(UnityObservable.CreateUpdate <NamedControllerId>(observer => { var xinputJoystickId = XInput.AnyJoystick(); var unityJoystickId = UnityInputMaps.AnyJoystick(); if (xinputJoystickId.HasValue) { var controllerId = new ControllerId.XInput(xinputJoystickId.Value); observer.OnNext(new NamedControllerId(controllerId, "XInput controller")); } else if (unityJoystickId.HasValue) { var controllerId = new ControllerId.Unity(unityJoystickId.Value); var controllerName = UnityEngine.Input.GetJoystickNames()[controllerId.Id]; observer.OnNext(new NamedControllerId(controllerId, controllerName)); } }) // XInput takes presedence over unity input because we know how to handle it better. // So if we detect an XInput device we ignore the Unity device(s) detected during the // same window. .Window(TimeSpan.FromSeconds(0.8f), Scheduler.TaskPool) .SelectMany(inputSources => { return inputSources.Scan((selectedInputSource, inputSource) => { if (selectedInputSource.ControllerId is ControllerId.XInput) { return selectedInputSource; } return inputSource; }).TakeLast(1); }) .ObserveOn(UnityThreadScheduler.MainThread));; }