| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class ApplicationFocusService : IApplicationFocusService |
| | 7 | | { |
| | 8 | |
|
| | 9 | | public event Action OnApplicationFocus; |
| | 10 | | public event Action OnApplicationFocusLost; |
| | 11 | | private bool currentFocusState; |
| | 12 | |
|
| | 13 | | public void Initialize() |
| | 14 | | { |
| 667 | 15 | | Application.focusChanged += FocusChange; |
| 667 | 16 | | currentFocusState = Application.isFocused; |
| 667 | 17 | | } |
| | 18 | | private void FocusChange(bool focus) |
| | 19 | | { |
| 6 | 20 | | if (currentFocusState == focus) |
| 0 | 21 | | return; |
| | 22 | |
|
| 6 | 23 | | if (focus) |
| | 24 | | { |
| 4 | 25 | | OnFocusGained(); |
| 4 | 26 | | } |
| | 27 | | else |
| | 28 | | { |
| 2 | 29 | | OnFocusLost(); |
| | 30 | | } |
| 6 | 31 | | currentFocusState = focus; |
| 6 | 32 | | } |
| | 33 | |
|
| | 34 | | internal void OnFocusGained() |
| | 35 | | { |
| 5 | 36 | | OnApplicationFocus?.Invoke(); |
| 1 | 37 | | } |
| | 38 | |
|
| | 39 | | internal void OnFocusLost() |
| | 40 | | { |
| 3 | 41 | | OnApplicationFocusLost?.Invoke(); |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Dispose() |
| | 45 | | { |
| 665 | 46 | | Application.focusChanged -= FocusChange; |
| 665 | 47 | | } |
| | 48 | |
|
| | 49 | | } |