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

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

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

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

https://cli.im/api/qrcode/code?text=http:///XXXXXXXXXX/UpLoad/upload/" + i.ToString() + ".png&mhid=5EqSDQzsmc0hMHcsKNZTMao"

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

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 = ";//将服务器地址通过草料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("<img src=") + 12, w.text.Length - (w.text.IndexOf("<img src=") + 12));

        //截取src="" 内部的链接地址,不包括'//'
        string result = s.Substring(0, s.IndexOf("\""));

        print(result);
        StartCoroutine(ReturnQR(result));
    }
    IEnumerator ReturnQR(string re)//返回二维码
    {
        WWW ww = new WWW(re);
        yield return ww;
        m_downloadTex = ww.texture;
        m_raw.texture = m_downloadTex;
    }
    
}

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值