使用bool.ToString()的$ _POST和WWWForm不起作用

时间:2015-10-26 21:10:00

标签: c# php unity3d

我一直致力于制作自定义服务器(可能不会工作,但我想尝试一下并将其作为一种爱好)但我已经遇到了问题!我有什么PHP脚本输出命令作为文本,然后统一将其转换为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();
        }

    }
}

对于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语句,即使启用了公共bool也是如此

1 个答案:

答案 0 :(得分:0)

使用isset()首先测试该值是否可用; - )