标签:
1、非授权服务器
----------------------------非授权服务器代码------------------------------
using UnityEngine;
using System.Collections;
public class networkserver : MonoBehaviour {
private string roip="127.0.0.1";
private int rport=10000;
//private int lport=10000;
private int concount=15;
private bool usenat = true ;
void OnGUI(){
switch (Network .peerType ){
case NetworkPeerType .Disconnected :
StartServer();
break ;
case NetworkPeerType.Server :
Onserver();
break ;
case NetworkPeerType.Connecting:
break;
}
}
void StartServer(){
roip =GUI.TextField(new Rect(10,30,100,20),roip);
if (GUI.Button(new Rect(10,50,100,30),"创建服务器")){
Network.incomingPassword="unitynetwork";
//启动服务器
NetworkConnectionError error=Network.InitializeServer(concount,rport,usenat);
Debug .Log (error);
}
}
void Onserver(){
GUILayout.Label ("创建服务器成功!等待连接。。。");
string ip=Network.player.ipAddress;
int port=Network.player.port ;
//显示外网IP
string exip=Network.player.externalIP;
GUILayout.Label ("IP地址:"+ip+"\n端口号:"+port+"\n外部IP:"+exip);
int connectlength=Network.connections.Length;
for(int i=0;i<connectlength;i++){
GUILayout.Label ("连接的IP:"+Network.connections[i].ipAddress );
GUILayout.Label ("连接的端口:"+Network.connections[i].port );
}
if(GUI.Button(new Rect(10,340,100,30),"断开连接")){
Network .Disconnect (200);
}
}
void OnPlayerDisconnected( NetworkPlayer player) {
//Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
}
2、客户端
-------------------------------------客户端代码----------------------------------------------
using UnityEngine;
using System.Collections;
public class networkclient : MonoBehaviour {
private string roip="192.168.0.100";
private int lport=10000;
//private bool usenat=false;
void OnGUI(){
switch (Network.peerType){
case NetworkPeerType .Disconnected :
Startconnect();
break;
case NetworkPeerType .Client:
Clientto();
break;
case NetworkPeerType.Connecting:
Debug.Log ("连接中");
break;
}
}
void Startconnect(){
roip=GUILayout.TextField(roip);
//lport=GUILayout.TextField("请输入要连接的端口",lport);
if (GUILayout.Button("连接服务器")){
NetworkConnectionError error=Network .Connect(roip,lport,"unitynetwork");
Debug.Log (error);
}
}
void Clientto(){
GUILayout.Label ("成功连接到服务器!");
}
//连接成功后系统自动调用函数
void OnConnectedToServer(){
foreach (GameObject go in FindObjectsOfType(typeof (GameObject))){
go.SendMessage("OnNetworkLoaded",SendMessageOptions.DontRequireReceiver);
}
}
void OnDisconnectedFromServer(NetworkDisconnection info){
//移除属于这个玩家的ID的RPC函数
Network.RemoveRPCs(Network.player);
//移除属于这个玩家的物体
Network.DestroyPlayerObjects(Network.player);
//重新加载场景
Application.LoadLevel(0);
}
}
--------------------------------实例化玩家代码------------------------------------
using UnityEngine;
using System.Collections;
public class playergame : MonoBehaviour {
public Transform control;
void OnNetworkLoaded(){
//实例化该目标对象
Network.Instantiate(control,transform.position,transform.rotation,0);
}
void Update(){
if(Network.isClient ){
Vector3 Direction=new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
float speed=5.0f;
GameObject.FindGameObjectWithTag("Player").transform.Translate(speed*Direction*Time.deltaTime);
}
}
}
在同时运行这两个工程时,输入了ip连接服务器的时候,要点击一下创建了服务器的工程窗口。。。。。。。。。
标签:
原文地址:http://www.cnblogs.com/ZeroMurder/p/5951370.html