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.
» Running the example
In order to run the example follow these steps:
- copy the SpaceWar folder from the /deploy/extensions folder to your SFS2X installation folder, under /SFS2X/extensions;
- copy the SpaceWar.zone.xml file (containing the Zone configuration) from the /deploy/zones folder to your SFS2X installation folder, under /SFS2X/zones;
- start SmartFoxServer 2X (v2.8 or later is required, v2.10 or later is highly recommended);
- make sure the client runs on the same machine as the server (the IP address can otherwise be changed on the Main script inspector of the Main Camera object);
- open the /deploy/client/SpaceWar.html file in a browser.
» Source code setup
The complete project is contained in a zipped folder. To access and build it please follow these steps:
- unzip the file contained in the /source folder;
- start Unity;
- in the Projects panel click on the Open other button and browse to the top folder of the unzipped package, then click Open;
- 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 the Game scene to open it.
All relevant 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 contained in the Plugins subfolder. Read the introduction to understand why multiple DLLs are used.
The game features a server-side Extension, as mentioned in the overview. In order to access its code, create and setup a new project in your Java IDE of choice as described in the Writing a basic Extension document. Copy the content of the /source/server/src folder to your project's source folder.
» 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: