码迷,mamicode.com
首页 > 其他好文 > 详细

非授权服务器连接

时间:2016-10-12 06:54:23      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!