SFS2X Docs / GettingStarted / client-api-csharp
» Client API setup | C#
The C# API is used to develop multiplayer games and applications:
- for PC, Mac, Linux, iOS, Android, Windows Store, through the Unity publishing features;
- for all modern browsers through the Unity's WebGL build option;
- for native .Net, Mono and Universal Windows platforms.
More platforms may be supported too (see the Unity publishing options), but we didn't test them directly. So thank you for your feedback in case you do!
The API is distributed in the form of four DLL libraries to be used alternatively or in conjunction with each other (read below) depending on your development platform:
The libraries are available in the release and debug versions. Using the release version is always recommended. The debug version allows API's internal debug messages to be printed to the output panel in Visual Studio when developing applications for Universal Windows Platform.
IMPORTANT FOR .Net/Mono and UWP
When developing native applications/games for .Net or Universal Windows Platform, always remember to set the API's property SmartFox.ThreadSafeMode (here) to false explicitly!
The default true value requires an event queue processing thread, like in Unity.
After creating a new project in Visual Studio (PC, Mac), edit the project's References and add the SmartFox2X.dll library contained in the /Release/DotNet folder of the API zip file.
After creating a new project in Visual Studio (PC), edit the project's References and add the SmartFox2X_UWP.dll library contained in the /Release/UWP folder of the API zip file.
» Editor and Standalone, iOS, Android, Windows Store builds
Create a new project in Unity and create a folder — for example /Assets/Plugins — where to copy the SmartFox2X.dll library contained in the /Release/Unity folder of the API zip file.
Now click on the SmartFox2X plugin and go to the Inspector panel; select Editor and the target platforms you need, excluding WebGL. Finally click on the Apply button.
» WebGL build
In addition to the Unity Editor setup described before, create a new folder under /Assets/Plugins — for example /Assets/Plugins/WebGL — and copy to it the SmartFox2X_WebGL.dll and SFSWebSockets.jslib files contained in the /Release/Unity_WebGL folder of the API zip file.
Now click on the newly added SmartFox2X plugin and go to the Inspector panel; select WebGL, deselect all the other platforms (including Editor) and click on the Apply button.
Unity will use the DLL in the /Assets/Plugins folder when working in the Editor and the other library files in the /Assets/Plugins/WebGL folder when exporting for browser execution.
» Multi-platform projects
If you need to build the same Unity project for multiple platforms at once, among which WebGL, you will need a bit of conditional compilation in your code.
The reason is that WebGL build connects to SmartFoxServer 2X via websocket, while all the other builds connect via regular socket connection. On the server side, the two connections use different ports (defaults are 9933 for regular socket connection and 8080 for websocket connection).
The following code snippet shows how to setup the basic connection configuration (using default ports), create the SmartFox object instance and connect:
// Set connection parameters ConfigData cfg = new ConfigData(); cfg.Host = “YourDomainOrIP"; cfg.Zone = "YourZone"; #if UNITY_WEBGL cfg.Port = 8080; #else cfg.Port = 9933; #endif // Initialize SFS2X #if UNITY_WEBGL sfs = new SmartFox(UseWebSocket.WS); #else sfs = new SmartFox(); #endif // Add SFS2X listeners ... // Connect to SFS2X sfs.Connect(cfg);
Other than using the right connection port, the conditional compilation allows calling the proper constructor for websocket connection. Check the API documentation for more information and the Unity tutorials for full examples.