| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.FPSDisplay |
| | 8 | | { |
| | 9 | | public class FPSDisplay : MonoBehaviour |
| | 10 | | { |
| | 11 | | private const float REFRESH_SECONDS = 0.1f; |
| | 12 | |
|
| | 13 | | [SerializeField] internal List<DebugValue> valuesToUpdate; |
| | 14 | | [SerializeField] private Button closeButton; |
| | 15 | | [SerializeField] private CopyToClipboardButton copySceneToClipboard; |
| | 16 | | [SerializeField] private DebugSection memorySection; |
| | 17 | | [SerializeField] private RectTransform container; |
| | 18 | | [SerializeField] private PerformanceMetricsDataVariable performanceData; |
| | 19 | |
|
| | 20 | | private Dictionary<DebugValueEnum, Func<string>> updateValueDictionary; |
| | 21 | | private Vector3 containerStartAnchoredPosition; |
| | 22 | |
|
| | 23 | | private List<IDebugMetricModule> debugMetricsModules; |
| | 24 | | private SceneDebugMetricModule sceneDebugMetricModule; |
| | 25 | |
|
| | 26 | | private void Awake() |
| | 27 | | { |
| 0 | 28 | | containerStartAnchoredPosition = container.anchoredPosition; |
| | 29 | |
|
| 0 | 30 | | sceneDebugMetricModule = new SceneDebugMetricModule(); |
| 0 | 31 | | debugMetricsModules = new List<IDebugMetricModule> |
| | 32 | | { |
| | 33 | | new FPSDebugMetricModule(performanceData), |
| | 34 | | new SkyboxDebugMetricModule(), |
| | 35 | | new MemoryJSDebugMetricModule(), |
| | 36 | | new MemoryDesktopDebugMetricModule(), |
| | 37 | | new GeneralDebugMetricModule(), |
| | 38 | | sceneDebugMetricModule |
| | 39 | | }; |
| 0 | 40 | | updateValueDictionary = new Dictionary<DebugValueEnum, Func<string>>(); |
| 0 | 41 | | foreach (IDebugMetricModule debugMetricsModule in debugMetricsModules) |
| | 42 | | { |
| 0 | 43 | | debugMetricsModule.SetUpModule(updateValueDictionary); |
| | 44 | | } |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void Start() |
| | 48 | | { |
| 0 | 49 | | closeButton.onClick.AddListener(() =>DataStore.i.debugConfig.isFPSPanelVisible.Set(false)); |
| 0 | 50 | | copySceneToClipboard.SetFuncToCopy(() => sceneDebugMetricModule.GetSceneID()); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnEnable() |
| | 54 | | { |
| 0 | 55 | | container.anchoredPosition = containerStartAnchoredPosition; |
| 0 | 56 | | foreach (IDebugMetricModule debugMetricsModule in debugMetricsModules) |
| | 57 | | { |
| 0 | 58 | | debugMetricsModule.EnableModule(); |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | StartCoroutine(UpdateLabelLoop()); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void OnDisable() |
| | 65 | | { |
| 0 | 66 | | foreach (IDebugMetricModule debugMetricsModule in debugMetricsModules) |
| | 67 | | { |
| 0 | 68 | | debugMetricsModule.DisableModule(); |
| | 69 | | } |
| 0 | 70 | | StopAllCoroutines(); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private IEnumerator UpdateLabelLoop() |
| | 74 | | { |
| 0 | 75 | | while (performanceData.Get().totalSeconds <= 0) |
| | 76 | | { |
| 0 | 77 | | yield return null; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | while (true) |
| | 81 | | { |
| 0 | 82 | | foreach (IDebugMetricModule debugMetricsModule in debugMetricsModules) |
| | 83 | | { |
| 0 | 84 | | debugMetricsModule.UpdateModule(); |
| | 85 | | } |
| 0 | 86 | | for (var i = 0; i < valuesToUpdate.Count; i++) |
| | 87 | | { |
| 0 | 88 | | if (valuesToUpdate[i].isActiveAndEnabled) |
| | 89 | | { |
| 0 | 90 | | valuesToUpdate[i].SetValue(updateValueDictionary[valuesToUpdate[i].debugValueEnum].Invoke()); |
| | 91 | | } |
| | 92 | | } |
| 0 | 93 | | yield return new WaitForSeconds(REFRESH_SECONDS); |
| | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnDestroy() |
| | 98 | | { |
| 0 | 99 | | closeButton.onClick.RemoveAllListeners(); |
| 0 | 100 | | foreach (IDebugMetricModule debugMetricsModule in debugMetricsModules) |
| | 101 | | { |
| 0 | 102 | | debugMetricsModule.Dispose(); |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void AddValueToUpdate(DebugValue valueToUpdate) |
| | 107 | | { |
| 0 | 108 | | valuesToUpdate.Add(valueToUpdate); |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | } |
| | 112 | | } |