diff --git a/Svelto.ECS/ECS/EnginesRootEngines.cs b/Svelto.ECS/ECS/EnginesRootEngines.cs index e2ebfaa..e8b557d 100644 --- a/Svelto.ECS/ECS/EnginesRootEngines.cs +++ b/Svelto.ECS/ECS/EnginesRootEngines.cs @@ -14,6 +14,17 @@ namespace Svelto.ECS { public partial class EnginesRoot : IDisposable { +#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR + /// + /// I still need to find a good solution for this. Need to move somewhere else + /// + static EnginesRoot() + { + UnityEngine.GameObject debugEngineObject = new UnityEngine.GameObject("Svelto.ECS.Profiler"); + debugEngineObject.gameObject.AddComponent(); + UnityEngine.GameObject.DontDestroyOnLoad(debugEngineObject); + } +#endif /// /// Engines root contextualize your engines and entities. You don't need to limit yourself to one EngineRoot /// as multiple engines root could promote separation of scopes. The EntitySubmissionScheduler checks @@ -40,10 +51,6 @@ namespace Svelto.ECS _scheduler = entityViewScheduler; _scheduler.Schedule(new WeakAction(SubmitEntityViews)); -#if ENGINE_PROFILER_ENABLED && UNITY_EDITOR - UnityEngine.GameObject debugEngineObject = new UnityEngine.GameObject("Engine Debugger"); - debugEngineObject.gameObject.AddComponent(); -#endif } public void AddEngine(IEngine engine) diff --git a/Svelto.ECS/ECS/Profiler/EngineInfo.cs b/Svelto.ECS/ECS/Profiler/EngineInfo.cs index c8fd453..038af1c 100644 --- a/Svelto.ECS/ECS/Profiler/EngineInfo.cs +++ b/Svelto.ECS/ECS/Profiler/EngineInfo.cs @@ -40,27 +40,16 @@ namespace Svelto.ECS.Profiler double _maxRemoveDuration; int _entityViewsRemovedCount; - public IEngine engine { get { return _engine; } } public string engineName { get { return _engineName; } } - public Type engineType { get { return _engineType; } } - - public double lastUpdateDuration { get { return _lastUpdateDuration[(int) UpdateType.Update]; } } - public double lastFixedUpdateDuration { get { return _lastUpdateDuration[(int)UpdateType.LateUpdate]; } } - public double lastLateUpdateDuration { get { return _lastUpdateDuration[(int)UpdateType.FixedUpdate]; } } - + public double minAddDuration { get { return _minAddDuration; } } public double minRemoveDuration { get { return _minRemoveDuration; } } - public double minUpdateDuration { get { return _minUpdateDuration[(int)UpdateType.Update]; } } public double maxAddDuration { get { return _maxAddDuration; } } public double maxRemoveDuration { get { return _maxRemoveDuration; } } - public double maxUpdateDuration { get { return _maxUpdateDuration[(int)UpdateType.Update]; } } public double averageAddDuration { get { return _entityViewsAddedCount == 0 ? 0 : _accumulatedAddDuration / _entityViewsAddedCount; } } public double averageRemoveDuration { get { return _entityViewsRemovedCount == 0 ? 0 : _accumulatedRemoveDuration / _entityViewsRemovedCount; } } - public double averageUpdateDuration { get { return _updateFrameTimes[(int)UpdateType.Update].Count == 0 ? 0 : _accumulatedUpdateDuration[(int)UpdateType.Update] / _updateFrameTimes[(int)UpdateType.Update].Count; } } - public double averageLateUpdateDuration { get { return _updateFrameTimes[(int)UpdateType.LateUpdate].Count == 0 ? 0 : _accumulatedUpdateDuration[(int)UpdateType.LateUpdate] / _updateFrameTimes[(int)UpdateType.LateUpdate].Count; } } - public double averageFixedUpdateDuration { get { return _updateFrameTimes[(int)UpdateType.FixedUpdate].Count == 0 ? 0 : _accumulatedUpdateDuration[(int)UpdateType.FixedUpdate] / _updateFrameTimes[(int)UpdateType.FixedUpdate].Count; } } public EngineInfo(IEngine engine) { @@ -79,42 +68,6 @@ namespace Svelto.ECS.Profiler ResetDurations(); } - public void AddUpdateDuration(double updateDuration) - { - AddUpdateDurationForType(updateDuration, (int)UpdateType.Update); - } - - public void AddLateUpdateDuration(double updateDuration) - { - AddUpdateDurationForType(updateDuration, (int)UpdateType.LateUpdate); - } - - public void AddFixedUpdateDuration(double updateDuration) - { - AddUpdateDurationForType(updateDuration, (int)UpdateType.FixedUpdate); - } - - void AddUpdateDurationForType(double updateDuration, int updateType) - { - if (updateDuration < _minUpdateDuration[updateType] || _minUpdateDuration[updateType] == 0) - { - _minUpdateDuration[updateType] = updateDuration; - } - if (updateDuration > _maxUpdateDuration[updateType]) - { - _maxUpdateDuration[updateType] = updateDuration; - } - - if (_updateFrameTimes[updateType].Count == NUM_FRAMES_TO_AVERAGE) - { - _accumulatedUpdateDuration[updateType] -= _updateFrameTimes[updateType].Dequeue(); - } - - _accumulatedUpdateDuration[updateType] += updateDuration; - _updateFrameTimes[updateType].Enqueue(updateDuration); - _lastUpdateDuration[updateType] = updateDuration; - } - public void AddAddDuration(double duration) { if (duration < _minAddDuration || _minAddDuration == 0) diff --git a/Svelto.ECS/ECS/Profiler/EngineProfiler.cs b/Svelto.ECS/ECS/Profiler/EngineProfiler.cs index 3e10786..a74333b 100644 --- a/Svelto.ECS/ECS/Profiler/EngineProfiler.cs +++ b/Svelto.ECS/ECS/Profiler/EngineProfiler.cs @@ -19,9 +19,9 @@ namespace Svelto.ECS.Profiler { _stopwatch.Start(); engine.Add(entityView); - _stopwatch.Reset(); - + _stopwatch.Stop(); info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds); + _stopwatch.Reset(); } } @@ -32,9 +32,10 @@ namespace Svelto.ECS.Profiler { _stopwatch.Start(); engine.Remove(entityView); - _stopwatch.Reset(); + _stopwatch.Stop(); info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds); + _stopwatch.Reset(); } } diff --git a/Svelto.ECS/ECS/Profiler/EngineProfilerBehaviour.cs b/Svelto.ECS/ECS/Profiler/EngineProfilerBehaviour.cs index a350ae7..b9bd384 100644 --- a/Svelto.ECS/ECS/Profiler/EngineProfilerBehaviour.cs +++ b/Svelto.ECS/ECS/Profiler/EngineProfilerBehaviour.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using UnityEngine; +using UnityEngine.SceneManagement; //This profiler is based on the Entitas Visual Debugging tool //https://github.com/sschmid/Entitas-CSharp @@ -16,6 +17,21 @@ namespace Svelto.ECS.Profiler { EngineProfiler.ResetDurations(); } + + void OnEnable() + { + SceneManager.sceneLoaded += OnLevelFinishedLoading; + } + + void OnDisable() + { + SceneManager.sceneLoaded -= OnLevelFinishedLoading; + } + + void OnLevelFinishedLoading(Scene arg0, LoadSceneMode arg1) + { + ResetDurations(); + } } } #endif \ No newline at end of file