You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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
+
usingBasis;
72
+
usingBasis.Network.Core;
73
+
usingBasis.Scripts.Networking.NetworkedAvatar;
74
+
usingSystem;
75
+
usingTMPro;
76
+
usingUnityEngine;
77
+
78
+
publicclassSCNE : BasisNetworkBehaviour
79
+
{
80
+
privateintcounter=0;
81
+
82
+
[SerializeField] TextMeshProcounterText;
83
+
84
+
publicvoidIncrementCounter()
85
+
{
86
+
TakeOwnership(); // Ensure we have ownership before sending the update
publicoverridevoidOnNetworkMessage(ushortplayerId, byte[] buffer, DeliveryMethoddeliveryMethod) // 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
+
publicvoidOnDeserialization() // Called after any change (local or remote)
102
+
{
103
+
Debug.Log($"Counter value is now: {counter}");
104
+
counterText.text=$"Counter: {counter}";
105
+
}
106
+
107
+
publicoverridevoidOnPlayerJoined(BasisNetworkPlayerplayer) // Ensure late joining players receive the correct count
108
+
{
109
+
if (IsLocalOwner())
110
+
{
111
+
SendCustomNetworkEvent(BitConverter.GetBytes(counter), DeliveryMethod.ReliableOrdered, newushort[] { player.playerId }); // Send the current counter value to the new player
112
+
}
113
+
}
114
+
}
115
+
```
116
+
117
+

118
+
119
+
<Callouttype="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
+

- 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
+
<Callouttype="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
+

138
+
139
+
- Open the BasisFramework prefab. In the Bundled Content Folder component's, add the generated .bee password and url.
0 commit comments