Unity上传图片到服务器及服务器如何配置

                                                                 先配置服务器

 

    1.  本人使用阿里云win服务器    

       下载phpstudy并安装到服务器上

        百度云盘:https://pan.baidu.com/s/1gfLxnI0uuEdZB-et2UMsyw    密码:46lq

        安装成功后,把套件里面的 Apache.2.4.39那一行后面的启动打开,目前上传图片打开这一个就行

       Unity上传图片到服务器及服务器如何配置_第1张图片

 

  2. 再下载UpLoad

        百度云盘:https://pan.baidu.com/s/1haRht6_qG5ix3WVv2G3u_Q 密码:qycp

        然后解压完成,把这个整体文件夹拖拽到你的phpstudy根路径下面(根据自己安装phpstudy的路径来)

      如下图:

Unity上传图片到服务器及服务器如何配置_第2张图片

 

 

3.接下来就是Unity里的上传图片代码,从服务器上如何下载资源的话自己百度(很多)

         

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour 
{
    public Texture2D tx;
	void Start () 
    {
        StartCoroutine(shangchuan());
	}

    IEnumerator shangchuan()
    {  
        if (tx)
        {
            WWWForm form = new WWWForm();
            form.AddField("Name", "3");  //这个3代表上传成功后的图片名字为 3.png      这个Name是和服务器自检的,不用修改
            form.AddBinaryData("post", tx.EncodeToPNG());       //这个post是和服务器自检的,不用修改         tx.EncodeToPNG()为图片转的16进制数组(byte[])
            WWW www = new WWW("http://xxxxxxxxxx/UpLoad/UnityUpload.php", form);    //必须上传这个路径http://xxxxxxxx/UpLoad/UnityUpload.php,因为服务器里的接收在这个路径下
            yield return www;
        }
        else
            yield return null;    
    }

}

  如若上传成功的话,图片放置的位置在C:/phpstudy_pro/WWW/UpLoad/upload这个文件夹下面,可以手动点开看

  下载使用的url为  http://xxxxxxxxxxx/UpLoad/upload/xx.png

 

 

你可能感兴趣的:(Unity)