dqlm80253 2015-10-26 21:09
浏览 54

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

I have been working on making a custom server (probably won't work but I want to give it a try and work on it as a hobby) but I've already run into a problem! What I have the php scripts outputing the commands as text then getting unity to translate that into the c# commands.

Here's the 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('
');
        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();
        }

    }
}

For 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
";
    if ($isAutoConnectToLobby == true){
        echo "OnConnectedToLobby
";
    }
    if($isShowingLobbyGui == true){
        echo "showLobbyGui
";
    }
    if($ClientClickConnectButton == true){
        sleep(5);      //Just to give some time to complete any in-complete operations
        echo "onClientClickConnectButton
";
    }
    if($hasPlayerJoinedRoom == true){
        echo "OnPlayerJoinedRoom
";
    }

?>

The problem is that the ServerMain.php script doesn't seem to listen to any of the WWWForm fields. If you add quotes to the if statements in the php script it just goes through all the if statements but if you don't add quotes it doesn't do any of the if statements, even if the public bools are enabled

  • 写回答

1条回答 默认 最新

  • dongpu1908 2015-10-28 11:07
    关注

    Use isset() to first test if the value is available ;-)

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择