This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/OwnerPermissionTests.cs
Unity Technologies 8060718e04 com.unity.netcode.gameobjects@1.3.1
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.3.1] - 2023-03-27

### Added

- Added detection and graceful handling of corrupt packets for additional safety. (#2419)

### Changed

- The UTP component UI has been updated to be more user-friendly for new users by adding a simple toggle to switch between local-only (127.0.0.1) and remote (0.0.0.0) binding modes, using the toggle "Allow Remote Connections" (#2408)
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.3. (#2450)
- `NetworkShow()` of `NetworkObject`s are delayed until the end of the frame to ensure consistency of delta-driven variables like `NetworkList`.
- Dirty `NetworkObject` are reset at end-of-frame and not at serialization time.
- `NetworkHide()` of an object that was just `NetworkShow()`n produces a warning, as remote clients will _not_ get a spawn/despawn pair.
- Renamed the NetworkTransform.SetState parameter `shouldGhostsInterpolate` to `teleportDisabled` for better clarity of what that parameter does. (#2228)
- Network prefabs are now stored in a ScriptableObject that can be shared between NetworkManagers, and have been exposed for public access. By default, a Default Prefabs List is created that contains all NetworkObject prefabs in the project, and new NetworkManagers will default to using that unless that option is turned off in the Netcode for GameObjects settings. Existing NetworkManagers will maintain their existing lists, which can be migrated to the new format via a button in their inspector. (#2322)

### Fixed

- Fixed issue where changes to a layer's weight would not synchronize unless a state transition was occurring.(#2399)
- Fixed issue where `NetworkManager.LocalClientId` was returning the `NetworkTransport.ServerClientId` as opposed to the `NetworkManager.m_LocalClientId`. (#2398)
- Fixed issue where a dynamically spawned `NetworkObject` parented under an in-scene placed `NetworkObject` would have its `InScenePlaced` value changed to `true`. This would result in a soft synchronization error for late joining clients. (#2396)
- Fixed a UTP test that was failing when you install Unity Transport package 2.0.0 or newer. (#2347)
- Fixed issue where `NetcodeSettingsProvider` would throw an exception in Unity 2020.3.x versions. (#2345)
- Fixed server side issue where, depending upon component ordering, some NetworkBehaviour components might not have their OnNetworkDespawn method invoked if the client side disconnected. (#2323)
- Fixed a case where data corruption could occur when using UnityTransport when reaching a certain level of send throughput. (#2332)
- Fixed an issue in `UnityTransport` where an exception would be thrown if starting a Relay host/server on WebGL. This exception should only be thrown if using direct connections (where WebGL can't act as a host/server). (#2321)
- Fixed `NetworkAnimator` issue where it was not checking for `AnimatorStateTtansition.destinationStateMachine` and any possible sub-states defined within it. (#2309)
- Fixed `NetworkAnimator` issue where the host client was receiving the ClientRpc animation updates when the host was the owner.(#2309)
- Fixed `NetworkAnimator` issue with using pooled objects and when specific properties are cleaned during despawn and destroy.(#2309)
- Fixed issue where `NetworkAnimator` was checking for animation changes when the associated `NetworkObject` was not spawned.(#2309)
- Corrected an issue with the documentation for BufferSerializer (#2401)
2023-03-27 00:00:00 +00:00

210 lines
8.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
namespace Unity.Netcode.RuntimeTests
{
public class OwnerPermissionObject : NetworkBehaviour
{
// indexed by [object, machine]
public static OwnerPermissionObject[,] Objects = new OwnerPermissionObject[3, 3];
public static int CurrentlySpawning = 0;
public static List<OwnerPermissionObject> ClientTargetedNetworkObjects = new List<OwnerPermissionObject>();
// a client-owned NetworkVariable
public NetworkVariable<int> MyNetworkVariableOwner;
// a server-owned NetworkVariable
public NetworkVariable<int> MyNetworkVariableServer;
// a client-owned NetworkVariable
public NetworkList<int> MyNetworkListOwner;
// a server-owned NetworkVariable
public NetworkList<int> MyNetworkListServer;
// verifies two lists are identical
public static void CheckLists(NetworkList<int> listA, NetworkList<int> listB)
{
Debug.Assert(listA.Count == listB.Count);
for (var i = 0; i < listA.Count; i++)
{
Debug.Assert(listA[i] == listB[i]);
}
}
// verifies all objects have consistent lists on all clients
public static void VerifyConsistency()
{
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
CheckLists(Objects[objectIndex, 0].MyNetworkListOwner, Objects[objectIndex, 1].MyNetworkListOwner);
CheckLists(Objects[objectIndex, 0].MyNetworkListOwner, Objects[objectIndex, 2].MyNetworkListOwner);
CheckLists(Objects[objectIndex, 0].MyNetworkListServer, Objects[objectIndex, 1].MyNetworkListServer);
CheckLists(Objects[objectIndex, 0].MyNetworkListServer, Objects[objectIndex, 2].MyNetworkListServer);
}
}
public override void OnNetworkSpawn()
{
Objects[CurrentlySpawning, NetworkManager.LocalClientId] = GetComponent<OwnerPermissionObject>();
Debug.Log($"Object index ({CurrentlySpawning}) spawned on client {NetworkManager.LocalClientId}");
}
private void Awake()
{
MyNetworkVariableOwner = new NetworkVariable<int>(writePerm: NetworkVariableWritePermission.Owner);
MyNetworkVariableOwner.OnValueChanged += OwnerChanged;
MyNetworkVariableServer = new NetworkVariable<int>(writePerm: NetworkVariableWritePermission.Server);
MyNetworkVariableServer.OnValueChanged += ServerChanged;
MyNetworkListOwner = new NetworkList<int>(writePerm: NetworkVariableWritePermission.Owner);
MyNetworkListOwner.OnListChanged += ListOwnerChanged;
MyNetworkListServer = new NetworkList<int>(writePerm: NetworkVariableWritePermission.Server);
MyNetworkListServer.OnListChanged += ListServerChanged;
}
public void OwnerChanged(int before, int after)
{
}
public void ServerChanged(int before, int after)
{
}
public void ListOwnerChanged(NetworkListEvent<int> listEvent)
{
}
public void ListServerChanged(NetworkListEvent<int> listEvent)
{
}
}
public class OwnerPermissionHideTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;
private GameObject m_PrefabToSpawn;
protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = CreateNetworkObjectPrefab("OwnerPermissionObject");
m_PrefabToSpawn.AddComponent<OwnerPermissionObject>();
}
[UnityTest]
public IEnumerator OwnerPermissionTest()
{
// create 3 objects
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
OwnerPermissionObject.CurrentlySpawning = objectIndex;
NetworkManager ownerManager = m_ServerNetworkManager;
if (objectIndex != 0)
{
ownerManager = m_ClientNetworkManagers[objectIndex - 1];
}
SpawnObject(m_PrefabToSpawn, ownerManager);
// wait for each object to spawn on each client
for (var clientIndex = 0; clientIndex < 3; clientIndex++)
{
while (OwnerPermissionObject.Objects[objectIndex, clientIndex] == null)
{
yield return new WaitForSeconds(0.0f);
}
}
}
var nextValueToWrite = 1;
var serverIndex = 0;
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
for (var clientWriting = 0; clientWriting < 3; clientWriting++)
{
// ==== Server-writable NetworkVariable ====
var gotException = false;
Debug.Log($"Writing to server-write variable on object {objectIndex} on client {clientWriting}");
try
{
nextValueToWrite++;
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableServer.Value = nextValueToWrite;
}
catch (Exception)
{
gotException = true;
}
// Verify server-owned netvar can only be written by server
Debug.Assert(gotException == (clientWriting != serverIndex));
// ==== Owner-writable NetworkVariable ====
gotException = false;
Debug.Log($"Writing to owner-write variable on object {objectIndex} on client {clientWriting}");
try
{
nextValueToWrite++;
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableOwner.Value = nextValueToWrite;
}
catch (Exception)
{
gotException = true;
}
// Verify client-owned netvar can only be written by owner
Debug.Assert(gotException == (clientWriting != objectIndex));
// ==== Server-writable NetworkList ====
gotException = false;
Debug.Log($"Writing to server-write list on object {objectIndex} on client {clientWriting}");
try
{
nextValueToWrite++;
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListServer.Add(nextValueToWrite);
}
catch (Exception)
{
gotException = true;
}
// Verify server-owned networkList can only be written by server
Debug.Assert(gotException == (clientWriting != serverIndex));
// ==== Owner-writable NetworkList ====
gotException = false;
Debug.Log($"Writing to owner-write list on object {objectIndex} on client {clientWriting}");
try
{
nextValueToWrite++;
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListOwner.Add(nextValueToWrite);
}
catch (Exception)
{
gotException = true;
}
// Verify client-owned networkList can only be written by owner
Debug.Assert(gotException == (clientWriting != objectIndex));
yield return WaitForTicks(m_ServerNetworkManager, 5);
yield return WaitForTicks(m_ClientNetworkManagers[0], 5);
yield return WaitForTicks(m_ClientNetworkManagers[1], 5);
OwnerPermissionObject.VerifyConsistency();
}
}
}
}
}