查看: 2011|回复: 0
打印 上一主题 下一主题

SmartFoxServer 基础教程

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-6-18 16:19:10 |只看该作者 |倒序浏览
Multiplayer with Unity and SmartFox tutorial
Introduction
This tutorial will show you how to create a simple level in Unity with SmartFox multiplayer support. It will use a third person view (as opposed to the first person view in the SmartFox Island demo). I have changed a some of the Island Demo settings (different zone and room for example) so you know what to change where.

I am by no means an expert in Unity or SmartFox, so if things can be done better or more efficient, please add and edit!
Install SmartFoxServer


•First install SmartFoxServer PRO (make sure it is the PRO version)

◦You can use the pro version for free with up to 20 connections, more than enough for this tutorial.

◦If you're installing on Windows 7, then you need to have "Run as Administrator" ticked for java*** (directory Crogram Files (x86)SmartFoxServerPRO_1.6.6jrein)

◦At the time of writing (November 5, 2010), version 1.6.6 is the latest stable. Pretty soon SFS2X should come out of beta, check the SmartFox F***ms for details.

&bull;Configure a new Zone that we will use for this tutorial, by adding the following in the <Zones> section of the config.xml file (Server directory):
<Zone name="city" uCountUpdate="***e" buddyList="20" maxUsers="4000" customLogin="false">

   <Rooms>

      <Room name="Central Square" maxUsers="50" isPrivate="false" isTemp="false" autoJoin="***e" uCountUpdate="***e" />           

      <Room name="The Station" maxUsers="50" isPrivate="false" isGame="false" isTemp="false" />

      <Room name="The Auction House" maxUsers="50" isPrivate="false" isTemp="false" />

      <Room name="The Safe House" maxUsers="50" isPrivate="***e" isTemp="false" pwd="test" />

   </Rooms>

   <Extensions>

      <extension name="json" className="jsonSample.as" type="script" />

   </Extensions>

   <Moderators status="on">

      <Mod name="modName" pwd="modPass" />

   </Moderators>

</Zone>

&bull;Start the server (in Windows using the Start Menu shortcuts, in Linux using the sfs script in the Server directory: ./sfs restart).

If you want to autostart the SmartFox Server under linux, add it to the startup sequence (with Ubuntu: copy/symlink the sfs script to /etc/init.d and***n sudo update-rc.d sfs defaults )
Create the scene in Unity


&bull;Create a new Unity project and select to import the Character Controller .unityPackage

You can always import more packages later, for now we only need this one.

&bull;For this tutorial we will create a simple box to walk around in:

◦Create a cube (GameObject > Create Other > Cube), in the Inspector set position to 0,0,0 and scale to 25,1,25. Rename it floor.

◦Create another cube, set position to 0,5,12 and scale to 25,10,1, rename it wall_1.

◦Copy wall_1, set position to 0,5,-12 and rename it wall_2.

◦Copy wall_1, set position to 12,5,0 and the rotation Y to 90. Rename it wall_3.

◦Copy wall_3, set position to -12,5,0 and rename it wall_4.

&bull;Add a directional light (GameObject > Create Other > Directional Light) and aim it in the box by rotating it.

It does not matter where you position a directional light in the scene, only the angle is important.

&bull;Add the 3rd Person Controller to the scene (position 0, 1.5, 0 places him in the center) and rename to localPlayer.

&bull;Save the scene as sc_City (I usually create a subfolder Scenes where I put the scenes) and add it to the Build Settings (ctrl-shift-b).
Add SmartFox to the Unity project


&bull;Download the SmartFox .Net API from the SmartFox API page.

&bull;In the Unity project folder, create a new folder Plugins in the Assets directory.

&bull;Copy the SmartFoxClient.dll from the API/binaries directory in the zipfile to Plugins folder.
Create the login page


&bull;Create a new scene in Unity, save it as sc_Login and add it to the Build Settings.

&bull;Create a new folder Scripts and in there create a new C# script, rename it gui_Login and copy the contents from the page BOX1 - gui_Login.cs script.

&bull;Create a new C# script, rename it SmartFox and copy the contents from the page BOX1 - SmartFox.cs script.

&bull;Create an empty GameObject, rename it to gui_Login, and attach the gui_Login script.

&bull;Save the scene and project and try***nning it. You should get a login page, after login the scene with the box is loaded.
Adding multiplayer Getting the components in place


&bull;We'll be using the network scripts that are provided with the SmartFox Island Demo, get the demo project here.

&bull;Copy the network folder with scripts from the demo to our scripts folder.

&bull;Copy the script Assets/Scripts/FpsStorage.cs and Assets/Scripts/FPSCounter.js from the demo to our scripts folder.

&bull;Add an empty object in the scene, rename it to " FPS" (with a space), attach both FPS scripts and add a GUIText component (Component > Rendering > GUIText).

&bull;Edit the NetworkController script:

◦Change Application.LoadLevel("login"); to Application.LoadLevel("sc_Login");

◦Change smartFoxClient.JoinRoom("The Garden"); to smartFoxClient.JoinRoom("Central Square");
Configuring local & remote player


&bull;Add the script NetworkTransformSender and AnimationSynchronizer to localPlayer. This will broadcast the position whenever the player moves so that other clients can update the position of this player.

&bull;Create a new folder Network Assets in the Project view, in that folder create a new prefab and rename it to remotePlayer.

&bull;In the Project view, drag the 3rd Person Controller (in Standard Assets > Character Controllers) onto the remotePlayer prefab.

&bull;Select the remotePlayer and remove the Third Person Camera, Third Person Controller and Character Controller.

&bull;Select the remotePlayer and add the scripts NetworkTransformReceiver and AnimationSynchronizer.
The network & spawn controller


&bull;Create an empty object, rename it to NetworkController and add the NetworkController script and the PlayerSpawnController script.

The NetworkController script updates movement of the other players and registers other SmartFox events (such as a player leaving or entering).

&bull;Select the NetworkController object and link the localPlayer from the Hierarchy to the Local Player Object field in the Inspector.

&bull;Select the NetworkController object and link the remotePlayer from the 'Project view' to the Remote Player Prefab field in the Inspector.

We do not need to create a remotePlayer object in the scene, because that is done dynamically when other players join. Therefore we link the prefab so the script knows what to instantiate.

&bull;Create 4 empty objects, place them in the box, rename to SpawnpointA - SpawnpointD and link to the NetworkController object (first set Size to 4 under Spawn Points).
Adding animation to remote players


&bull;To get the remote players to actually walk (instead of floating around), we need to add sendmessage commands to the Third Person Controller script. This networkcontroller monitors for these messages and upon receiving them will trigger the animation in the remote player object.

&bull;Edit ThirdPersonController.js in AssetsStandard AssetsCharacter ControllersSourcesScripts and replace the //ANIMATION sector part with the lines below (with added the sendmessage lines):
    // ANIMATION sector

    if(_animation) {

        if(_characterState == CharacterState.Jumping)

        {

            SendMessage("layAnimation", "jump");

            if(!jumpingReachedApex) {

                _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;

                _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;

                _animation.CrossFade(jumpPoseAnimation.name);

            } else {

                _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;

                _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;

                _animation.CrossFade(jumpPoseAnimation.name);            

            }

        }

        else

        {

            if(controller.velocity.sqrMagnitude < 0.1) {

                _animation.CrossFade(idleAnimation.name);

                SendMessage("layAnimation", "idle");

            }

            else

            {

                if(_characterState == CharacterState.Running) {

                    _animation***nAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0,***nMaxAnimationSpeed);

                    _animation.CrossFade***nAnimation.name);   

                }

                else if(_characterState == CharacterState.Trotting) {

                    _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, trotMaxAnimationSpeed);

                    _animation.CrossFade(walkAnimation.name);   

                }

                else if(_characterState == CharacterState.Walking) {

                    _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);

                    _animation.CrossFade(walkAnimation.name);   

                    SendMessage("layAnimation", "walk");

                }

               

            }

        }

    }

    // ANIMATION sector


Adding a in-game GUI with status window and chat


&bull;Load the sc_City scene.

&bull;For the chat, we will be using the scripts and skin that come with the Island Demo.

◦Copy the skin folder with gui files from the demo to our assets folder

◦Link the ChatController to the NetworkController by dragging the script from the Project view on the NetworkController object in the Hierarchy view.

◦Link the skin script to the ChatController that is attached to the NetworkController by dragging the skin script from the Skin folder in the Project view on the Skin field in the ChatController section of the NetworkController object (in the Hierarchy view).

&bull;Create a new C# script, rename it gui_Game and copy the contents from the page BOX1 - gui_Game.cs script.

&bull;Add some changes to the NetworkController.cs script to update the Status Window when players enter or leave.

◦The windows is updated through the UpdateStatusMessage function in gui_Game.cs, therefore we add:

Complete code for adapted NetworkController.cs is here: BOX1 - NetworkController.cs script.

private gui_Game statusHud;

statusHud = GetComponent(typeof(gui_Game)) as gui_Game;

statusHud.UpdateStatusMessage(message); // in OnUserEnterRoom and OnUserLeaveRoom

&bull;To be able to access the functions, link the gui_Game script to the NetworkController by dragging the script from the Project view on the NetworkController object in the Hierarchy view.

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-7 18:21 , Processed in 0.147305 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部