SFS2X Docs / GettingStarted / client-api-csharp
» Client API setup | C#
The C# API is used to develop games and applications:
- for all browsers through the Unity Web Player plugin or WebGL build option;
- standalone games for PC, Mac, Linux, iOS, Android, Windows Store through the Unity publishing features;
- native games for .Net, Mono and Windows Universal 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 Windows Universal.
After creating a new project in Visual Studio (PC), MonoDevelop (Mac) or Xamarin (PC/Mac), edit the project's References and add the SmartFox2X.dll library contained in the /Release/DotNet folder of the API zip file.
These instructions refer to API v1.6.0 or subsequent, which provides Windows Universal compatibility (so Windows 8.1 or later). For developers requiring Windows 8.0 / Phone 8.0 compatibility, please use a previous version of the API and refer to this document for setup instructions.
After creating a new project in Visual Studio, edit the project's References and add the SmartFox2X.dll library contained in the /Release/Windows_Universal folder of the API zip file.
» Editor and Standalone, Web Player, iOS, Android 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, WSAPlayer (Windows Store) and WP8Player (Windows Phone 8). Finally click on the Apply button.
» Windows Store build
Starting form v1.6.0, C# is compatible with Unity's Windows Store build — SDKs 8.1, Phone 8.1 and Universal 8.1. For developers requiring compatibility with older SDK 8.0 or Windows Phone 8 build, please use a previous version of the API and refer to this document for setup instructions.
In addition to the steps described before, create a new folder under /Assets/Plugins — for example /Assets/Plugins/WindowsUniversal — and copy to it the SmartFox2X.dll library contained in the /Release/Windows_Universal folder of the API zip file.
Now click on the newly added SmartFox2X DLL and go to the Inspector panel; select WSAPlayer, deselect all the other platforms (including Editor) and click on the Apply button.
Unity will use the first DLL when working in the Editor and the second DLL when building the final game for Windows Store. For more informations please read the Unity documentation available here. In particular refer to the Windows Store Apps: Plugins page, here.
» 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.dll and SFSWebSockets.jslib files contained in the /Release/Unity_WebGL folder of the API zip file.
Now click on the newly added SmartFox2X DLL 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 8888 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 = 8888; #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.