最近学习了Networking的用法 以及官方实例本地化单机《坦克大战》 实现了加入战斗 选择队伍 攻击掉血 玩家死亡 玩家得分 在介绍坦克大战之前 先介绍一个简单的玩家移动同步
Step 1. 创建场景 一个panel 以及cube预设体 cube 注意 实时玩家名字功能没有 不用管他 一个是主机按钮 一个是客服端按钮 输入框是ip地址 与端口号 端口号我一般设置为23456 cube预设体带上NetworkTransform 、刚体 限定下x,y,z的rotation 注意 cube身上组件Network Identity 勾选local player Authority
Step 2. 两个脚本 1个NetworkInit 本地化局域网初始化工作 不需要挂在对象山 1个GameObjectControl脚本 挂在空物体上 1个Player脚本 挂在cube上
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.UI;
usingUnityEngine.Networking;
public class NetworkInit:MonoBehaviour{
public InputField ipInput;
public Input FieldportInput;
public InputField playerName;
//客服端实例
public Network Clientclient;
//网络预设体
public GameObject playerPrefab;
voidAwake()
{
Application.runInBackground=true;
}
//Usethisforinitialization
voidStart(){
//GameObjectControl.ins.playName=this.playerName;
GameObjectControl.instant.networkEvent=ClientDisconnect;
}
//Updateiscalledonceperframe
voidUpdate(){
}
//委托回调方法
publicvoidClientDisconnect()
{
client.Disconnect();
Application.Quit();
}
//服务器初始化服务器按钮回调方法
publicvoidServerInit()
{
//根据指定端口开启服务器
NetworkServer.Listen(int.Parse(portInput.text));
NetworkServer.RegisterHandler(MsgType.Connect,OnClientConnectToMyServer);
NetworkServer.RegisterHandler(MsgType.AddPlayer,OnServerAddPlayer);
NetworkServer.RegisterHandler(MsgType.Disconnect,OnServerFindDisconnected);
client=ClientScene.ConnectLocalServer();
//注册回调
client.RegisterHandler(MsgType.Connect,OnConnectToSever);
}
//客服端按钮调回调方法连接服务器
publicvoidClientConnectToServer()
{
client=newNetworkClient();
//连接服务器
client.Connect(ipInput.text,int.Parse(portInput.text));
//注册回调
client.RegisterHandler(MsgType.Connect,OnConnectToSever);
}
//当客服端连接到服务器时调用调用方是服务器
privatevoidOnClientConnectToMyServer(NetworkMessagemsg)
{
Debug.Log("AClientConnected"+msg.conn.address);
}
//当客服端连接到服务器时调用调用房是客服端自己
privatevoidOnConnectToSever(NetworkMessagemsg)
{
Debug.Log("HasConnectedToServer:"+msg.conn.address);
//客服端准备
ClientScene.Ready(msg.conn);
ClientScene.RegisterPrefab(playerPrefab);
//请求服务器添加玩家
ClientScene.AddPlayer(0);
}
//
privatevoidOnServerAddPlayer(NetworkMessagemsg)
{
floatrandomX=Random.Range(-5,5);
floatrandomY=Random.Range(-5,5);
//生成对象
GameObjectcurrentPlayer=Instantiate(playerPrefab,newVector3(2,0.5f,-2),Quaternion.identity);
//给请求的客服端分配全限
NetworkServer.AddPlayerForConnection(msg.conn,currentPlayer,0);
//给所有准好的客服端上实例化物体
NetworkServer.Spawn(currentPlayer);
}
//???
privatevoidOnServerFindDisconnected(NetworkMessagemsg)
{
NetworkServer.DestroyPlayersForConnection(msg.conn);
}
}
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.UI;
public delegate voidOnNetworkEventHandle();
public class GameObjec tControl:MonoBehaviour{
public static GameObject Controlinstant;
public Player currentPlayer=null;
//publicInputFieldplayName;
//publicOnNetworkEventHandlenetworkEvent;
voidAwake()
{
instant=this;
}
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
}
}
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.UI;
usingUnityEngine.Networking;
public class Player:NetworkBehaviour{
public float hor,ver;
public float speed=3f;
//Usethisforinitialization
void Start(){
if(isLocalPlayer){
GameObject Control.instant.currentPlayer=this;
}
}
//Updateiscalledonceperframe
voidUpdate(){
//是否是当前玩家,是否拥有权限
if(isLocalPlayer){
hor=Input.GetAxis("Horizontal");
ver=Input.GetAxis("Vertical");
transform.position+=newVector3(hor,0,ver)*Time.deltaTime*speed;
//CmdUpdatePlayerName(GameController.ins.playerName.text);
//如果玩家掉下去,强制断开
if(transform.position.y<-5&&!isServer){
//GameObjectControl.instant.networkEvent();
}
}
}
}