| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- namespace SRDebugger.UI.Controls
- {
- using System;
- using SRF;
- using UnityEngine;
- using UnityEngine.UI;
- #if UNITY_5_5_OR_NEWER
- using UnityEngine.Profiling;
- #endif
- public class ProfilerMonoBlock : SRMonoBehaviourEx
- {
- private float _lastRefresh;
- [RequiredField]
- public Text CurrentUsedText;
- [RequiredField]
- public GameObject NotSupportedMessage;
- [RequiredField]
- public Slider Slider;
- [RequiredField]
- public Text TotalAllocatedText;
- private bool _isSupported;
- protected override void OnEnable()
- {
- base.OnEnable();
- #if UNITY_5_6_OR_NEWER
- _isSupported = Profiler.GetMonoUsedSizeLong() > 0;
- #else
- _isSupported = Profiler.GetMonoUsedSize() > 0;
- #endif
- NotSupportedMessage.SetActive(!_isSupported);
- CurrentUsedText.gameObject.SetActive(_isSupported);
- TriggerRefresh();
- }
- protected override void Update()
- {
- base.Update();
- if (SRDebug.Instance.IsDebugPanelVisible && (Time.realtimeSinceStartup - _lastRefresh > 1f))
- {
- TriggerRefresh();
- _lastRefresh = Time.realtimeSinceStartup;
- }
- }
- public void TriggerRefresh()
- {
- long max;
- long current;
- #if UNITY_5_6_OR_NEWER
- max = _isSupported ? Profiler.GetMonoHeapSizeLong() : GC.GetTotalMemory(false);
- current = Profiler.GetMonoUsedSizeLong();
- #else
- max = _isSupported ? Profiler.GetMonoHeapSize() : GC.GetTotalMemory(false);
- current = Profiler.GetMonoUsedSize();
- #endif
- var maxMb = (max >> 10);
- maxMb /= 1024; // On new line to workaround IL2CPP bug
- var currentMb = (current >> 10);
- currentMb /= 1024;
- Slider.maxValue = maxMb;
- Slider.value = currentMb;
- TotalAllocatedText.text = "Total: <color=#FFFFFF>{0}</color>MB".Fmt(maxMb);
- if (currentMb > 0)
- {
- CurrentUsedText.text = "<color=#FFFFFF>{0}</color>MB".Fmt(currentMb);
- }
- }
- public void TriggerCollection()
- {
- GC.Collect();
- TriggerRefresh();
- }
- }
- }
|