C# 如何将数据库中的多个图像显示到unity中

C# 如何将数据库中的多个图像显示到unity中,c#,php,mysql,unity3d,C#,Php,Mysql,Unity3d,我的数据库中有4个图像,我想统一显示这4个图像。我所做的是在php中使用base64_encode函数在浏览器中将我的图像显示为字符串,然后unity将该字符串转换为图像,但问题是它将所有4个图像的字符串都视为1,并且只显示第一个图像 我曾尝试在php中使用分隔字符串,但unity无法理解换行,我不知道如何分隔它们。一旦它们分开,我就可以制作一个字符串数组并将它们保存在其中 <?php $servername = "localhost"; $username = "root

我的数据库中有4个图像,我想统一显示这4个图像。我所做的是在php中使用base64_encode函数在浏览器中将我的图像显示为字符串,然后unity将该字符串转换为图像,但问题是它将所有4个图像的字符串都视为1,并且只显示第一个图像

我曾尝试在php中使用
分隔字符串,但unity无法理解换行,我不知道如何分隔它们。一旦它们分开,我就可以制作一个字符串数组并将它们保存在其中

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

    //Make connection
    $conn = new mysqli($servername, $username, $password, $dbName);
    //Check connection
    if(!$conn)
    {
        die("Connection failed.". mysqli_connect_error());
    }
    //
    //else echo("Connection success") . "<br>";;

    $sql = "SELECT * FROM images";
    $result = mysqli_query($conn, $sql);

    if(mysqli_num_rows($result) > 0)
    {
        //show data for each row
        while($row = mysqli_fetch_assoc($result))
        {
            //echo '<img src="data:image/jpeg;base64,'.base64_encode($row['pics']).'"/>';
            echo "".base64_encode($row['pics']);
        }
    }
?>
结果应该是4个单独的图像,但它只显示一个


这里的问题是,您的代码将依次输出所有四个图像,而Unity中的代码将获得完整的输出并将其显示为一个图像

您可以将数据输出为数组,然后统一解析并分别创建每个图像,而不是像现在这样在一个大数据块中输出所有图像

要在PHP中创建json,可以执行以下操作:

// Create an array in which we will save the images and then encode as json
$images = [];

while($row = mysqli_fetch_assoc($result))
{
    // Add the image to the array
    $images[] = base64_encode($row['pics']);
}

// Let the client know what type of content we're returning
header('Content-Type: application/json');

// Encode the array as json and output it
echo json_encode($images);

// Exit the script to make sure that we don't get any unwanted output after the json
exit;

这将输出一个json数组。您需要查看如何在Unity中解析json(我知道有对它的支持),只需在每次迭代时遍历数组并创建输出图像。

这里的问题是,您的代码将依次输出所有四个图像,而Unity中的代码将获得完整的输出并将其显示为一个图像

您可以将数据输出为数组,然后统一解析并分别创建每个图像,而不是像现在这样在一个大数据块中输出所有图像

要在PHP中创建json,可以执行以下操作:

// Create an array in which we will save the images and then encode as json
$images = [];

while($row = mysqli_fetch_assoc($result))
{
    // Add the image to the array
    $images[] = base64_encode($row['pics']);
}

// Let the client know what type of content we're returning
header('Content-Type: application/json');

// Encode the array as json and output it
echo json_encode($images);

// Exit the script to make sure that we don't get any unwanted output after the json
exit;

这将输出一个json数组。您需要查看如何在Unity中解析json(我知道有对它的支持),只需迭代数组并在每次迭代时创建输出图像。

为什么您对这行echo注释“返回包含四个图像的json数组并在Unity中单独使用,或者创建四个不同的端点,每个图像一个。我最大的问题是,为什么一开始就将图像存储在mysql中?只需将图像路径存储在数据库中,然后从文件系统加载图像,就像处理网站上的任何图像一样。如果需要缩放,则将图像存储在CDN上。@VinayPatil-因为它们希望以统一的形式显示图像,而不是显示在网页上。好的,那么您必须将这些图像返回一个数组,并在实用程序端,你必须一封接一封地处理每封电子邮件。为什么你评论这行echo“要么返回一个包含四个图像的json数组,并单独使用它们,要么创建四个不同的端点,每个图像一个端点。我最大的问题是,为什么一开始就将图像存储在mysql中?只需将图像路径存储在数据库中,然后从文件系统加载图像,就像处理网站上的任何图像一样。如果需要缩放,则将图像存储在CDN上。@VinayPatil-因为它们希望以统一的形式显示图像,而不是显示在网页上。好的,然后您必须将这些图像返回一个数组,在实用程序端,您必须逐个处理每封电子邮件。