You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.3 KiB
128 lines
3.3 KiB
using UnityEngine;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.UI;
|
|
|
|
public class GetImage : MonoBehaviour
|
|
{
|
|
|
|
public Camera mainCam; //待截图的目标摄像机
|
|
public string path;
|
|
RenderTexture rt; //声明一个截图时候用的中间变量
|
|
Texture2D t2d;
|
|
int num = 0; //截图计数
|
|
[Header("服务器地址")]
|
|
public string ServerPath;
|
|
[Header("播放图片集的RenderTex")]
|
|
public Image imgPlayer;
|
|
private List<byte[]> imgList = new List<byte[]>();
|
|
|
|
|
|
//public GameObject pl; //一个调试用的板子
|
|
|
|
|
|
|
|
void Start()
|
|
{
|
|
t2d = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
|
|
rt = new RenderTexture(1920, 1080, 24);
|
|
mainCam.targetTexture = rt;
|
|
|
|
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
//按下空格键来截图
|
|
if (Input.GetKey(KeyCode.Space))
|
|
{
|
|
//将目标摄像机的图像显示到一个板子上
|
|
//pl.GetComponent<Renderer>().material.mainTexture = rt;
|
|
|
|
//截图到t2d中
|
|
RenderTexture.active = rt;
|
|
t2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
|
|
t2d.Apply();
|
|
RenderTexture.active = null;
|
|
|
|
//将图片保存起来
|
|
byte[] byt = t2d.EncodeToJPG();
|
|
imgList.Add(byt);
|
|
// File.WriteAllBytes(Application.dataPath + "/" +path+"/"+ num.ToString() + ".jpg", byt);
|
|
if (ServerPath != "")
|
|
{
|
|
StartCoroutine(UnLoadImage(byt));
|
|
}
|
|
|
|
|
|
Debug.Log("当前截图序号为:" + num.ToString());
|
|
num++;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上传到服务器
|
|
/// </summary>
|
|
/// <param name="path0"></param>
|
|
/// <returns></returns>
|
|
IEnumerator UnLoadImage(byte[] data)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
try
|
|
{
|
|
byte[] bytes = data;
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("Name", num);
|
|
form.AddBinaryData("post", bytes);
|
|
WWW www = new WWW(ServerPath, form);
|
|
|
|
StartCoroutine(PostData(www));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.Log("出现异常:" + e);
|
|
}
|
|
//Destroy(tex);
|
|
}
|
|
/// <summary>
|
|
/// 返回服务器信息
|
|
/// </summary>
|
|
/// <param name="www"></param>
|
|
/// <returns></returns>
|
|
IEnumerator PostData(WWW www)
|
|
{
|
|
yield return www;
|
|
Debug.Log(www.text);
|
|
}
|
|
|
|
public void PlayImgsAction()
|
|
{
|
|
if (imgPlayer != null)
|
|
{
|
|
StartCoroutine(PlayImg());
|
|
}
|
|
}
|
|
|
|
IEnumerator PlayImg()
|
|
{
|
|
for (int i = 0; i < imgList.Count; i++)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
int width = 1920;
|
|
int height = 1080;
|
|
byte[] bytes = imgList[i];//资源
|
|
Texture2D texture = new Texture2D(width, height);
|
|
texture.LoadImage(bytes);
|
|
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
|
|
imgPlayer.sprite = sprite;
|
|
Resources.UnloadUnusedAssets(); //一定要清理游离资源。
|
|
|
|
}
|
|
}
|
|
|
|
} |