Getting Started
Walk from a fresh Unity project to a running NetCrafter schema that generates working multiplayer network code.
Install the asset
Import the NetCrafter .unitypackage into your project via Assets → Import Package → Custom Package. After import the following structure will appear under Assets/PivecLabs/NetCrafter/:
Documentation/ — these docs Editor/ — editor window and code-generation tools Examples/ — example schemas Plugins/x86_64/ — NetCrafterNative.dll (Windows only) Scripts/ — ScriptableObject definitions Templates/ — built-in code templates
Open the NetCrafter editor window
Unity menu → Tools → Pivec Labs → NetCrafter Pro
The window opens with four top-level sections in the toolbar: Definition, Code, Scenes, and Settings. Resize it to at least 1100 × 650 if controls appear clipped.
Create a schema (Definition)
A Definition is a named collection of messages, events, models, and server actions. Each definition generates its own isolated set of C# files.
- In the schema bar at the very top of the window, click Create Definition.
- Name it (e.g.
MyGame). A folder is created atAssets/PivecLabs/NetCrafter/Definitions/MyGame/.
You can switch between definitions using the dropdown in the schema bar.
Configure SmartFox settings
Click Settings in the top toolbar, then SmartFox Settings in the sidebar.
| Field | Value to set |
|---|---|
| Host | Your server IP or localhost for local testing |
| TCP Port | 9977 (SFS3 default) |
| Zone Name | The zone name defined in your SFS3 zones.xml |
Leave all other fields at their defaults for now. Click Save Settings.
Define a model (Data Definition)
Models are plain data classes that can be nested inside messages and events. Create a PlayerState model:
- Click Definition in the top toolbar, then Data Definitions in the sidebar.
- Click Create Data Definition. Name it
PlayerState. - Add fields:
| Name | Type |
|---|---|
position | Vector3 |
health | int |
username | string |
isAlive | bool |
Define a message (ClientToServer)
Messages sent from the client to the server generate a {Name}Request class with a Send() method.
- Click Definition → Messages in the sidebar.
- Click Create Gameplay Message. Name it
PlayerMove. - Set Direction to
ClientToServer. - Set Server Handler Name to
HandlePlayerMove(this must match a Server Action you add next). - Add fields:
| Name | Type |
|---|---|
playerId | PlayerId |
position | Vector3 |
speed | float |
Add a matching Server Action
- Click Server Actions in the sidebar.
- Click Create Server Action. Set Handler Name to
HandlePlayerMove.
Define an event (ServerToClient)
Events sent from the server to the client generate a {Name}Event class with a static FromSFSObject() factory.
- Click Messages in the sidebar. Click Create Gameplay Message.
- Name it
PlayerStateUpdate. Set Direction toServerToClient. - Set Server Handler Name to
HandlePlayerStateUpdate. - Add a field: name
state, typestring.
To use a nested model type such as PlayerState as a field type, open the generated .asset file in a text editor and set type: PlayerState directly. The editor field-type dropdown currently shows only primitive types.
Add a matching Server Action with handler name HandlePlayerStateUpdate.
Run Generate All Files
- Click Code in the top toolbar, then Build Code in the sidebar.
- Click Validate Definition first to confirm there are no errors.
- Click Generate All Files.
Generated files appear in:
Assets/PivecLabs/NetCrafter/Definitions/MyGame/Generated/Client/
What was generated
For the schema above, the generator produces:
| File | Description |
|---|---|
PlayerMoveRequest.cs | Send a PlayerMove message to the server |
PlayerStateUpdateEvent.cs | Receive a PlayerStateUpdate event from the server |
PlayerState.cs | Serialisable data model |
SmartFoxConnectionManager.cs | MonoBehaviour that manages the SFS3 connection |
Set up your scene
The Scene Assistant automates the boilerplate of wiring a multiplayer scene.
Create the ConnectionManager prefab
- Click Scenes in the top toolbar, then Prefab Generator in the sidebar.
- Click Create ConnectionManager Prefab.
The generator finds the SmartFoxConnectionManager.cs in your Generated/Client/ folder, wraps it in a prefab, and saves it to Generated/Prefabs/ConnectionManager.prefab. If the script is not found, generate client code first (step 8).
Create the scene
- Click Scene Assistant in the sidebar.
- Confirm the green banner shows that the prefab was found.
- Optionally enable Add Event Dispatcher — this adds a
NetCrafterEventDispatcherGameObject that lets any script subscribe to server events without touching the generated file (see step 13). - Click Create Multiplayer Scene.
The assistant creates a new scene containing:
- A Main Camera
- A ConnectionManager GameObject (instantiated from the prefab)
- An EventDispatcher GameObject (if the toggle was enabled)
- A NetworkManager empty GameObject (reserved for future use)
The scene is saved to Definitions/{Name}/GameScene.unity and opened in the editor. You can rename it using the Scene Name field before clicking Create.
Add to an existing scene
If you already have a scene open, click Add to Existing Scene instead. This adds only the ConnectionManager (and EventDispatcher) — it does not add a camera or NetworkManager. If a ConnectionManager is already present, a confirmation dialog appears before a second one is added.
Add the ConnectionManager to a scene manually
If you prefer not to use the Scene Assistant:
- Create an empty GameObject in your scene. Name it
ConnectionManager. - Attach the generated
SmartFoxConnectionManagercomponent to it. - Verify the Host, Port, and Zone Inspector fields match your server.
The ConnectionManager is a singleton with DontDestroyOnLoad. It calls sfs.ProcessEvents() each frame and sends a guest login on successful connection. Call Connect() from a script when the game starts:
void Start()
{
SmartFoxConnectionManager.Instance.Connect();
}
Send a request
Populate the generated request class and call Send():
using PivecLabs.MyGame.Generated;
using Sfs3.Client;
public class PlayerController : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var request = new PlayerMoveRequest
{
playerId = SmartFoxConnectionManager.Instance.Sfs.MySelf.Id,
position = transform.position,
speed = 5.0f
};
request.Send(SmartFoxConnectionManager.Instance.Sfs);
}
}
}
Handle an event
Option A — Fill in the generated stub. Open SmartFoxConnectionManager.cs and complete the stub method:
private void OnPlayerStateUpdate(PlayerStateUpdateEvent ev)
{
// ev.state is a deserialized PlayerState object
Debug.Log($"Player at {ev.state.position}, health {ev.state.health}");
}
Option B — Subscribe via EventDispatcher. From any MonoBehaviour in the scene, subscribe to the event by command name. This approach does not require editing the generated file:
private void OnEnable()
{
NetCrafterEventDispatcher.Instance.Subscribe("PlayerStateUpdate", OnPlayerState);
}
private void OnDisable()
{
NetCrafterEventDispatcher.Instance.Unsubscribe("PlayerStateUpdate", OnPlayerState);
}
private void OnPlayerState(object payload)
{
var ev = (PlayerStateUpdateEvent)payload;
Debug.Log($"Player at {ev.state.position}, health {ev.state.health}");
}
Both options can be used simultaneously — the dispatcher fires first, then the stub. See the API Reference for full EventDispatcher details.
Next steps
- See the Manual for a complete reference of every tab and setting.
- See the API Reference for the full generated class API.