Forge Networking: Step by Step Example
New Getting Started tutorial!
Importing the Package
To import our package into unity just do one of the following:
- Drag it from your file system into the unity window and drop it onto the "Assets" folder
- With Unity open, double click on the package and then click on import when prompted in Unity
- Locate your purchase in the Unity Asset Store and click the "Import" link. If you have not yet downloaded it, you will need to click the "Download" link before the "Import" link becomes available
Create a Scene
To create a new scene, just go to File->New Scene or use the appropriate hot-keys. Once this is complete you should save your scene to the file system. Click File->Save Scene and when prompted, save it to your Unity project named "Game".
Making a Networked Cube
In your new scene click on GameObject->3D Object->Cube. You will see a new cube in your scene Select the cube and then set its position to (0, 0, 0) so that it is visible by the camera. Now click on the "Add Component" button in the "Inspector" of Unity while the cube is still selected. Type in "NetworkedMonoBehavior" and you will see a C# script that is named that. Select that script to add it to the object.
Making the Cube Move Across the Network
To see that the cube is being networked, we will add a script to it that makes it move.
Right click on the "Assets" folder and then go to Create->C# Script.
Name this script "DemoMove" and then open the file to be edited.
Make the script look like the following and then save it:
Preparing Your Build
Now that you have altered the scene, make sure to save it with File->Save Scene.
Next, you need to open up the Build Settings by clicking File->Build Settings…
First, we are going to make sure that we have "Run In Background" turned on for our build so that the network communications continue while the game is not focused on our desktop. Select the "Player Settings…" button located in the lower left corner of the build settings window. Open the settings section labeled "Resolution and Presentation" in the inspector window. Now just click the checkbox labeled "Run In Background*" so that it is turned on.
Next, while the Build Settings window is still open, navigate to the Bearded Man Studios, Inc.->Networking->Scenes folder and drag the "ForgeQuickStart" scene into the build settings area labeled "Scenes in Build". Next, navigate to your "Game" scene and drag it into the same area but below the "ForgeQuickStart" scene.
Under the "Platform" area of the Build Settings, click on "PC, Mac & Linux Standalone" then click the "Switch Platform" button on the lower left.
Running the Build
Now click "Build and Run" and then save the game onto your hard drive. Once the game opens (accept any firewall permissions you are prompted with), click on "Start Server", then go to the Unity Editor and open the "ForgeQuickStart" by double clicking it. Press play in the editor and then click "Start Client". Now you can click on your running PC build and press any of the arrow keys.
You now have glorious networking!