Skip to content

Commit 41de94c

Browse files
committed
Documentation: Add guide on how to create client-based network-synced scripts
1 parent 5fa16c2 commit 41de94c

8 files changed

Lines changed: 98 additions & 1 deletion

File tree

content/docs/world/scripting.mdx

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,101 @@ public class TestButton : MonoBehaviour
5353

5454
- Click the button and the TMP text object should update to show "Clicked!".
5555

56-
![Window showing custom script running](/img/scripting/8.png)
56+
![Window showing custom script running](/img/scripting/8.png)
57+
58+
## How to Add Network Syncing Scripts Via Unity Client Build
59+
60+
- Create a canvas object with a button inside it.
61+
62+
![Inspector Window showing canvas](/img/scripting/9.png)
63+
64+
- Add a script to the button object. As a simple example, we'll create a script called SCNE (short for Send Custom Network Events) which updates a TMP text object with the count of how many times a button gets clicked.
65+
66+
- Create and assign a TMP text object to the Button Text field in the inspector.
67+
68+
- Select the button and add an onClick() event to it. Assign your Button Gameobject to it, making sure to select SCNE.IncrementCounter as the method which your button will call on when clicked.
69+
70+
``` cs title="SCNE.cs"
71+
using Basis;
72+
using Basis.Network.Core;
73+
using Basis.Scripts.Networking.NetworkedAvatar;
74+
using System;
75+
using TMPro;
76+
using UnityEngine;
77+
78+
public class SCNE : BasisNetworkBehaviour
79+
{
80+
private int counter = 0;
81+
82+
[SerializeField] TextMeshPro counterText;
83+
84+
public void IncrementCounter()
85+
{
86+
TakeOwnership(); // Ensure we have ownership before sending the update
87+
counter++;
88+
SendCustomNetworkEvent(BitConverter.GetBytes(counter), DeliveryMethod.ReliableOrdered);
89+
OnDeserialization();
90+
}
91+
92+
public override void OnNetworkMessage(ushort playerId, byte[] buffer, DeliveryMethod deliveryMethod) // Called when a network message is received
93+
{
94+
if (buffer != null && buffer.Length >= 4)
95+
{
96+
counter = BitConverter.ToInt32(buffer, 0);
97+
OnDeserialization();
98+
}
99+
}
100+
101+
public void OnDeserialization() // Called after any change (local or remote)
102+
{
103+
Debug.Log($"Counter value is now: {counter}");
104+
counterText.text = $"Counter: {counter}";
105+
}
106+
107+
public override void OnPlayerJoined(BasisNetworkPlayer player) // Ensure late joining players receive the correct count
108+
{
109+
if (IsLocalOwner())
110+
{
111+
SendCustomNetworkEvent(BitConverter.GetBytes(counter), DeliveryMethod.ReliableOrdered, new ushort[] { player.playerId }); // Send the current counter value to the new player
112+
}
113+
}
114+
}
115+
```
116+
117+
![Inspector Window showing button with SCNE script](/img/scripting/10.png)
118+
119+
<Callout type="info">
120+
The overridden **OnPlayerJoined()** is used as, without it, newly joining players would otherwise not see the updated count number.
121+
122+
**TakeOwnership()** is used to prevent desynchronisation such as the counter value resetting due to a newly joining player clicking on the button.
123+
</Callout>
124+
125+
- Add a Basis Prop component to the canvas Gameobject, enter an Asset Bundle Name and press the 'Create Prop Bee File' button. Use the Loadable Config Editor to generate an XML file.
126+
127+
![Inspector Window showing button using Basis Prop](/img/scripting/11.png)
128+
129+
![Inspector Window showing BasisFramework prefab](/img/scripting/13.png)
130+
131+
- Add a Basis Scene component to the an empty Gameobject, enter an Asset Bundle Name and press the 'Create Scene Bee File' button. Store it in a remote server.
132+
133+
<Callout type="info">
134+
You should disable or remove the canvas object from the scene. This is because keeping it active will result in two copies of it existing in the scene later: one copy from the scene itself and another generated by the XML.
135+
</Callout>
136+
137+
![Inspector Window showing Basis Scene component](/img/scripting/12.png)
138+
139+
- Open the BasisFramework prefab. In the Bundled Content Folder component's, add the generated .bee password and url.
140+
141+
![Inspector Window showing BasisFramework prefab](/img/scripting/14.png)
142+
143+
- Run the Initialization scene.
144+
145+
![Window showing Initialization scene running](/img/scripting/6.png)
146+
147+
- Build the Unity client. Your custom script will script will be included in the build.
148+
149+
![Window showing Unity Client Build Profiles](/img/scripting/7.png)
150+
151+
- Click the button and the TMP text object should update the count after each button click for both local and remote players.
152+
153+
![Inspector Window showing scene running in client](/img/scripting/15.png)

public/img/scripting/10.png

58.6 KB
Loading

public/img/scripting/11.png

18.6 KB
Loading

public/img/scripting/12.png

94 KB
Loading

public/img/scripting/13.png

35.5 KB
Loading

public/img/scripting/14.png

66.3 KB
Loading

public/img/scripting/15.png

185 KB
Loading

public/img/scripting/9.png

29.5 KB
Loading

0 commit comments

Comments
 (0)