0

我已经编写了两个程序,我希望这两个程序都在我的 android 设备上运行,它们在同一个网络上,但一个是简单的客户端服务器程序,它将在两个设备上运行并建立连接......但是 Unity Master Server 将运行不正常,无法连接同一网络上的两个 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);
}

}

任何帮助

4

1 回答 1

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 ); 
  } 
} 

而且你的按钮也会覆盖自己。(如果超过 1 个服务器初始化并找到)。您需要创建具有动态起始高度值的 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 穿透通常会导致问题。

于 2015-01-20T13:43:05.107 回答