首先是简单的登陆
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoginUser : MonoBehaviour {
public InputField Username;
public InputField Password;
public GameObject Tip;
void Update () {
}
public void ButtonEvent()
{
if (Username.text == "")
{
Tip.SetActive(true);
Tip.GetComponentInChildren<Text>().text = "请输入用户名";
Invoke("FalseTip", 3f);
}
else if (Password.text == "")
{
Tip.SetActive(true);
Tip.GetComponentInChildren<Text>().text = "请输入密码";
Invoke("FalseTip", 3f);
}
else
{
StartCoroutine(Userlogin(Username.text, Password.text));
}
}
IEnumerator Userlogin(string username,string password)
{
WWWForm form = new WWWForm();
form.AddField("UserName", username);
form.AddField("Password", password);
WWW www = new WWW(IP地址, form);
yield return www;
if (www.error == null)
{
if (!www.isDone)
{
Debug.Log(www.uploadProgress);
}
else
{
if(www.text!="0")
{
//登录成功
MessageConst.username = username;
SceneManager.LoadScene("SampleScene");
}
else
{
//登录失败
Tip.SetActive(true);
Tip.GetComponentInChildren<Text>().text = "用户名或密码错误";
Invoke("FalseTip", 3f);
}
}
}
else
{
Debug.Log(www.error);
}
}
void FalseTip()
{
Tip.SetActive(false);
}
}
关于webtexture网上的博文很多了 以下几点是少见的我写以下
旋转图片函数 顺时针90度
public Texture2D rotate(Texture2D t,int width,int height)
{
newTexture = new Texture2D(height, width); //实例化一个新的texture,高度是原来的宽度,宽度是原来的高度
for (int i = 0; i < width - 1; i++)
{
for (int j = 0; j < height - 1; j++)
{
Color color = t.GetPixel(i,j);
newTexture.SetPixel(j, width - 1 - i, color);
}
}
newTexture.Apply();
return newTexture;
}
// 录像
// 连续捕获照片 会有可能造成app内存爆掉 所以要及时释放内存 以及 。把图片保存到本地 会涉及图片大小,也有可能会把图片上传到服务器 byte[] byt = Savetexture.EncodeToJPG(95);//100 改变括号内数字即可
public IEnumerator SeriousPhotoes()
{
for (int i = 3; i > 0; i--)
{
yield return new WaitForSeconds(1f);
}
txtDaoJiShi.text = "";
f_time = Time.time;
while (true)
{
if (Time.time - f_time < i_Max_Miao)
{
//Texture2D t = new Texture2D(Screen.width, Screen.height-200, TextureFormat.RGB24, true);
//t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height - 200), 0, 0, false);//Screen.width / 2 - 200
//距X左的距离 距Y屏上的距离
t = new Texture2D(webCamera.width, webCamera.height, TextureFormat.RGB24, true);
t.SetPixels(webCamera.GetPixels());
//t.ReadPixels(new Rect(0, 0, Screen.width, (int)(Screen.width * aspect)), 0, 0, false);
t.Apply();
SaveTextureToLoacl(rotate(t, t.width, t.height)) ;
//yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(i_Miao);//此处会造成卡顿 。可用上面
}
}
}
Texture2D newTexture;
void SaveTextureToLoacl(Texture2D Savetexture)
{
byte[] byt = Savetexture.EncodeToJPG(95);//100
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
}
System.IO.File.WriteAllBytes(Path + CountId + ".jpg", byt);
CountId++;
t = null;
newTexture = null;
byt = null;
Destroy(t);
Destroy(newTexture);
GC.Collect();
//释放无用资源
Resources.UnloadUnusedAssets();
GC.Collect();
}
下面是简单的上传服务器代码
public void upLoadImage()
{
if (upload_jd == 0)
{
btnUpLoadText.text = "上传中(" + upload_jd + "/" + fileNum + ")";
for (int i = 1; i <= fileNum; i++)
{
StartCoroutine(PoseDate(i));
}
}
}
IEnumerator PoseDate(int i_name)
{
WWWForm form = new WWWForm();
FileStream fs = File.OpenRead(Path + i_name + ".jpg");
int len = (int)fs.Length;
byte[] b = new byte[len];
fs.Read(b, 0, len);
form.AddField("name", i_name.ToString());
form.AddBinaryData("imgdata", b,"screenshot", "image/jpg");
WWW www = new WWW("x?id="+MessageConst.Line+"IP地址"+MessageConst.Count, form);
yield return www;
if (www.error == null)
{
if (!www.isDone)
{
Debug.Log(www.uploadProgress);
}
else
{
Debug.Log("完成");
}
upload_jd++;
btnUpLoadText.text = "上传中(" + upload_jd + "/" + fileNum + ")";
if (upload_jd>=fileNum)
{
btnUpLoadText.text = "上传完成";
}
}
else
{
Debug.Log(www.error);
}
}