SFS2X Docs / ExamplesUnity / introduction
» Unity/C# examples
In this section of the documentation we provide a series of brief tutorials on the C# examples for the Unity platform distributed as a specific package on the SmartFoxServer 2X download page. Specifically the tutorials refer to the Unity 2017.1 examples, but the concepts and the code to interact with the SFS2X API are valid for any version of Unity (unless otherwise noted).
Most tutorials analyze a single example, describing its objectives, giving an insight into the SmartFoxServer features it wants to highlight and providing the direct link to download the source code, which includes all the assets required to compile and test it (both client and — if existing — server side). If necessary, code excerpts are provided in the tutorial itself, in order to better explain the approach that was followed to implement a specific feature. At the bottom of the tutorial, additional resources are linked if available.
The tutorials follow an increasingly complexity curve from basic server connections over chat messaging to full realtime games with authoritative server extension.
Specifically the examples will show:
- basic connection
- chat messaging and room management
- buddy list management
- game rooms and extension messaging
- simple client side 3d object movement
- server authoritative first person shooter
- demo of the MMO-related features
- clients synchronization in a space shooter game
- protocol encryption
» Usability on different Unity deployment targets
All examples run seamless and are interoperable on the following build targets:
- PC, Mac and Linux standalone
- iOS
- Android
- WebGL-compatible browsers (except the First Person Shooter example)
- Universal Windows Platform
Other platforms might be compatible with the examples, but haven't been tested directly.
The user interfaces have been developed primarily for desktop usage, so they might not fit 100% into mobile screen size without a little extra work. Also they are based on the UI system available in Unity since version 4.6.
Please note that due to the different requirements of the Unity build targets, the SmartFoxServer C# API contained in the examples is made of three separate DLLs: a DLL specific for WebGL builds, a DLL for Universal Windows Platform builds and a DLL for all other Unity target platforms (including the Unity Editor itself).
Most examples use all three DLLs in separate subfolders under the Plugins folder. Each DLL is assigned to its target by clicking on it and selecting one or more platforms in the Inspector panel as follows:
- /Plugins/SmartFox2X.dll → all platforms excluding WebGL and WSAPlayer
- /Plugins/WebGL/SmartFox2X_WebGL.dll → WebGL only
- /Plugins/UWP/SmartFox2X_UWP.dll → WSAPlayer only

NOTE
Starting from Unity 2018.2, using a separate DLL for Universal Windows Platform builds is not needed anymore. You can assign the generic SmartFox2X.dll to that platform too.
Check this link for more information on the API setup.
» Usability on non-Unity platforms
The client API is fully usable on other .NET enabled platforms like ASP.NET or console applications and is thus not limited to Unity usage. The examples are not directly compilable on these platforms, but all relevant C# code is placed in the same Scripts folder and it's 99% usable via copy/paste in a different context.
NOTE
When using the API on these targets you will have to either run the API in non-thread safe mode or process the callback events in a timer.
Check this link for more information on non-thread safe mode.
» Requirements
Unless otherwise noted inside the tutorials:
- all examples are distributed in a single package and the same download link is provided in all the tutorials;
- client side of the examples is provided as a Unity project: just open its folder with Unity (when opening for the first time, it will take a few seconds to Unity to regenerate the required libraries for the project);
- the main folder of each example in the downloaded package is referenced in the related tutorial using this icon:
;
- all examples require Unity 2017.1 or later, but the relevant multiplayer code is compatible with all versions of Unity starting form Unity 4 (NOTE: depending on the Unity version in which examples are imported, the DLL assignment to different platforms may get lost: if multiple errors CS0433 are displayed in the Unity Editor console, please follow the instructions above to re-assign the DLLs);
- some of the examples are based on a stripped down version of the Shader Calibration Scene provided by the Unity team on the Asset Store (all rights reserved);
- all examples are developed with C#; UnityScript can be used as well with the API, but this is not documented or supported directly;
- the server side Extensions of those examples which require one have been developed using Eclipse, but you should be able to use any Java IDE of your choice, provided you use the JDK 1.6 or later.
NOTE
All the examples make use of the latest version of the SmartFoxServer 2X C# API available at the time of their development. As the API evolve in time (bug fixes, new features), we strongly recommend that, after downloading an example, you overwrite the included API DLL files with those provided in the latest SmartFoxServer C# API package available on our website.