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

feat: initialized callback #2973

Open
wants to merge 4 commits into
base: develop-2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public bool DAHost
/// </summary>
public event OnSessionOwnerPromotedDelegateHandler OnSessionOwnerPromoted;

/// <summary>
/// When a server or client is initializing, before the network connecting is established
/// </summary>
public event Action OnInitialized;

internal void SetSessionOwner(ulong sessionOwner)
{
var previousSessionOwner = CurrentSessionOwner;
Expand Down Expand Up @@ -1166,6 +1171,17 @@ internal void Initialize(bool server)

NetworkConfig.InitializePrefabs();
PrefabHandler.RegisterPlayerPrefab();

// Invoke initialized callback
try
{
OnInitialized?.Invoke();
}
catch (Exception)
{
Shutdown();
throw;
}
}

private enum StartType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using NUnit.Framework;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Unity.Netcode.RuntimeTests
{
internal class NetworkManagerInitializedTests
{
[Test]
[TestCase(true)]
[TestCase(false)]
public void OnInitializedIsCalled(bool isServer)
{
var networkManagerObject = CreateNetworkManager(out var networkManager);

var callbackHit = false;

networkManager.OnInitialized += () => callbackHit = true;
networkManager.Initialize(isServer);

Assert.True(callbackHit, "OnInitialized callback was never triggered");

// Clean up
Object.Destroy(networkManagerObject);
}

[Test]
public void OnInitializedShutsDownOnException()
{
var networkManagerObject = CreateNetworkManager(out var networkManager);

networkManager.OnInitialized += () => throw new Exception();

try
{
networkManager.StartServer();
}
catch (Exception)
{
// do nothing
}

Assert.True(networkManager.ShutdownInProgress, "Manager did not shutdown");

// Clean up
Object.Destroy(networkManagerObject);
}

private GameObject CreateNetworkManager(out NetworkManager networkManager)
{
var networkManagerObject = new GameObject(nameof(OnInitializedIsCalled));

var unityTransport = networkManagerObject.AddComponent<UnityTransport>();
networkManager = networkManagerObject.AddComponent<NetworkManager>();
networkManager.NetworkConfig = new NetworkConfig() { NetworkTransport = unityTransport };
return networkManagerObject;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading