Skip to content
This repository was archived by the owner on Jul 23, 2025. It is now read-only.

Commit 551a283

Browse files
authored
More issue fixes (#1253)
1 parent 82af0e3 commit 551a283

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

docs/advanced-topics/message-system/rpc.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ title: Rpc
55
import ImageSwitcher from '@site/src/ImageSwitcher.js';
66

77

8-
Any process can communicate with any other process by sending an RPC. Starting in version 1.8, the `Rpc` attribute encompasses Server to Client Rpcs, Client to Server Rpcs, and Client to Client Rpcs.
8+
Any process can communicate with any other process by sending an RPC. Starting in version 1.8, the `Rpc` attribute encompasses server to client RPCs, client to server RPCs, and client to client RPCs.
99

1010
<figure>
1111
<ImageSwitcher
1212
lightImageSrc="/sequence_diagrams/RPCs/ServerRPCs.png?text=LightMode"
1313
darkImageSrc="/sequence_diagrams/RPCs/ServerRPCs_Dark.png?text=DarkMode"/>
1414
</figure>
1515

16-
17-
1816
<figure>
1917
<ImageSwitcher
2018
lightImageSrc="/sequence_diagrams/RPCs/ClientRPCs.png?text=LightMode"
2119
darkImageSrc="/sequence_diagrams/RPCs/ClientRPCs_Dark.png?text=DarkMode"/>
2220
</figure>
2321

24-
25-
26-
2722
## Declaring an RPC (Remote Procedure Call)
2823

2924
You can declare an RPC by marking a method with the `[Rpc]` attribute and including the `Rpc` suffix in the method name. RPCs have a number of possible targets that can be declared at both runtime and compile time, but a default must be passed to the `[Rpc]` attribute. For example, to create an RPC that will be executed on the server, you would declare it like this:
@@ -58,7 +53,7 @@ While client-to-client RPCs are supported, it is important to note that there ar
5853

5954
## Invoking an RPC
6055

61-
You can invoke an RPC by invoking the function directly with parameters:
56+
You can invoke an RPC by invoking the function directly with parameters. The following code is a heavily simplified example of a server to client RPC and a client to server RPC, with additional logic removed to display only the most basic features of an RPC.
6257

6358
```csharp
6459
[Rpc(SendTo.Server)]

docs/advanced-topics/serialization/cprimitives.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ id: cprimitives
33
title: C# primitives
44
---
55

6-
C# primitive types will be serialized by built-in serialization code. These types include `bool`, `char`, `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `float`, `double`, and `string`.
6+
C# primitive types are serialized by built-in serialization code. These types include `bool`, `char`, `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `float`, `double`, and `string`.
7+
8+
You can also serialize [arrays](serialization-arrays.md) of C# primitive types, with the exception of arrays of `strings` (`string[]`) for [performance reasons](serialization-arrays.md#performance-considerations).
79

810
```csharp
911
[Rpc(SendTo.Server)]

docs/advanced-topics/serialization/serialization-arrays.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ id: arrays
33
title: Arrays and native containers
44
---
55

6-
Arrays of [C# primitive types](cprimatives.md), like `int[]`, and [Unity primitive types](unity-primitives.md), such as `Vector3`, are serialized by built-in serialization code. Otherwise, any array of types that aren't handled by the built-in serialization code, such as `string[]`, needs to be handled through a container class or structure that implements the [`INetworkSerializable`](inetworkserializable.md) interface.
6+
Netcode for GameObjects has built-in serialization code for arrays of [C# value-type primitives](cprimitives.md), like `int[]`, and [Unity primitive types](unity-primitives.md). Any arrays of types that aren't handled by the built-in serialization code, such as `string[]`, need to be handled using a container class or structure that implements the [`INetworkSerializable`](inetworkserializable.md) interface.
7+
8+
## Performance considerations
9+
10+
Sending arrays and strings over the network has performance implications. An array incurs a garbage collected allocation, and a string also incurs a garbage collected allocation, so an array of strings results in an allocation for every element in the array, plus one more for the array itself.
11+
12+
For this reason, arrays of strings (`string[]`) aren't supported by the built-in serialization code. Instead, it's recommended to use `NativeArray<FixedString*>` or `NativeList<FixedString*>`, because they're more efficient and don't incur garbage collected memory allocation. Refer to [`NativeArray<T>`](#nativearrayt) and [`NativeList<T>`](#nativelistt) below for more details.
13+
14+
## Built-in primitive types example
15+
16+
Using built-in primitive types is fairly straightforward:
717

8-
## Built-In Primitive Types Example
9-
Using built-in primitive types is fairly straight forward:
1018
```csharp
1119
[Rpc(SendTo.Server)]
1220
void HelloServerRpc(int[] scores, Color[] colors) { /* ... */ }
1321
```
1422

15-
## INetworkSerializable Implementation Example
16-
There are many ways to handle sending an array of managed types.
17-
The below example is a simple `string` container class that implements `INetworkSerializable` and can be used as an array of "StringContainers":
23+
## INetworkSerializable implementation example
24+
25+
There are many ways to handle sending an array of managed types. The following example is a simple `string` container class that implements `INetworkSerializable` and can be used as an array of "StringContainers":
26+
1827
```csharp
1928
[Rpc(SendTo.ClientsAndHost)]
2029
void SendMessagesClientRpc(StringContainer[] messages)
@@ -42,11 +51,12 @@ public class StringContainer : INetworkSerializable
4251
}
4352
```
4453

45-
## Native Containers
54+
## Native containers
4655

47-
Netcode for GameObjects supports `NativeArray` and `NativeList` native containers with built-in serialization, RPCs, and NetworkVariables. However, you cannot nest either of these containers without causing a crash.
56+
Netcode for GameObjects supports `NativeArray` and `NativeList` native containers with built-in serialization, RPCs, and NetworkVariables. However, you can't nest either of these containers without causing a crash.
4857

4958
A few examples of nesting that will cause a crash:
59+
5060
* `NativeArray<NativeList<T>>`
5161
* `NativeList<NativeArray<T>>`
5262
* `NativeArray<NativeArray<T>>`
@@ -59,6 +69,7 @@ To serialize a `NativeArray` container, use `serializer.SerializeValue(ref Array
5969
### `NativeList<T>`
6070

6171
To serialize a `NativeList` container, you must:
72+
6273
1. Ensure your assemblies reference `Collections`.
6374
2. You must add `UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT` to your **Scriptiong Define Symbols** list.
6475
1. From the Unity Editor top bar menu, go to **Edit** > **Project Settings...** > **Player**.
@@ -71,8 +82,7 @@ To serialize a `NativeList` container, you must:
7182
When using `NativeLists` within `INetworkSerializable`, the list `ref` value must be a valid, initialized `NativeList`.
7283

7384
NetworkVariables are similar that the value must be initialized before it can receive updates.
74-
For example,
75-
`public NetworkVariable<NativeList<byte>> ByteListVar = new NetworkVariable<NativeList<byte>>{Value = new NativeList<byte>(Allocator.Persistent)};`
85+
For example, `public NetworkVariable<NativeList<byte>> ByteListVar = new NetworkVariable<NativeList<byte>>{Value = new NativeList<byte>(Allocator.Persistent)};`.
7686

7787
RPCs do this automatically.
7888
:::

0 commit comments

Comments
 (0)