我正试图从服务器下载图像,我会给出图像的名称,如果存在,服务器会向我发送数据这是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文件的格式),但不起作用。我在这里做错了什么?
您应该删除
echo $file . "'n";
从php脚本中,它在实际图像内容之前回显文件名,因此Unity无法识别图像格式,因此无法加载图像数据。
您也可以考虑使用readfile函数而不是
$imgEncode = file_get_contents($path, FILE_USE_INCLUDE_PATH);
echo $imgEncode;
我使用Unity3D,并使用PHP提供图像。我发现这里可能缺少一个很大的区别,那就是Content-Type标头。我知道在IE中的正常web开发中,你必须发送标题才能正常工作。这是我的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.这样的文本。。。
...
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);
}
...
希望这能有所帮助!