Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# $\u POST和WWWForm与bool.ToString()不匹配';行不通_C#_Php_Unity3d - Fatal编程技术网

C# $\u POST和WWWForm与bool.ToString()不匹配';行不通

C# $\u POST和WWWForm与bool.ToString()不匹配';行不通,c#,php,unity3d,C#,Php,Unity3d,我一直在做一个定制服务器(可能不会工作,但我想尝试一下,并把它作为一种爱好),但我已经遇到了一个问题!我的php脚本将命令输出为文本,然后使用unity将其转换为c#命令 以下是ServerCalls.cs: using UnityEngine; using UnityEngine.UI; using System.Collections; using System; using System.Linq; public class ServerCallsScript : MonoBehavio

我一直在做一个定制服务器(可能不会工作,但我想尝试一下,并把它作为一种爱好),但我已经遇到了一个问题!我的php脚本将命令输出为文本,然后使用unity将其转换为c#命令

以下是ServerCalls.cs:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Linq;

public class ServerCallsScript : MonoBehaviour {

    private bool isPlayerInRoom = false;

    public bool autoConnectToLobby;
    public bool showLobbyGUI;

    public float width;
    public float height;
    public float x;
    public float y;

    public enum logTypeEnum {
        Developer,
        Debug,
        Warning,
        Error
    };
    public logTypeEnum logType = logTypeEnum.Warning;

    public string MainServerUrl = "https://network-hlapi-myusername.c9.io/serverMain.php";

    private string serverText = "";

    void Start()
    {
        StartCoroutine(ServerCommands());
    }
    void Update()
    {

    }
    void OnGUI()
    {
        if (showLobbyGUI)
        {
            if(GUI.Button(new Rect(5,80,115,21), "Create Game"))
            {

            }
            GUI.TextField(new Rect(130, 80, 115, 21), "Game1");
            if(GUI.Button(new Rect(5, 110, 115, 21), "Join Game"))
            {

            }
            GUI.TextField(new Rect(130, 110, 115, 21),"Game Name");
        }
    }



    /// <summary>
    /// This is called as soon as the player opens the scene
    /// </summary>
    public void OnConnectedToServer()
    {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Successfully Connected To Server at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }
    /// <summary>
    /// Called When Player Connects To Lobby. Automatically called if auto-join lobby is enabled
    /// </summary>
    public void OnConnectedToLobby() {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Connected To Lobby at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }
    public void ShowLobbyGui() {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Showing Lobby Gui");
        }

    }
    /// <summary>
    /// Called when the player joins a game
    /// </summary>
    public void OnPlayerJoinedRoom()
    {
        if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
        {
            Debug.Log("Player Successfully Joined Room at " + DateTime.Now.ToString("hh:mm:ss"));
        }
    }



    IEnumerator ServerCommands() {
        Debug.Log("Collecting Info from Server...");
        WWWForm Mainform = new WWWForm();
        Mainform.AddField("isAutoJoinLobby", autoConnectToLobby.ToString());
        Mainform.AddField("isShowingLobbyGui", showLobbyGUI.ToString());
        Mainform.AddField("hasPlayerJoinedRoom", isPlayerInRoom.ToString());

        WWW mainServer = new WWW(MainServerUrl, Mainform);
        yield return mainServer;


        serverText = mainServer.text;
        string[] serverCommands = serverText.Split('\n');
        if(mainServer.error != null)
        {
            Debug.LogError("We encountered an error! Error:" + mainServer.error);
        }
        if (serverCommands.Contains("OnConnectedToServer"))
        {
            OnConnectedToServer();
        }
        if (serverCommands.Contains("OnConnectedToLobby"))
        {
            Debug.Log("OnConnectedToLobby");
            OnConnectedToLobby();
        }
        if (serverCommands.Contains("showLobbyGui"))
        {
            ShowLobbyGui();
        }
        if (serverCommands.Contains("OnPlayerJoinedRoom"))
        {
            OnPlayerJoinedRoom();
        }

    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
使用制度;
使用System.Linq;
公共类ServerCallsScript:MonoBehavior{
private bool isPlayerInRoom=假;
公共布尔自动连接到OBBY;
公共场所展示游说团;
公共浮动宽度;
公众浮标高度;
公共浮动x;
公众浮躁;
公共枚举日志类型枚举{
开发商
调试,
警告
错误
};
公共logTypeEnum logType=logTypeEnum.Warning;
公共字符串MainServerUrl=”https://network-hlapi-myusername.c9.io/serverMain.php";
私有字符串serverText=“”;
void Start()
{
start例程(ServerCommands());
}
无效更新()
{
}
void OnGUI()
{
if(showLobbyGUI)
{
if(GUI.按钮(新矩形(5,80115,21),“创建游戏”))
{
}
TextField(新的Rect(130,80,115,21),“Game1”);
if(GUI.按钮(新Rect(5111011521),“加入游戏”))
{
}
TextField(新的Rect(13011011521),“游戏名”);
}
}
/// 
///一旦播放器打开场景,就会调用该函数
/// 
连接到服务器()上的公共无效
{
if(logType==logTypeEnum.Debug | | logType==logTypeEnum.Developer)
{
Log(“在”+DateTime.Now.ToString(“hh:mm:ss”)成功连接到服务器);
}
}
/// 
///当玩家连接到大厅时调用。如果启用自动加入大厅,则自动调用
/// 
连接到Tolobby()的公共空间{
if(logType==logTypeEnum.Debug | | logType==logTypeEnum.Developer)
{
Log(“在”+DateTime.Now.ToString(“hh:mm:ss”)连接到大厅);
}
}
public void ShowLobbyGui(){
if(logType==logTypeEnum.Debug | | logType==logTypeEnum.Developer)
{
Log(“显示大厅Gui”);
}
}
/// 
///当玩家加入游戏时调用
/// 
公共无效OnPlayerJoinedRoom()
{
if(logType==logTypeEnum.Debug | | logType==logTypeEnum.Developer)
{
Log(“玩家在”+DateTime.Now.ToString(“hh:mm:ss”)成功加入了房间);
}
}
IEnumerator服务器命令(){
Log(“从服务器收集信息…”);
WWWForm Mainform=新WWWForm();
AddField(“isAutoJoinLobby”,autoConnectToLobby.ToString());
AddField(“isShowingLobbyGui”,showLobbyGUI.ToString());
AddField(“hasPlayerJoinedRoom”,isplayerroom.ToString());
WWW mainServer=新的WWW(MainServerUrl,Mainform);
返回主服务器;
serverText=mainServer.text;
字符串[]serverCommands=serverText.Split('\n');
if(mainServer.error!=null)
{
LogError(“我们遇到了一个错误!错误:“+mainServer.error”);
}
if(serverCommands.Contains(“OnConnectedToServer”))
{
OnConnectedToServer();
}
if(serverCommands.Contains(“OnConnectedToLobby”))
{
Log(“OnConnectedToLobby”);
OnConnectedToLobby();
}
if(serverCommands.Contains(“showLobbyGui”))
{
ShowLobbyGui();
}
if(serverCommands.Contains(“OnPlayerJoinedRoom”))
{
OnPlayerJoinedRoom();
}
}
}
对于ServerMain.php:

<?php
    $servername = getenv('IP');
    $username = getenv('C9_USER');
    $password = "I Wouldn't Tell You That Now";
    $database = "server";
    $dbport = 3306;

    // Create connection
    $db = mysql_connect($servername, $username, $password, $dbport)or die("Cant Connect to server");
    mysql_select_db($database) or die("Cant connect to database");

    $isAutoConnectToLobby = $_POST['isAutoJoinLobby'];
    $isShowingLobbyGui = $_POST['isShowingLobbyGui'];
    $hasPlayerJoinedRoom = $_POST['hasPlayerJoinedRoom'];
    $ClientClickConnectButton = $_POST['ClientClickConnectButton'];

    echo "OnConnectedToServer\n";
    if ($isAutoConnectToLobby == true){
        echo "OnConnectedToLobby\n";
    }
    if($isShowingLobbyGui == true){
        echo "showLobbyGui\n";
    }
    if($ClientClickConnectButton == true){
        sleep(5);      //Just to give some time to complete any in-complete operations
        echo "onClientClickConnectButton\n";
    }
    if($hasPlayerJoinedRoom == true){
        echo "OnPlayerJoinedRoom\n";
    }

?>


问题是ServerMain.php脚本似乎没有侦听任何WWWForm字段。如果在php脚本中向If语句添加引号,它只会遍历所有If语句,但如果不添加引号,则不会执行任何If语句,即使启用了公共布尔值,也不会使用isset()首先测试该值是否可用;-)

另外,请不要回答“你的代码真的很乱!清理干净!”或“你不需要这个!它没有用!”。我知道我的代码目前非常混乱,可能需要一些清理,但我想在睡觉前把这个问题弄清楚,这样我可能明天就能得到答案。你能测试一下这些值是否使用isset设置吗?不看你的代码-你试过Charles Proxy吗?然后,您可以确切地看到发送到服务器的内容。@AdamJeffers更改了$\u POST命令,该命令应返回true到isset()变量,但效果良好。现在我只在unity中的变量为true时发送命令。把你的评论记为答案,我会记为正确的