Skip to content

feat: client network transform move to samples [MTT-3406] #629

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 8 commits into from
Apr 27, 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure
{
public static class MessageChannelDIExtensions
Expand All @@ -6,7 +8,7 @@ public static void BindMessageChannelInstance<TMessage>(this DIScope scope)
{
scope.BindInstanceAsSingle<MessageChannel<TMessage>, IPublisher<TMessage>, ISubscriber<TMessage>, IMessageChannel<TMessage>>(new MessageChannel<TMessage>());
}
public static void BindNetworkedMessageChannelInstance<TMessage>(this DIScope scope) where TMessage : unmanaged
public static void BindNetworkedMessageChannelInstance<TMessage>(this DIScope scope) where TMessage : unmanaged, IComparable, IConvertible, IComparable<TMessage>, IEquatable<TMessage>
{
scope.BindInstanceAsSingle<NetworkedMessageChannel<TMessage>, IPublisher<TMessage>, ISubscriber<TMessage>, IMessageChannel<TMessage>>(new NetworkedMessageChannel<TMessage>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure
/// subscribing will be required each time a new session starts.
/// </summary>
/// <typeparam name="T"></typeparam>
public class NetworkedMessageChannel<T> : MessageChannel<T> where T : unmanaged
public class NetworkedMessageChannel<T> : MessageChannel<T> where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>
{
string m_Name;

Expand Down
3 changes: 2 additions & 1 deletion Assets/BossRoom/Scripts/Shared/NetworkNameState.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
Expand All @@ -18,7 +19,7 @@ public class NetworkNameState : NetworkBehaviour
/// </summary>
public struct FixedPlayerName : INetworkSerializable
{
private FixedString32Bytes m_Name;
ForceNetworkSerializeByMemcpy<FixedString32Bytes> m_Name; // using ForceNetworkSerializeByMemcpy to force compatibility between FixedString and NetworkSerializable
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref m_Name);
Expand Down

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Unity.Netcode.Components;
using Unity.Netcode;
using UnityEngine;

namespace Unity.Multiplayer.Samples.Utilities.ClientAuthority
{
// TODO inherit from `NetworkBehaviour` instead of `NetworkTransform` to cut direct relationship between two
// TODO change to owner netvar instead of RPC based
/// <summary>
/// Used for syncing a transform with client side changes. This includes host. Pure server as owner isn't supported by this. Please use NetworkTransform
/// for transforms that'll always be owned by the server.
/// </summary>
[DisallowMultipleComponent]
public class ClientNetworkTransform : NetworkTransform
{
/// <summary>
/// Used to determine who can write to this transform. Owner client only.
/// Changing this value alone will not allow you to create a NetworkTransform which can be written to by clients.
/// We're using RPCs to send updated values from client to server. Netcode doesn't support client side network variable writing.
/// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
/// </summary>
// 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

public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
CanCommitToTransform = IsOwner;
}

protected override void Update()
{
CanCommitToTransform = IsOwner;
base.Update();
if (NetworkManager.Singleton != null && (NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsListening))
{
if (CanCommitToTransform)
{
TryCommitTransformToServer(transform, NetworkManager.LocalTime.Time);
}
}
}

protected override bool OnIsServerAuthoritatitive()
{
return false;
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Unity.Multiplayer.Samples.Utilities.ClientAuthority",
"rootNamespace": "",
"references": [
"Unity.Netcode.Components",
"Unity.Netcode.Runtime"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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

8 changes: 4 additions & 4 deletions Packages/com.unity.multiplayer.samples.coop/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "com.unity.multiplayer.samples.coop",
"displayName": "Boss Room",
"version": "1.1.0-pre",
"displayName": "Multiplayer Samples Utilities",
"version": "1.2.0-pre",
"type": "template",
"host": "hub",
"unity": "2020.3",
"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.",
"description": "Utilities package built on top of Netcode for GameObjects, providing useful scripts and tools.",
"dependencies": {
"com.unity.learn.iet-framework": "1.2.1",
"com.unity.netcode.gameobjects": "1.0.0-pre.7",
"com.unity.netcode.gameobjects": "1.0.0-pre.8",
"com.unity.services.authentication": "1.0.0-pre.4",
"com.unity.services.lobby": "1.0.0-pre.6",
"com.unity.services.relay": "1.0.1-pre.5"
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"source": "embedded",
"dependencies": {
"com.unity.learn.iet-framework": "1.2.1",
"com.unity.netcode.gameobjects": "1.0.0-pre.7",
"com.unity.netcode.gameobjects": "1.0.0-pre.8",
"com.unity.services.authentication": "1.0.0-pre.4",
"com.unity.services.lobby": "1.0.0-pre.6",
"com.unity.services.relay": "1.0.1-pre.5"
Expand Down