Unity截图上传到服务器,并且产生二维码供玩家扫描下载

1.首先在服务器端配置好环境,这里使用PhpStudy进行环境的安装

2.现在开始客户端的代码制作

我们现在要做软件是:截图>上传到服务器>返回二维码

《放一下服务器接收文件    点击下载》

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

public class Test : MonoBehaviour {

    string url  = "http://XXXXXXXXXX/UpLoad/UnityUpload.php";//服务器地址

    public Button SaveBtn;
    public Button QRcode;

    public RawImage m_raw;

    string uploadName;
    int i;

    string path;
    string downloadName;

   Texture2D m_downloadTex;
  
    // Use this for initialization
    void Start () {
        
        SaveBtn.onClick.AddListener(delegate() {

            StartCoroutine(UploadScreen());
        });
        QRcode.onClick.AddListener(delegate ()
        {
            StartCoroutine(Qrcodse());
        });
	}
	
	// Update is called once per frame
	void Update () {

    }
    public Texture2D Screenshot()//截屏
    {

        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
        tex.Apply();

        return tex;

    }
   IEnumerator UploadScreen()//上传图片
    {

        yield return new WaitForEndOfFrame();

       

        byte[] bytes = Screenshot().EncodeToPNG();
        
        WWWForm form = new WWWForm();
        form.AddField("Name", UploadNameStr());
        form.AddBinaryData("post", bytes);
        WWW www = new WWW(url, form);
        StartCoroutine(PostData(www));



        System.IO.File.WriteAllBytes(path, bytes);
        
    }

    public string UploadNameStr()//
    {
        path = Application.dataPath + "/" + i.ToString() + ".jpg";
        uploadName = i.ToString();
      
        downloadName = "https://cli.im/api/qrcode/code?text=http:///XXXXXXXXXX/UpLoad/upload/" + i.ToString() + ".png&mhid=5EqSDQzsmc0hMHcsKNZTMao";//将服务器地址通过草料API转换成为二维码

        i++;
        if (i > 3)
            i = 0;
        Debug.Log(uploadName);
        return uploadName;
    }
    IEnumerator PostData(WWW www)//接受服务器返回的信息
    {
        yield return www;
        Debug.Log(www.text);
    }

      IEnumerator    Qrcodse()
    {
       
        WWW w = new WWW(downloadName);

        yield return w;

        print(w.text);

        //获取'src=" //' 后所有的数据
        string s = w.text.Substring(w.text.IndexOf("

 

你可能感兴趣的:(unity)