我已经编写了两个程序,我希望这两个程序在我的Android设备上运行,这些设备位于同一个网络上但是一个简单的客户端服务器程序它将在两个设备上运行并且将建立连接......但是Unity主服务器将无法运行正常,无法连接两个在同一网络上的android wifi设备,这个程序将在localhost上正常运行。 这是简单的客户端服务器代码。这个代码运行正常
var object:Transform;
static var Isconnected:boolean=false;
//172.16.11.4
function Start()
{
Application.runInBackground = true;
Debug.Log("run in back groud");
}
function Update()
{
if(Isconnected)
var temp:Transform;
temp=Instantiate(object,transform.position, transform.rotation);
}
function OnGUI()
{
if(Network.peerType==NetworkPeerType.Disconnected)
{
if(GUILayout.Button(" Initlized server"))
{
var useNat = !Network.HavePublicAddress();
Network.InitializeServer(32,25001,useNat);
Debug.Log("Server has been Initlized");
}
}
if(GUILayout.Button("connect to server"))
{
Network.Connect("172.16.11.4",25001);
}
}
function OnConnectedToServer() {
Debug.Log("Connected to server");
Isconnected=true;
// Send local player name to server ...
}
但是这段代码不会在wifi上运行...如果我们给ip 127.0.0.1这个代码在localhost上运行
#pragma strict
private var rotAngle : float = 0;
private var pivotPoint : Vector2;
var Gamename:String="MyGame";
var refreshing:boolean=false;
private var hostdata:HostData[];
function Start () {
}
function Update () {
var farword=Input.GetKey("up");
if(farword==true)
{
//animation.Play("ArmatureAction");
}
transform.Translate(0,0,Input.GetAxis("Vertical"));
transform.Rotate(0,Input.GetAxis("Horizontal"),0);
if(refreshing)
{
if(MasterServer.PollHostList().Length>0)
{
refreshing=false;
hostdata=MasterServer.PollHostList();
Debug.Log(MasterServer.PollHostList().Length);
}
}
}
function startServer()
{
Network.InitializeServer(32,25001,!Network.HavePublicAddress);
MasterServer.RegisterHost(Gamename,"THis is tutorial","THis is tutorial game");
}
function OnServerInitialized()
{
Debug.Log("Sever is initlized");
}
function OnMasterServerEvent(mgs:MasterServerEvent)
{
if(mgs==MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Register server");
}
}
function refreshHostList(){
MasterServer.RequestHostList(Gamename);
refreshing=true;
}
function OnGUI()
{
if((!Network.isClient && !Network.isServer))
{
if(GUI.Button(Rect(100,100,100,100),"Start Sever"))
{
Debug.Log("Start Server");
startServer();
}
if(GUI.Button(Rect(100,220,100,100),"Refresg Hosts"))
{
Debug.Log("Refresh HOsts");
refreshHostList();
}
if(hostdata)
{
for(var i:int=0;i<hostdata.length;i++)
{
if(GUI.Button(Rect(230,100,100,100),hostdata[i].gameName))
{
Network.Connect("172.16.11.4",25001);
}
}
}
}
if(GUI.Button(Rect(300,300,100,100),"back"))
{
transform.Translate(9,0,0);
}
if(GUI.Button(Rect(300,400,100,100),"farword"))
{
transform.Translate(-9,0,0);
}
}
任何帮助
答案 0 :(得分:0)
我认为在第二段代码中
if(hostdata)
{
for(var i:int=0;i<hostdata.length;i++)
{
if(GUI.Button(Rect(230,100,100,100),hostdata[i].gameName))
{
Network.Connect("172.16.11.4",25001);
}
}
需要改变
if(hostdata)
{
for(var i:int=0;i<hostdata.length;i++)
{
if(GUI.Button(Rect(230,100,100,100),hostdata[i].gameName))
{
Network.Connect(hostdata[i]); //Or Network.Connect( hostdata[i].ip[ 0 ], hostdata[i].port );
}
}
而且你的按钮也会叠加。 (如果初始化并找到多个服务器)。您需要使用动态起始高度值创建Rect。像:
if(hostdata)
{
var y = 100; // or other start y position like 0
for(var i:int=0;i<hostdata.length;i++)
{
if(GUI.Button(Rect(230,y,100,100),hostdata[i].gameName))
{
Network.Connect(hostdata[i]); //Or Network.Connect( hostdata[i].ip[ 0 ], hostdata[i].port );
}
y+=100+ 5 //Some padding.
}
如果出现任何问题,如无法连接服务器请记住NAT穿通通常会导致问题。