Skip to content

Commit 8fb3fe2

Browse files
feat: client network transform move to samples [MTT-3406] (#629)
1 parent c8b10f2 commit 8fb3fe2

File tree

10 files changed

+102
-8
lines changed

10 files changed

+102
-8
lines changed

Assets/BossRoom/Scripts/Shared/Infrastructure/PubSub/MessageChannelDIExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure
24
{
35
public static class MessageChannelDIExtensions
@@ -6,7 +8,7 @@ public static void BindMessageChannelInstance<TMessage>(this DIScope scope)
68
{
79
scope.BindInstanceAsSingle<MessageChannel<TMessage>, IPublisher<TMessage>, ISubscriber<TMessage>, IMessageChannel<TMessage>>(new MessageChannel<TMessage>());
810
}
9-
public static void BindNetworkedMessageChannelInstance<TMessage>(this DIScope scope) where TMessage : unmanaged
11+
public static void BindNetworkedMessageChannelInstance<TMessage>(this DIScope scope) where TMessage : unmanaged, IComparable, IConvertible, IComparable<TMessage>, IEquatable<TMessage>
1012
{
1113
scope.BindInstanceAsSingle<NetworkedMessageChannel<TMessage>, IPublisher<TMessage>, ISubscriber<TMessage>, IMessageChannel<TMessage>>(new NetworkedMessageChannel<TMessage>());
1214
}

Assets/BossRoom/Scripts/Shared/Infrastructure/PubSub/NetworkedMessageChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure
1212
/// subscribing will be required each time a new session starts.
1313
/// </summary>
1414
/// <typeparam name="T"></typeparam>
15-
public class NetworkedMessageChannel<T> : MessageChannel<T> where T : unmanaged
15+
public class NetworkedMessageChannel<T> : MessageChannel<T> where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>
1616
{
1717
string m_Name;
1818

Assets/BossRoom/Scripts/Shared/NetworkNameState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Unity.Collections;
23
using Unity.Netcode;
34
using UnityEngine;
@@ -18,7 +19,7 @@ public class NetworkNameState : NetworkBehaviour
1819
/// </summary>
1920
public struct FixedPlayerName : INetworkSerializable
2021
{
21-
private FixedString32Bytes m_Name;
22+
ForceNetworkSerializeByMemcpy<FixedString32Bytes> m_Name; // using ForceNetworkSerializeByMemcpy to force compatibility between FixedString and NetworkSerializable
2223
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
2324
{
2425
serializer.SerializeValue(ref m_Name);

Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Unity.Netcode.Components;
2+
using Unity.Netcode;
3+
using UnityEngine;
4+
5+
namespace Unity.Multiplayer.Samples.Utilities.ClientAuthority
6+
{
7+
// TODO inherit from `NetworkBehaviour` instead of `NetworkTransform` to cut direct relationship between two
8+
// TODO change to owner netvar instead of RPC based
9+
/// <summary>
10+
/// Used for syncing a transform with client side changes. This includes host. Pure server as owner isn't supported by this. Please use NetworkTransform
11+
/// for transforms that'll always be owned by the server.
12+
/// </summary>
13+
[DisallowMultipleComponent]
14+
public class ClientNetworkTransform : NetworkTransform
15+
{
16+
/// <summary>
17+
/// Used to determine who can write to this transform. Owner client only.
18+
/// Changing this value alone will not allow you to create a NetworkTransform which can be written to by clients.
19+
/// We're using RPCs to send updated values from client to server. Netcode doesn't support client side network variable writing.
20+
/// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
21+
/// </summary>
22+
// This is public to make sure that users don't depend on this IsClient && IsOwner check in their code. If this logic changes in the future, we can make it invisible here
23+
24+
public override void OnNetworkSpawn()
25+
{
26+
base.OnNetworkSpawn();
27+
CanCommitToTransform = IsOwner;
28+
}
29+
30+
protected override void Update()
31+
{
32+
CanCommitToTransform = IsOwner;
33+
base.Update();
34+
if (NetworkManager.Singleton != null && (NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsListening))
35+
{
36+
if (CanCommitToTransform)
37+
{
38+
TryCommitTransformToServer(transform, NetworkManager.LocalTime.Time);
39+
}
40+
}
41+
}
42+
43+
protected override bool OnIsServerAuthoritatitive()
44+
{
45+
return false;
46+
}
47+
}
48+
}

Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/ClientNetworkTransform.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Unity.Multiplayer.Samples.Utilities.ClientAuthority",
3+
"rootNamespace": "",
4+
"references": [
5+
"Unity.Netcode.Components",
6+
"Unity.Netcode.Runtime"
7+
],
8+
"includePlatforms": [],
9+
"excludePlatforms": [],
10+
"allowUnsafeCode": false,
11+
"overrideReferences": false,
12+
"precompiledReferences": [],
13+
"autoReferenced": true,
14+
"defineConstraints": [],
15+
"versionDefines": [],
16+
"noEngineReferences": false
17+
}

Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/com.unity.multiplayer.samples.utilities.clientauthority.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.multiplayer.samples.coop/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "com.unity.multiplayer.samples.coop",
3-
"displayName": "Boss Room",
4-
"version": "1.1.0-pre",
3+
"displayName": "Multiplayer Samples Utilities",
4+
"version": "1.2.0-pre",
55
"type": "template",
66
"host": "hub",
77
"unity": "2020.3",
8-
"description": "Boss Room is a small scale cooperative game sample project built on top of the new Unity Networking Core library, designed to help you explore the concepts and patterns behind a multiplayer game flow. \n\nBoss Room features character abilities, casting animations to hide latency, replicated objects, RPCs, and integration with a relay service.",
8+
"description": "Utilities package built on top of Netcode for GameObjects, providing useful scripts and tools.",
99
"dependencies": {
1010
"com.unity.learn.iet-framework": "1.2.1",
11-
"com.unity.netcode.gameobjects": "1.0.0-pre.7",
11+
"com.unity.netcode.gameobjects": "1.0.0-pre.8",
1212
"com.unity.services.authentication": "1.0.0-pre.4",
1313
"com.unity.services.lobby": "1.0.0-pre.6",
1414
"com.unity.services.relay": "1.0.1-pre.5"

Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"source": "embedded",
122122
"dependencies": {
123123
"com.unity.learn.iet-framework": "1.2.1",
124-
"com.unity.netcode.gameobjects": "1.0.0-pre.7",
124+
"com.unity.netcode.gameobjects": "1.0.0-pre.8",
125125
"com.unity.services.authentication": "1.0.0-pre.4",
126126
"com.unity.services.lobby": "1.0.0-pre.6",
127127
"com.unity.services.relay": "1.0.1-pre.5"

0 commit comments

Comments
 (0)