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;
}
}