Skip to content

test: Nested NetworkObjects in prefabs throws a warning [MTT-2976] #1969

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

Merged
merged 7 commits into from
May 19, 2022
Merged
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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Added

- Added test to ensure a warning occurs when nesting NetworkObjects in a NetworkPrefab (#1969)
- Added `NetworkManager.RemoveNetworkPrefab(...)` to remove a prefab from the prefabs list (#1950)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public Func<ConnectionApprovalRequest, ConnectionApprovalResponse> ConnectionApp
internal static event Action OnSingletonReady;

#if UNITY_EDITOR
private void OnValidate()
internal void OnValidate()
{
if (NetworkConfig == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using NUnit.Framework;
using UnityEngine;
using Unity.Netcode.Editor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

namespace Unity.Netcode.EditorTests
Expand Down Expand Up @@ -78,5 +80,37 @@ public void NetworkObjectNotAllowed([Values] NetworkObjectPlacement networkObjec
// Clean up
Object.DestroyImmediate(gameObject);
}

[Test]
public void NestedNetworkObjectPrefabCheck()
{
// Setup
var networkManagerObject = new GameObject(nameof(NestedNetworkObjectPrefabCheck));
var networkManager = networkManagerObject.AddComponent<NetworkManager>();
networkManager.NetworkConfig = new NetworkConfig();

var parent = new GameObject("Parent").AddComponent<NetworkObject>();
var child = new GameObject("Child").AddComponent<NetworkObject>();

// Set parent
child.transform.SetParent(parent.transform);

// Make it a prefab, warning only applies to prefabs
networkManager.AddNetworkPrefab(parent.gameObject);

// Mark scene as dirty to ensure OnValidate actually runs
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());

// Force OnValidate
networkManager.OnValidate();

// Expect a warning
LogAssert.Expect(LogType.Warning, $"[Netcode] {NetworkManager.PrefabDebugHelper(networkManager.NetworkConfig.NetworkPrefabs[0])} has child {nameof(NetworkObject)}(s) but they will not be spawned across the network (unsupported {nameof(NetworkPrefab)} setup)");

// Clean up
Object.DestroyImmediate(networkManagerObject);
Object.DestroyImmediate(parent);

}
}
}