我已经编写了两个程序,我希望这两个程序都在我的 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);
}
}
任何帮助