- 最后登录
- 2015-3-2
- 注册时间
- 2012-11-21
- 阅读权限
- 20
- 积分
- 231
- 纳金币
- 208
- 精华
- 0
|
我在认识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的变化。二在客户端那边的话我们接受并解压或者解码送发送的信息并赋值给当前的信息。
好了,这个教程到这,基本你就会连接两个场景了,下一个教程我们来学习如何交互两个场景。这个附件就是这个完整的程序。
|
|