IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Node.js服务器进行 Unity Multiplayer游戏开发

    u010019717发表于 2016-05-04 09:27:01
    love 0

    孙广东   2016.5.4


                如何用一个专用的服务器驱动的Node.Js 创建我们自己的多人游戏。这将建立在 websocket的实时响应的多人游戏。   具体逻辑自己 编写吧

         首先在 AssetStore中搜索  Web   Socket 就看到Unity官方的 插件‘

    https://www.assetstore.unity3d.com/cn/#!/content/38367


    计算机生成了可选文字:
Simple Web Sockets for Unity WebCL 
—nit' Essentials Projects 
unity Technologies 
Free

             然后介绍一个 开源库 Socket IO


    https://www.assetstore.unity3d.com/en/#!/content/21721


    计算机生成了可选文字:
Socket_K) for Unny 
Scri pting i Net-work 
Fabio Panettieri 
(X 32) 
Free


    这有片文章对这两个的对比


    https://gist.github.com/zeusdeux/5491cff541fb4ac4c142


    然后在项目中导入   SocketIO


    下载开发工具   Brackets 

    在项目的Asset 文件夹同级新建  目录 server  , 然后使用 Brackets 打开这个文件夹。


    我们搜索 Socket IO , 进入地址为: http://socket.io/get-started/chat/


    http://socket.io/docs/#  文档可以查看各种API。



    计算机生成了可选文字:
Now, in order to easily populate the dependencies with the things we need, we'll use np. install - 
-save . 
npm install --save express@4.Ie.2


    来到这个server  文件夹下 进入CMD  , 然后输入


    计算机生成了可选文字:
1 crosoft Windows 6.1.7601] 
(c) 2009 Mi crosoft Corporation- 
install express 
expressa4.13.4 node_modules\express 
escape-html@l.0.3 
varyal. 0.1


    然后就在项目中出现了 


    计算机生成了可选文字:
File Edit 
Server 
node modules 
express 
server os 
View 
Navigate


    还要安装 :


    计算机生成了可选文字:
D: install socket. io 
socket . io@1.4.5 . io 
has-binarß0.1.7 (isarraß0.0.1) 
debug@2.2.o (msao.7.1)


    客户端连接服务器

    // require()异步加载socket.io,  然后执行回调函数构造一个IO
    
    var io = require('socket.io')(process.env.PORT || 3000);
    
    // 输出一个Log
    
    console.log('server started');
    


    同样在控制台   启动服务器, ip:localhost, port:3000        输入:     


    计算机生成了可选文字:
server . js 
server started


    Node  中添加这样代码:


    io.on('connection',function(socket){
    
       console.log('客户端被连接');
    
    })
    


                unity中新建一个main 场景,  新建一个对象 命名 NetWork  然后添加脚本 SocketIOComponent.cs  然后设置 端口号为咱们代码中的3000。


    计算机生成了可选文字:
V Socket 10 Component (Script) 
Auto Connect 
Reconnect Delay 
Ack Expiration Time 
R SocketIOComponel 
ns://127.o.o. 
:3000 soc 
1800


    然后我们Cmd启动  Node服务器,  然后在play model Unity编辑器。 会看到输出:


    计算机生成了可选文字:
server . js 
erver started


    


沪ICP备19023445号-2号
友情链接