Unity播放器位置加载错误

时间:2018-07-04 13:17:27

标签: c# php unity3d

首先,我不确定这是发布此主题的合适地点,对此表示抱歉。 我正在使用XAMPP和MYSQL创建数据库,一切正常,但是当我关闭游戏并尝试加载最后一个位置时,玩家只是传送到原点。

统一的C#代码:

数据加载器脚本

using UnityEngine;
using System.Collections;

public class DataLoader : MonoBehaviour {
    public int playerxd;
    public int playeryd;
    public int playerzd;
    public string[] pos;
    IEnumerator Start(){

        WWW posData = new WWW("http://localhost/BegoneBD/DataPos.php");
        yield return posData;
        string posDataString = posData.text;
        print (posDataString);
        pos = posDataString.Split(';');
        print(GetDataValue(pos[0], "x:"));
        print(GetDataValue(pos[0], "y:"));
        print(GetDataValue(pos[0], "z:"));
    }

    string GetDataValue(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerxd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue2(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playeryd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue3(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerzd = System.Convert.ToInt32(value);
        return value;
    }

}

保存脚本

public class Save : MonoBehaviour {
    public string[] pos;
    bool teste;
    float playerx;
    float playery;
    float playerz;
    int inputplayerx;
    int inputplayery;
    int inputplayerz;
    string CreateUserUrl = "http://localhost/BegoneBD/InsertPos.php";
    public void SavePosition()
    {
        playerx = transform.position.x;
        playery = transform.position.y;
        playerz = transform.position.z;
        inputplayerx = (int)playerx;
        inputplayery = (int)playery;
        inputplayerz = (int)playerz;
        CreatePos(inputplayerx, inputplayery, inputplayerz);
    }
    public void CreatePos(int playerx1, int playery1, int playerz1)
    {
        WWWForm form = new WWWForm();
        form.AddField("playerxpost", playerx1);
        form.AddField("playerypost", playery1);
        form.AddField("playerzpost", playerz1);
        WWW www = new WWW(CreateUserUrl, form);
    }
    public void LoadPosition()
    {
        DataLoader f = new DataLoader();
        f.playerxd = (int)playerx;
        f.playeryd = (int)playery;
        f.playerzd = (int)playerz;
        transform.position = new Vector3(playerx, playery, playerz);
    }
    public void CursorSaveOn()
    {
            Cursor.visible = true;

    }
    public void CursorSaveOff()
    {
        Cursor.visible = false;

    }
}

PHP与数据库连接的代码: Datapos.php

<?php
$servername = "localhost";
$username =  "root";
$password = "";
$dbName = "begonebd";

//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
    die("Connection Failed. ". mysqli_connect_error());
}

$sql = "SELECT id, x, y, z FROM playerposition";
$result = mysqli_query($conn ,$sql);


if(mysqli_num_rows($result) > 0){
    //show data for each row
    while($row = mysqli_fetch_assoc($result)){
        echo "id:".$row['id'] . "|x:".$row['x']. "|y:".$row['y']. "|z:".$row['z'] . "<br>";
    }
}

Insertpos.php

<?php
$servername = "localhost";
$username =  "root";
$password = "";
$dbName = "begonebd";
$x = "-240";
$y = "2";
$z = "-247";

$x = $_POST["playerxpost"];
$y = $_POST["playerypost"];
$z = $_POST["playerzpost"];
Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
Check Connection
if(!$conn){
    die("Connection Failed. ". mysqli_connect_error());
}
else echo("Connection Success");

$sql = "SELECT id, x, y, z FROM playerposition";
$result = mysqli_query($conn ,$sql);


$sql = "INSERT INTO playerposition (x, y, z)
        VALUES ('".$x."','".$y."','".$z."')";
$result = mysqli_query($conn ,$sql);

if(!result) echo "there was an error";
else echo "Everything ok.";

?>

预先感谢,这对我来说真的很重要,因为这是一个学校项目,如果数据库无法正常工作,我将无法进行演示。-

0 个答案:

没有答案