Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event sub-unsub only for first static instance #2

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions Assets/Mirage.Profiler/Runtime/NetworkProfilerRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,35 @@ private void Start()
Debug.LogWarning("NetworkProfilerBehaviour only works in editor");
return;
#endif


if (Instance == null)
{
#if UNITY_EDITOR
_lastProcessedFrame = ProfilerDriver.lastFrameIndex;
_lastProcessedFrame = ProfilerDriver.lastFrameIndex;
#endif

var provider = new NetworkInfoProvider();
_sentCounter = new CountRecorder(null, provider, Counters.SentCount, Counters.SentBytes, Counters.SentPerSecond);
_receivedCounter = new CountRecorder(null, provider, Counters.ReceiveCount, Counters.ReceiveBytes, Counters.ReceivePerSecond);
NetworkDiagnostics.InMessageEvent += _receivedCounter.OnMessage;
NetworkDiagnostics.OutMessageEvent += _sentCounter.OnMessage;

Debug.Assert(Instance == null);
Instance = this;
DontDestroyOnLoad(this);
var provider = new NetworkInfoProvider();
_sentCounter = new CountRecorder(null, provider, Counters.SentCount, Counters.SentBytes, Counters.SentPerSecond);
_receivedCounter = new CountRecorder(null, provider, Counters.ReceiveCount, Counters.ReceiveBytes, Counters.ReceivePerSecond);
NetworkDiagnostics.InMessageEvent += _receivedCounter.OnMessage;
NetworkDiagnostics.OutMessageEvent += _sentCounter.OnMessage;

Instance = this;
DontDestroyOnLoad(this);
}
}

private void OnDestroy()
{
if (_receivedCounter != null)
NetworkDiagnostics.InMessageEvent -= _receivedCounter.OnMessage;
if (_sentCounter != null)
NetworkDiagnostics.OutMessageEvent -= _sentCounter.OnMessage;

Debug.Assert(Instance == this);
Instance = null;
if (Instance == this)
{
if (_receivedCounter != null)
NetworkDiagnostics.InMessageEvent -= _receivedCounter.OnMessage;
if (_sentCounter != null)
NetworkDiagnostics.OutMessageEvent -= _sentCounter.OnMessage;

Instance = null;
}
}

#if UNITY_EDITOR
Expand Down