12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 19162|回复: 13
打印 上一主题 下一主题

[教程] Unity3D基础认知——网络编程(一)

[复制链接]

12

主题

2

听众

231

积分

设计实习生

Rank: 2

纳金币
208
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2012-11-21 18:25:44 |只看该作者 |倒序浏览
我在认识unity的过程也是挺崎岖的,我并非一开始接触了unity3d,而是在图形学,和OpenGL之后认识的它。当然,之后有接触了OGRE这一工具。其实,只是使用方式不一样罢了,基本原理和功能还是一样的(当然,对于OGRE这样的开源渲染引擎就需要其他的一些支持了)。闲话不多,先把最近的知识分享出来,有时间在来整理一起的那些笔记。

网络编程,其实我的网络基础并不是那么好,所以就不讲一些基础了,主要是我也不是很懂,直接上基于Unity3D的API就行了。

1. 关于网络传输

在网络传输的过程中是以字符串的形式传播的,即便是数字也是用字符的形式,所以很需要一个函数 在JS中是 parseInt() 这个函数主要功能是 讲数字转换为对应的字符串,具体参数请参考:http://www.w3school.com.cn/js/jsref_parseInt.asp。

2.Unity中关于网络编程的 主要类: NetWork 类

我们可以看一下 这个类的 方法和 属性 如下:


   
        
            incomingPassword
            
            Set the password for the server (for incoming connections).
            
        
        
            logLevel
            
            Set the log level for network messages (default is Off).
            
        
        
            connections
            
            All connected players.
            
        
        
            player
            
            Get the local NetworkPlayer instance
            
        
        
            isClient
            
            Returns ***e if your peer type is client.
            
        
        
            isServer
            
            Returns ***e if your peer type is server.
            
        
        
            peerType
            
            The status of the peer type, i.e. if it is disconnected, connecting, server or client.
            
        
        
            sendRate
            
            The default send rate of network updates for all Network Views.
            
        
        
            isMessageQueueRunning
            
            Enable or disable the processing of network messages.
            
        
        
            time
            
            Get the current network time (seconds).
            
        
        
            minimumAllocatableViewIDs
            
            Get or set the minimum number of ViewID numbers in the ViewID pool given to clients by the server.
            
        
        
            natFacilitatorIP
            
            The IP address of the NAT punchthrough facilitator.
            
        
        
            natFacilitatorPort
            
            The port of the NAT punchthrough facilitator.
            
        
        
            connectionTesterIP
            
            The IP address of the connection tester used in Network.TestConnection.
            
        
        
            connectionTesterPort
            
            The port of the connection tester used in Network.TestConnection.
            
        
        
            maxConnections
            
            Set the maximum amount of connections/players allowed.
            
        
        
            proxyIP
            
            The IP address of the proxy server.
            
        
        
            proxyPort
            
            The port of the proxy server.
            
        
        
            useProxy
            
            Indicate if proxy support is needed, in which case traffic is relayed through the proxy server.
            
        
        
            proxyPassword
            
            Set the proxy server password.
            
        
   


Class Functions

   
        
            InitializeServer
            
            Initialize the server.
            
        
        
            InitializeSecurity
            
            Initializes security layer.
            
        
        
            Connect
            
            Connect to the specified host (ip or domain name) and server port.
            
        
        
            Disconnect
            
            Close all open connections and shuts down the network interface.
            
        
        
            CloseConnection
            
            Close the connection to another system.
            
        
        
            AllocateViewID
            
            Query for the next available network view ID number and allocate it (reserve).
            
        
        
            Instantiate
            
            Network instantiate a prefab.
            
        
        
            Destroy
            
            Destroy the object associated with this view ID across the network.
            
        
        
            DestroyPlayerObjects
            
            Destroy all the objects based on view IDs belonging to this player.
            
        
        
            RemoveRPCs
            
            Remove all RPC functions which belong to this player ID.
            
        
        
            RemoveRPCsInGroup
            
            Remove all RPC functions which belong to given group number.
            
        
        
            SetLevelPrefix
            
            Set the level prefix which will then be prefixed to all network ViewID numbers.
            
        
        
            GetLastPing
            
            The last ping time to the given player in milliseconds.
            
        
        
            GetAveragePing
            
            The last average ping time to the given player in milliseconds.
            
        
        
            SetReceivingEnabled
            
            Enable or disables the reception of messages in a specific group number from a specific player.
            
        
        
            SetSendingEnabled
            
            Enables or disables transmission of messages and RPC calls on a specific network group number.
            
        
        
            TestConnection
            
            Test this machines network connection.
            
        
        
            TestConnectionNAT
            
            Test the connecction specifically for NAT punchthrough connectivity.
            
        
        
            HavePublicAddress
            
            Check if this machine has a public IP address.
            
        
   

以上出走帮助文档,如果英文不好可以参考:http://game.ceeger.com/Script/Network/Network.html

3.接下来我们来做一个简单练习作为今天的学习:

      3.1 建立服务器:

=============================================================================

if (GUILayout.Button ("Start Server"))
{
//Start a server for 32 clients using the "connectPort" given via the GUI
//Ignore the nat for now
Network.useNat = false;
Network.InitializeServer(32, connectPort);
}

===============================================================================

      3.2 连接已有服务器:

===============================================================================

if (GUILayout.Button ("Connect as client"))
{
//Connect to the "connectToIP" and "connectPort" as entered via the GUI
//Ignore the NAT for now
Network.useNat = false;
Network.Connect(connectToIP, connectPort);
}
===============================================================================

在这两个操作中,主要涉及了NetWork类中的两个方法  InitializeServer() 和  Connect ()

接下来,你可以打开两个程序运行并连接一下了,你会发现只有互动窗口才会有反应,为啥呢?我们需要设置后台来运行。

你可以在脚本里设置:Application***nInBackground = ***e;

亦可以 playersettings中设置***n in background

4.你来决定发送什么

===============================================================================

function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)
{
        if (stream.isWriting){

               
                var pos : Vector3 = transform.position;               
                stream.Serialize(pos.x);//"Encode" it, and send it
               
               
        }else{

               
                var posReceive : Vector3 = Vector3.zero;
                stream.Serialize(posReceive.x); //"Decode" it and receive it
                transform.position.x = posReceive.x;

        }
}

===============================================================================

在这里面,我们重写了    function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo)

其中:if (stream.isWriting) 表示 stream被写了说明是服务器的行为,否则是 客户端的行为。我们将物体的坐标保存并装载(压制或者叫编码)所需要发送的内容,我们这里发送了位置信息中x的变化。二在客户端那边的话我们接受并解压或者解码送发送的信息并赋值给当前的信息。
好了,这个教程到这,基本你就会连接两个场景了,下一个教程我们来学习如何交互两个场景。这个附件就是这个完整的程序。



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

使用道具 举报

Zack    

459

主题

1

听众

5478

积分

高级设计师

Rank: 6Rank: 6

纳金币
5531
精华
0

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

沙发
发表于 2012-11-22 18:11:58 |只看该作者
学习学习,感谢!!
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

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

板凳
发表于 2012-11-22 20:01:20 |只看该作者
顶,支持这个教程
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

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

地板
发表于 2012-11-22 20:50:40 |只看该作者
如果有一些photon的教程就更好
回复

使用道具 举报

may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52304
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

5#
发表于 2012-11-30 17:12:53 |只看该作者
支持楼主的帖子
回复

使用道具 举报

2

主题

9

听众

6387

积分

高级设计师

Rank: 6Rank: 6

纳金币
881
精华
0

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

6#
发表于 2013-4-5 09:18:21 |只看该作者
支持网络编程  这方面的资料太少了
回复

使用道具 举报

0

主题

1

听众

25

积分

设计初学者

Rank: 1

纳金币
5
精华
0

活跃会员 灌水之王

7#
发表于 2013-7-5 17:39:46 |只看该作者
虽然看不懂,只怪自己水平太低了,谢谢楼主的资源
回复

使用道具 举报

0

主题

1

听众

89

积分

设计初学者

Rank: 1

纳金币
-11
精华
0

活跃会员 灌水之王

8#
发表于 2013-7-7 17:07:40 |只看该作者
学习学习
回复

使用道具 举报

0

主题

0

听众

16

积分

设计初学者

Rank: 1

纳金币
1
精华
0
9#
发表于 2013-11-12 10:53:41 |只看该作者
....附件呢
回复

使用道具 举报

6

主题

3

听众

2195

积分

中级设计师

Rank: 5Rank: 5

纳金币
175
精华
0

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

10#
发表于 2013-11-12 11:35:27 |只看该作者
举双脚顶。
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

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

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

GMT+8, 2024-4-27 05:35 , Processed in 0.107201 second(s), 37 queries .

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

© 2008-2019 Narkii Inc.

回顶部