unet多人联机

函数

1.publicoverride void OnStartLocalPlayer()

    {

       GetComponent<MeshRenderer>().material.color = Color.blue;

    }

Description

描述

Called when the local player object hasbeen set up.

当本地玩家对象被设置时调用

This happens after OnStartClient(), as itis triggered by an ownership message from the server.

 Thisis an appropriate place to activate components or functionality that shouldonly be active for the local player, such as cameras and input.

发生这种情况后,onstartclient(),因为它是由来自服务器的所有消息触发。这是一个适当的地方,激活组件或功能,只应该是活跃的本地播放器,如摄像头和输入

ps:垃圾机翻自行解决,因为辣眼睛不再翻译

 

2.       public override void OnStartServer()

   {

        for(inti=1;i<=enemyamount;i++)

       {

            Vector3 position = new Vector3(Random.Range(-6f, 6f), 0, Random.Range(-6f, 6f));

            Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);//四元数 3d图形学问题

            GameObject enemy = Instantiate(enemyprefab, position, rotation)as GameObject;

            NetworkServer.Spawn(enemy);//同步

       }

   }Description

This is invoked for NetworkBehaviour objectswhen they become active on the server.

This could be triggered byNetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn()for objects that are dynamically created.

This will be called for objects on a "host" as well as for object ona dedicated server.

3Object.FindObjectsOfType

publicstatic Object[] FindObjectsOfType(Type type);

Parameters

type

The type of object to find.

Returns

Object[] Thearray of objects found matching the type specified.

Description

Returns a list of all active loaded objectsof Type type.

It will return no assets (meshes, textures,prefabs, ...) or inactive objects.

Please note that this function is very slow. It is not recommended to use thisfunction every frame. In most cases you can use the singleton pattern instead.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnMouseDown() {
        HingeJoint[] hinges = FindObjectsOfType(typeof(HingeJoint)) as HingeJoint[];
        foreach (HingeJoint hinge in hinges) {
            hinge.useSpring = false;
        }
    }
}

 

子弹的生成需要server完成,然后把子弹同步到客户端

   [Command]//called in client,run in server

   void CmdFire()

    {

       GameObject bullet = Instantiate(bulletprefab, fireposition.position,fireposition.rotation) as GameObject;

       bullet.GetComponent<Rigidbody>().velocity =bullet.transform.forward * 34;

       Destroy(bullet, 2);

       NetworkServer.Spawn(bullet);//同步方法

 

       Description:

       Spawnthe given game object on all clients which are ready.

       Thiswill cause a new object to be instantiated from the registered prefab, or froma custom spawn function.

    }

 

 

初始碰撞检测

void OnCollisionEnter(Collisioncollision){}

Description

OnCollisionEnter is called when thiscollider/rigidbody has begun touching another rigidbody/collider.

In contrast to OnTriggerEnter, OnCollisionEnteris passed the Collision class and not a Collider. The Collision class containsinformation about contact points,

 impact velocity etc. If you don't usecollisionInfo in the function, leave out the collisionInfo parameter as thisavoids unneccessary calculations.

 Notes: Collision events are only sent if oneof the colliders also has a non-kinematic rigidbody attached. Collision eventswill be sent to disabled MonoBehaviours,

to allow enabling Behaviours in response tocollisions.

 

使ui始终朝向照相机

transform.LookAt(Camera.main.transform);

public void LookAt(Transform target,Vector3 worldUp = Vector3.up

 

Description

 

Rotates the transform so the forward vectorpoints at /target/'s current position.

 

Then it rotates the transform to point itsup direction vector in the direction hinted at by the worldUp vector.

 Ifyou leave out the worldUp parameter, the function will use the world y axis.worldUp is only a hint vector.

The up vector of the rotation will onlymatch the worldUp vector if the forward direction is perpendicular to worldUp.

 

判断是否在本地端或服务端(bool)

isServer  

Description

Returns true if your peer type is server

islocalplayer

Description

This returns true if this object is theone that represents the player on the local machine.

This is set when the server has spawnedan object for this particular client.

参考:

Unity5.1 新的网络引擎UNET(八) UNET 系统概括,unity5.1unet


孙广东  2015.7.12

 

Server and Host

Unity 网络系统,游戏有一个服务器和多个客户端。当没有专用的服务器时,客户端之一扮演服务器的角色 — — 我们称之为此客户端“host”


host
(主机)是一个服务器和客户端在同一进程中。主机将使用一种特殊的客户端被称为 LocalClient,而其他客户端是 RemoteClientsLocalClient (本地) 服务器通信通过直接的函数调用和消息队列,因为它是同一进程中。它实际上与服务器共享一个场景。RemoteClients 通过定期的网络连接与服务器进行通信。

网络系统的目标是 LocalClients RemoteClients 的代码是相同的,所以开发者只需大部分时间想一种类型的客户端的。


Instantiate and Spawn

Unity中,GameObject.Instantiate 创建新的Unity游戏对象。但与联网系统在一起,对象必须也会“spawned” 要在网络上激活。这只能在服务器上,并且导致要在连接的客户端上创建对象。一旦对象被Spawning System生成,分布式的对象生命周期管理和状态同步。

Players, Local Players and Authority


在网络系统中,玩家对象都是特殊的。还有伴玩游戏,每个人的player 对象和命令都将路由到该对象。一个人不能对另一个人的player对象调用命令 - 只有他们自己。所以是的“my” player 对象的概念。当添加一个player 和联系把他们连接时,这个player对象就成为该玩家客户端上的“local player” 对象。那里是被设置为 true,并且回调在客户端上的对象调用的 OnStartLocalPlayer() 属性 isLocalPlayer。下图显示两个客户端和他们 local players.


只是 “yours” player对象  将已设置了 isLocalPlayer 标志。这可以用于筛选  输入的处理,能够处理相机附件,或做任何其他客户端只应做为您的player的身边事。


除了 isLocalPlayerplayer对象可以有 “local authority”.  这意味着其拥有者的客户端上的player对象是负责对象 — — 它有授权。最常用的是控制运动,但也被使用其他的东西。NetworkTransform组件明白这一点,并将从客户端发送移动,如果设置该选项。NetworkIdentity 有一个复选框用于设置 LocalPlayerAuthority

对于非玩家对象如敌人,还有没有相关联的客户端,所以权威驻留在服务器上。

 

NetworkBehaviour 上的属性 "isAuthority" 可以用来告诉是否对象具有授权。所以非玩家对象在服务器上,具有权威和player对象与 localPlayerAuthority 有授权在其拥有者的客户端上。

 

 

unity5.1 Unet同步问题总结  参考资料:http://www.cnblogs.com/xianunity/p/5646138.html

 

状态同步是从服务器向客户端方向上的。本地客户端没有序列化的数据,因为它和服务器共享同一个场景。任何为本地客户端序列化的数据都是多余的。

然而,SyncVar钩子函数会被本地客户端调用。注意数据不会从客户端向服务器同步,这个方向上的操作叫做命令(Commands)。

 

[SyncVar]属性

SyncVar is an attribute that can be put onmember variables of UNeBehaviour classes.

These variables will have their valuessychronized from the server to clients in the game that are in the ready state.

 

Setting thje value of a [SyncVar] marks itas dirty, so it will be sent to clients at the end of the current frame.

Only simple values can be marked as[SyncVars]. The type of the SyncVar variable cannot be from an external DLL orassembly.

 

Variables

hook      Thehook attribute can be used to specify a function to be called when the sync varchanges value on the client.

 

同步变量是NetworkBehaviour脚本中的成员变量,他们会从服务器同步到客户端上。当一个物体被派生出来之后,或者一个新的玩家中途加入游戏后,

他会接收到他的视野内所有物体的同步变量。成员变量通过[SyncVar]标签被配置成同步变量:

class Player :NetworkBehaviour

{

[SyncVar]

int health;

 

public void TakeDamage(int amount)

{

if (!isServer)

return;

 

health -= amount;

}

}

同步变量的状态在OnStartClient()之前就被应用到物体上了,所以在OnStartClient函数中,物体的状态已经是最新的数据。

 

  同步变量可以是基础类型,如整数,字符串和浮点数。也可以是Unity内置数据类型,如Vector3和用户自定义的结构体,但是对结构体类型的同步变量,

如果只有几个字段的数值有变化,整个结构体都会被发送。每个NetworkBehaviour脚本可以有最多32个同步变量,包括同步列表(见下面的解释)。

 

  当同步变量有变化时,服务器会自动发送他们的最新数据。不需要手工为同步变量设置任何的脏数据标志位。

 

  注意在属性设置函数中设置一个同步变量的值不会使他的脏数据标志被设置。如果这样做的话,会得到一个编译期的警告。

因为同步变量使用他们自己内部的标识记录脏数据状态,在属性设置函数中设置脏位会引起递归调用问题。

 

  同步变量还可以指定函数,使用hook:

 

  当服务器改变了playerName的值,客户端会调用OnMyName这个函数

[SyncVar(hook = "OnMyName")]

public string playerName = "";

//变量值会传入函数要定义参数

public void OnMyName(string newName)

{

           playerName = newName;

           nameInput.text = playerName;

}

客户端对服务器端操作

【Command】  属性

  命令从客户端上的物体发给服务器上的物体。出于安全考虑,命令只能从玩家控制的物体上发出,因此玩家不能控制其他玩家的物体。要把一个函数变成命令,需要给这个函数添加[Command]属性,并且为函数名添加“Cmd”前缀,这样这个函数会在客户端上被调用时在服务器上运行。所有的参数会自动和命令一起发送给服务器。

  命令函数的名字必须要有“Cmd”前缀。在阅读代码的时候,这也是个提示这个函数比较特殊,他不像普通函数一样在本地被执行。
  注意如果每一帧都发送命令消息,会产生很多的网络流量。

  默认情况下,命令是通过0号通道(默认的可靠传输通道)进行传输的。所以默认情况下,所有的命令都会被可靠地发送到服务器。可以使用命令的“Channel”参数修改这个配置。参数是一个整数,表示通道号。
1
号通道是默认的不可靠传输通道,如果要用这个通道,把这个参数设置为1,示例如下:
[Command(channel=1)]


  从Unity5.2开始,可以从拥有客户端授权的非玩家物体发出命令。这些物体必须是使用函数NetworkServer.SpawnWithClientAuthority()派生出来的,或者是使用NetworkIdentity.AssignClientAuthority()授权过的。从物体发送出来的命令会在服务器上运行,而不是在相关玩家物体所在的客户端上。

class in UnityEngine.Networking

Description

 

This is an attribute that can be put onmethods of NetworkBehaviour classes to allow them to be invoked on the serverby sending a command from a client.

 

[Command] functions are invoked on theplayer object associated with a connection. This is setup in response to the"ready" message,

 bypassing the player objec to the NetworkServer.PlayerIsReady() function. Thearguments to the command call are seriialized across the network,

 !!sothat the server function is invoked with the same values as the function on theclient. These functions must begin with the prefix "Cmd".!!

 

using UnityEngine;

using UnityEngine.Networking;

 

 

public class Player : NetworkBehaviour {

 

       intmoveX = 0;

       intmoveY = 0;

       floatmoveSpeed = 0.2f;

      

       voidUpdate () {

              if(!isLocalPlayer) {

                     return;

              }

             

              //input handling for local player only

              intoldMoveX = moveX;

              intoldMoveY = moveY;

             

              moveX= 0;

              moveY= 0;

             

              if(Input.GetKey(KeyCode.LeftArrow)) {

                     moveX-= 1;

              }

              if(Input.GetKey(KeyCode.RightArrow)) {

                     moveX+= 1;

              }

              if(Input.GetKey(KeyCode.UpArrow)) {

                     moveY+= 1;

              }

              if(Input.GetKey(KeyCode.DownArrow)) {

                     moveY-= 1;

              }

              if(moveX != oldMoveX || moveY != oldMoveY)       {

                     CmdMove(moveX,moveY);

              }

       }

      

       [Command]

       publicvoid CmdMove(int x, int y) {

              moveX= x;

              moveY= y;

              isDirty= true;

       }

      

       publicvoid FixedUpdate() {

              if(NetworkServer.active) {

                     transform.Translate(moveX* moveSpeed, moveY * moveSpeed, 0);

              }

       }

}

服务器端对非本地角色进行操作

[ClientRpc]属性

客户端远程过程调用(ClientRPC Calls)

  客户端远程过程调用从服务器的物体发送到客户端的物体上去。他们可以从任何带有NetworkIdentity并被派生出来的物体上发出。因为服务器拥有授权,所以这个过程不存在安全问题。要把一个函数变成客户端远程过程调用,需要给函数添加[ClientRpc]属性,并且为函数名添加“Rpc”前缀。这个函数将在服务端上被调用时,在客户端上执行。所有的参数都将自动传给客户端。

  客户端远程调用必须带有“Rpc”前缀。在阅读代码的时候,这将是个提示 – 这个函数比较特殊,不像一般函数那样在本地执行。

class Player :NetworkBehaviour
{

[SyncVar]
int health;

[ClientRpc]
void RpcDamage(int amount)
{
Debug.Log("Took damage:" +amount);
}

public void TakeDamage(int amount)
{
if (!isServer)
return;

health -= amount;
RpcDamage(amount);
}
}

  当使用伺服器模式运行游戏的时候,客户端远程调用将在本地客户端执行 – 即使他其实和服务器运行在同一个进程。因此本地客户端和远程客户端对客户端远程过程调用的处理是一样的。
  如果想将[ClientRpc]用在点击事件的同步操作上,不能直接绑定点击事件函数,而是应该起一个新的Rpc函数,点击事件去绑定这个Rpc函数,Rpc函数里才是对点击事件的操作:

 //点击事件
    public void ClickDXView()
    {

            RpcDXView();

    }

   [ClientRpc]
    public void RpcDXView()
    {
        readyPN.gameObject.SetActive(false);
        startGm();
       Camera.main.GetComponent<DOTweenPath>().DOPlay();
    }

回调函数--

[ServerCallback]:只执行在服务器端,并使一些特殊函数(eg:Update)不报错(若在此函数中改变了带有syncvar的变量,客户端不同步)

          (使用ServerCallback时,将Update中的重要语句摘出来写入Rpc函数中并调用)

[ClientCallback]:只执行在客户端

另:[Server]:只执行在服务器端但是不能标识一些特殊函数(可以在这里调用Rpc类函数)

 

远程过程的参数

  传递给客户端远程过程调用的参数会被序列化并在网络上传送,这些参数可以是:

- 基本数据类型(字节,整数,浮点树,字符串,64位无符号整数等)

- 基本数据类型的数组

- 包含允许的数据类型的结构体

- Unity内建的数学类型(Vector3,Quaternion等)

- NetworkIdentity

- NetworkInstanceId

- NetworkHash128

- 带有NetworkIdentity组件的物体

  远程过程的参数不可以是游戏物体的子组件,像脚本对象或Transform,他们也不能是其他不能在网络上被序列化的数据类型。

  

  在使用过程中发现一个问题:带有NetworkIdentity的组件在运行之前不能是隐藏的,否则同步会受影响,在代码Start函数中置为SetActive = false,或者因为网络问题一开始隐藏的物体在后续同步中都没有问题。

 

Simple Game Summary

 

With this example, we have covered mostof the basic concepts and components needed to make a simple MultiplayerNetworked game.

We have covered that basic architectureof a project written to use the Multiplayer Networking High Level API(HLAPI). We understand that when using the HLAPI, the Server and all of theClients are executing the same code from the same scripts on the sameGameObjects at the same time. We have discussed how to control the flow oflogic when using the HLAPI with checks for the Server,LocalPlayer and Client.

We have covered the RPCs availableto the HLAPI; both Commands and ClientRpc’s.We have seen how to work with Commands, which are called on theClient and are executed on the Server; and ClientRpc’s, which arecalled on the Server, but invoked on the Clients.

We have covered SyncVars and SyncVarhooks. We can keep variables in sync with the [SyncVar] attribute,and call functions when the values change with SyncVar hooks.

We have seen how we can keep the Transforms ofour networked GameObjects synchronized with the the NetworkIdentity andtheNetworkTransform component.

We have covered many of the built-incomponents available under Networking in the editor, including the NetworkManager,theNetworkManagerHUD and the NetworkStartPosition.There are more built-in components available under Networking, but these aremore specialized, not used as frequently and will be covered in other lessons.

We hope that this lesson has proven tobe a good starting point for understanding how to make a Networked Multiplayerproject and that this quick overview of the most important aspects of thesystem have shown how easy it is to create Multiplayer games in Unity.

For more information on MultiplayerNetworking, please see the lessons and documentation pages linked below.

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值