SFS2X Docs / ExamplesUnity / spacewar
The SpaceWar example is a tribute to to the homonymous game developed in the 60s, one of the earliest computer games in history! The purpose of this example is to showcase the capabilities of SmartFoxServer 2X MMO Rooms in a realtime game featuring flying starships, weapon shots, collisions... with the possibility of having thousands of them in the same Room.
This example is a porting of its Flash counterpart. The two versions of the game share the same server-side Extension, which contains the core logic of the game, and a similar approach in client development. Also, the game flow and basic concepts like clients synchronization and Room configuration (but not only) are the same for the two versions. For this reason we strongly recommend that you read the in-depth Flash tutorial to learn the multiplayer programming techniques used in the Unity game too.
This example makes use of the new 2D Tools introduced with Unity 4.3, in particular the new sprite renderers and animators. Before Unity 4.3, making 2D games was still possible, but the process was quite long, which led to many plugins helping in the workflow appear on the Asset Store.
With the new 2D tools, porting the original Flash example (ActionScript 3.0 language) to Unity engine was a much simpler task. Most of the code was reused, differing mostly on Unity’s renderer specific tasks and on the menu controller.
The next paragraphs will draw your attention on a few steps of the 2D development process specific to Unity. Again, you should read the Flash tutorial for an extended explanation of the core game logic.
Also check the comments to methods and properties in the source code for additional informations and possible code optimizations.
» Setup & run
In order to setup and run the example, follow these steps:
- copy the SpaceWar.zone.xml file (containing the Zone configuration) from the /server/Zone folder to your SFS2X installation folder, under /SFS2X/zones;
- copy the /server/Extension_Java/deploy/SpaceWar folder to the server's /SFS2X/extensions folder;
- start SmartFoxServer 2X (v2.13 or later is highly recommended);
- start Unity (v5.6 or later required);
- in the Projects panel click on the Open icon and browse to the /client/SpaceWar folder, then click the Open button;
- wait for the project setup completion (Unity needs to regenerate some libraries);
- go to the Project panel, click on the Assets/Scenes folder and double click on one of the Gamescene to open it;
- if SmartFoxServer and Unity are not running on the same machine, change the IP address of the server in the inspector of the Main Camera game object in the scene;
- click on the Play button to run the example in the Unity Editor, or go to the Build settings panel and build it for your target platform of choice.
All relevant client assets are contained in the Assets folder; in particular the C# code is in the Scripts subfolder and the SmartFoxServer 2X client API DLLs are in the Plugins subfolder. Read the introduction to understand why multiple DLLs are used.
Step 2 above shows how to deploy the Java Extension, which is the default one for the example. In order to access its code, create and setup a new project in your Java IDE of choice as described in the Writing the first Java Extension document of the Java Extensions Development section. Copy the content of the /server/Extension_Java/source/src folder to your Java project's source folder.
- locate the "Sol" Room definition and change the Room Extension settings to:
- locate the Zone Extension settings and change them to:
» Client highlights
» SpriteSheet creation
Before creating the sprites for the game elements, first we have to create the assets' SpriteSheet.
In the Images folder you'll find a SpriteSheet with all the animated elements plus separated SpriteSheets for each static element. All of these assets were obtained from the original Flash example's SpriteSheet.
To create a SpriteSheet in Unity, you have to select your SpriteSheet image and, on the Inspector, set its Texture Type to Sprite and configure its Sprite Mode according to the SpriteSheet you're adding: Single for a single framed SpriteSheet or Multiple, in case it's a SpriteSheet with multiple elements.
In a Multiple SpriteSheet, you also have to specify each element's frames. Thankfully Unity deals with most of the work, and you just need to open the Sprite Editor, open the Slice menu, at the top left, and set the Type to Automatic. You'll notice that, on the Project Panel, there's an arrow at the left of your SpriteSheet. If you open it, you'll have access to the different frames.
After generating the Multiple SpriteSheet, you'll want to specify the animations of each element. In order to achieve that, simply select the corresponding frames and drag them onto the Scene. Unity will then prompt you to create an AnimationClip and an AnimatorController for the selected frames, which you can later add to the prefab.
» The Animator Controller
In the Animations folder you'll find folders with the animation clips and controllers for the different animated game elements. These assets were generated by manually selecting the corresponding frames and adding them to the Scene.
When you double click one of the AnimatorController, you'll be presented with Unity's new Animator Panel. Here you'll be able to customize the animations' sequence and duration, alongside with the necessary triggers, which will be used in the code for triggering each animation.
» Controlling the menu
While the Flash example controls the menu through multiple screens, on the Unity Example makes use of Unity's UI API, which is all controlled by the Main class.
» The FPS counter
Contrary to the Starling engine used in the Flash example, Unity doesn't come with an embedded Frames-Per-Second counter. You can find multiple implementations online. We used the one from Unity's wiki: http://wiki.unity3d.com/index.php?title=FramesPerSecond#CSharp_HUDFPS.cs
» More resources
You can learn more about the MMO Room features by consulting the following resources: