使用 php 将图像数据下载到 unity3D

Downloading image data to unity3D using php

我正在尝试从服务器下载图像,我给出了图像的名称,如果它存在,服务器会向我发送数据 这是 php 部分:

if ($_POST) 
 {
     if ( isset ($_POST['fileToDownload']) ) 
     {
             $file = $_POST['fileToDownload'];
             echo $file . "\n";
             $path = 'uploads/' . $file;
            if(file_exists($path)){
              $imgEncode = file_get_contents($path, FILE_USE_INCLUDE_PATH);
              echo $imgEncode;       
            }
            else{
             echo "File does not exist";
            }
     }
 }

这是我的统一代码:

public Texture2D texture ;
static string image_url = "http://localhost/aditya/download.php";

IEnumerator DownloadImage(){

        WWWForm form = new WWWForm();
        form.AddField("fileToDownload" , file + ".png");

        WWW download = new WWW(image_url, form);

        yield return download;
        // check for errors
        if (download.error == null)
        {
            texture = new Texture2D(64, 64, TextureFormat.ARGB32, false);
            download.LoadImageIntoTexture(texture);
            Debug.Log(download.text);

            download.Dispose();
            download = null;

        } else {
            Debug.Log("WWW Error: "+ download.error);
        }   

    }
void OnGUI(){

        GUI.Label( new Rect (10, 10, 80, 20), "File name:" );           
        file = GUI.TextField ( new Rect (90, 10, 100, 20), file );    
        if ( GUI.Button ( new Rect (10, 90, 100, 20) , "download image" ) ){ //just a button
            StartCoroutine(DownloadImage());
        }    
        GUI.DrawTexture(new Rect(100, 10, 500, 300), texture, ScaleMode.ScaleToFit, true, 0);

    }

图像显示为红色问号,我尝试将 TextureFormat 更改为 ARGB32(unity 表示这是 .png 文件的格式)但它不起作用。我在这里做错了什么?

我正在使用 Unity3D 并使用 PHP 提供图像,我发现您可能在这里遗漏了一个很大的区别是 Content-Type header。我知道在 IE 中的普通网络开发中你必须发送 header 才能正常工作。这是我的 CakePHP 代码:

public function doImage($id=null) {

    $doThumb=(isset($this->request->query['doThumb'])) ? (bool)$this->request->query['doThumb'] : false;
    $doMed=(isset($this->request->query['doMed'])) ? (bool)$this->request->query['doMed'] : false;
    $doLg=(isset($this->request->query['doLg'])) ? (bool)$this->request->query['doLg'] : false;

    $data=null;

    if($id==null) {
        $data = $this->GameDrawing->find('first', array('fields'=>array('MAX(GameDrawing.id) AS maxID','linedata')));
        if($data) {
            $id=$data[0]['maxID'];
        }
    }
    $this->GameDrawing->recursive=-1;
    $data = $this->GameDrawing->read(null, $id);

    header("Content-Type: image/png"); 
    if($data) {

        if($data['GameDrawing']['flagged']) {

            echo file_get_contents(WWW_ROOT . 'img' . DS . 'ss-web-flagdrawing.png');

        } else {

            //Do Thumbnail
            if($doThumb) {                

                $file = $this->GameDrawing->getThumb($id, 213, 143);

            } else if($doMed) {

                $file = $this->GameDrawing->getMed($id, 841, 566);

            } else if($doLg) {

                $file = $this->GameDrawing->getLg($id, 700, 570);

            } else {

                //Do Normal Image
                $file = $this->GameDrawing->getImage($id);
            }

            if($file) {

                header("Cache-Control: no-cache"); 
                header("Pragma: no-cache"); 
                echo file_get_contents(ROOT . DS . 'ss-upload' . DS . $file);

            } else {
                //show default image
                echo file_get_contents(WWW_ROOT . 'img' . DS . 'ss-web-nodrawing.png');
            }
        }
    } else {
        //show default image
        echo file_get_contents(WWW_ROOT . 'img' . DS . 'ss-web-nodrawing.png');
    }
    exit();
}

此代码在 PHP 端运行。

在 Unity3D C# 方面,我已经成功使用 WWWW.texture 像这样...

...

    WWW download = new WWW(image_url, form);

    yield return download;
    // check for errors
    if (download.error == null)
    {
        texture = downloaded.texture;
        download.LoadImageIntoTexture(texture);
        Debug.Log(download.text);

        download.Dispose();
        download = null;

    } else {
        Debug.Log("WWW Error: "+ download.error);
    }

... 

希望对您有所帮助!

你应该删除

echo $file . "\n";

在您的 php 脚本中,它会在实际图像内容之前回显文件名,因此 Unity 无法识别图像格式,因此无法加载图像数据。

您也可以考虑使用 readfile 函数代替

$imgEncode = file_get_contents($path, FILE_USE_INCLUDE_PATH);
echo $imgEncode;