|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ImageInfo
|
|
|
|
|
{
|
|
|
|
|
public int type;
|
|
|
|
|
public Vector2 pos=Vector2.zero;
|
|
|
|
|
public float width;
|
|
|
|
|
public float height;
|
|
|
|
|
}
|
|
|
|
|
public class GetImage : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("待截图的目标摄像机")]
|
|
|
|
|
public Camera mainCam;
|
|
|
|
|
[Header("是否开始录像")]
|
|
|
|
|
public bool isStartRecord=false;
|
|
|
|
|
public string ImgPath;
|
|
|
|
|
public string TxtPath;
|
|
|
|
|
RenderTexture rt; //声明一个截图时候用的中间变量
|
|
|
|
|
Texture2D t2d;
|
|
|
|
|
int num = 0; //截图计数
|
|
|
|
|
[Header("服务器地址")]
|
|
|
|
|
public string ServerPath;
|
|
|
|
|
[Header("播放图片集的RenderTex")]
|
|
|
|
|
public Image imgPlayer;
|
|
|
|
|
private List<byte[]> imgList = new List<byte[]>();
|
|
|
|
|
|
|
|
|
|
public Transform TestObj;
|
|
|
|
|
|
|
|
|
|
//public GameObject pl; //一个调试用的板子
|
|
|
|
|
|
|
|
|
|
int width = 512;
|
|
|
|
|
int height = 512;
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
t2d = new Texture2D(width, height, TextureFormat.RGB24, false);
|
|
|
|
|
rt = new RenderTexture(width, height,24);
|
|
|
|
|
mainCam.targetTexture = rt;
|
|
|
|
|
|
|
|
|
|
//Debug.Log((Vector2)mainCam.WorldToScreenPoint(TestObj.position));
|
|
|
|
|
|
|
|
|
|
//float x = mainCam.WorldToViewportPoint(TestObj.position).x;
|
|
|
|
|
|
|
|
|
|
//float y = mainCam.WorldToViewportPoint(TestObj.position).y;
|
|
|
|
|
//Debug.Log(x+"-"+y);
|
|
|
|
|
//Debug.Log(mainCam.WorldToScreenPoint(TestObj.position).normalized.x);
|
|
|
|
|
//Debug.Log(mainCam.WorldToScreenPoint(TestObj.position).normalized.y);
|
|
|
|
|
//Vector2 view = mainCam.WorldToViewportPoint(TestObj.position) * new Vector2(1920, 1080);
|
|
|
|
|
|
|
|
|
|
//Debug.Log(view.normalized);
|
|
|
|
|
//Debug.Log(Input.mousePosition);
|
|
|
|
|
//Debug.Log((Vector2)mainCam.WorldToViewportPoint(TestObj.position));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//截图
|
|
|
|
|
if (isStartRecord)
|
|
|
|
|
{
|
|
|
|
|
if (Time.frameCount % 2 == 0)
|
|
|
|
|
{
|
|
|
|
|
CreatImg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//CreatImgAndText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreatImgAndText()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
List<Car> cars = DriveManager.Instance.carList;
|
|
|
|
|
|
|
|
|
|
List<ImageInfo> infos = new List<ImageInfo>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < cars.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
//检测是否在屏幕内
|
|
|
|
|
if (IsInScreen(mainCam, cars[i].gameObject.transform.position))
|
|
|
|
|
{
|
|
|
|
|
ImageInfo info = new ImageInfo();
|
|
|
|
|
Vector3 viewPos = mainCam.WorldToViewportPoint(cars[i].gameObject.transform.position);
|
|
|
|
|
//Vector3 viewPos = mainCam.WorldToScreenPoint(cars[i].gameObject.transform.position);
|
|
|
|
|
float x = viewPos.x;
|
|
|
|
|
float y = 1-viewPos.y;
|
|
|
|
|
|
|
|
|
|
info.pos.x=x;
|
|
|
|
|
info.pos.y = y;
|
|
|
|
|
Debug.Log(x + "-" + y);
|
|
|
|
|
info.type = cars[i].CarType;
|
|
|
|
|
//计算框
|
|
|
|
|
float distance = Vector3.Distance(mainCam.transform.position, cars[i].transform.position);
|
|
|
|
|
float maxDistance = 100;
|
|
|
|
|
distance = Mathf.Clamp(distance, 15, maxDistance);
|
|
|
|
|
float rate = Mathf.SmoothStep(0, 1, 1 - (distance / maxDistance));
|
|
|
|
|
if (rate <= 0.04f)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//float h = (1 - (viewPos.y * 1080 / 1080)) * 240/1080;
|
|
|
|
|
//float w= (1 - (viewPos.y * 1080 / 1080)) * 195/1920;
|
|
|
|
|
float h = rate * 240 / 1080;
|
|
|
|
|
float w = rate * 195 / 1920;
|
|
|
|
|
info.width = w;
|
|
|
|
|
info.height = h;
|
|
|
|
|
|
|
|
|
|
//添加到图片信息队列
|
|
|
|
|
infos.Add(info);
|
|
|
|
|
|
|
|
|
|
#region 画线测试
|
|
|
|
|
// Debug.Log(info.type + "-" + info.pos.x + "-" + info.pos.y + "-" + info.width + "-" + info.height);
|
|
|
|
|
//画线,测试用
|
|
|
|
|
//DrawTool.mMouseStart = new Vector2((viewPos.x-w)*Screen.width,(viewPos.y-h)*Screen.height);
|
|
|
|
|
//DrawTool.mMouseEnd = new Vector2((viewPos.x + w) * Screen.width, (viewPos.y + h) * Screen.height);
|
|
|
|
|
//DrawTool.mBDrawMouseRect = true;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreatImg(infos.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsInScreen(Camera camera, Vector3 targetPos)
|
|
|
|
|
{
|
|
|
|
|
Vector3 viewPos = camera.WorldToViewportPoint(targetPos);
|
|
|
|
|
Vector3 dir = (camera.transform.position - viewPos).normalized;
|
|
|
|
|
float dot = Vector3.Dot(camera.transform.forward, dir);
|
|
|
|
|
return dot > 0 && viewPos.x > 0 && viewPos.x < 1 && viewPos.y > 0 && viewPos.y < 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void CreatImg()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//截图到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);
|
|
|
|
|
|
|
|
|
|
//string imageFilePath = "D:/UnityProject/ChuanGanQi" + "/ " + ImgPath + "/";
|
|
|
|
|
// if (!Directory.Exists(imageFilePath))
|
|
|
|
|
// {
|
|
|
|
|
// Directory.CreateDirectory(imageFilePath);
|
|
|
|
|
// }
|
|
|
|
|
//File.WriteAllBytes(imageFilePath + num.ToString() + ".jpg", byt);
|
|
|
|
|
|
|
|
|
|
////如有服务器地址上传服务器
|
|
|
|
|
//if (ServerPath != "")
|
|
|
|
|
//{
|
|
|
|
|
// StartCoroutine(UnLoadImage(byt));
|
|
|
|
|
//}
|
|
|
|
|
Debug.Log("当前截图序号为:" + num.ToString());
|
|
|
|
|
|
|
|
|
|
num++;
|
|
|
|
|
}
|
|
|
|
|
public void CreatImg(ImageInfo[] info)
|
|
|
|
|
{
|
|
|
|
|
//截图到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);
|
|
|
|
|
string imageFilePath = "D:/UnityProject" + "/ " + ImgPath + "/";
|
|
|
|
|
if (!Directory.Exists(imageFilePath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(imageFilePath);
|
|
|
|
|
}
|
|
|
|
|
File.WriteAllBytes(imageFilePath + num.ToString() + ".jpg", byt);
|
|
|
|
|
//如有服务器地址上传服务器
|
|
|
|
|
if (ServerPath != "")
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(UnLoadImage(byt));
|
|
|
|
|
}
|
|
|
|
|
Debug.Log("当前截图序号为:" + num.ToString());
|
|
|
|
|
//添加信息
|
|
|
|
|
string txtFilePath = "D:/UnityProject" + "/ " + TxtPath + "/";
|
|
|
|
|
if (!Directory.Exists(txtFilePath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(txtFilePath);
|
|
|
|
|
}
|
|
|
|
|
// StreamWriter sr =new StreamWriter(txtFilePath + num.ToString() + ".txt");
|
|
|
|
|
//注入文本信息
|
|
|
|
|
string text = "";
|
|
|
|
|
List<string> strList = new List<string>();
|
|
|
|
|
for (int i = 0; i < info.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string content = info[i].type + " " + info[i].pos.x.ToString("0.000000") + " " + info[i].pos.y.ToString("0.000000") + " " + info[i].width.ToString("0.000000") + " " + info[i].height.ToString("0.000000");
|
|
|
|
|
|
|
|
|
|
if (strList.Contains(content))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
strList.Add(content);
|
|
|
|
|
text += content+'\n';
|
|
|
|
|
}
|
|
|
|
|
File.WriteAllText(txtFilePath + num.ToString() + ".txt", text);
|
|
|
|
|
//sr.WriteLine(text);
|
|
|
|
|
//sr.Close();
|
|
|
|
|
//sr.Dispose();
|
|
|
|
|
|
|
|
|
|
num++;
|
|
|
|
|
if (num == 10000)
|
|
|
|
|
{
|
|
|
|
|
isStartRecord = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <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()
|
|
|
|
|
{
|
|
|
|
|
Time.timeScale = 0.5f;
|
|
|
|
|
//float width = imgPlayer.rectTransform.rect.width;
|
|
|
|
|
//float height = imgPlayer.rectTransform.rect.height;
|
|
|
|
|
float width = 1024;
|
|
|
|
|
float height = 1024;
|
|
|
|
|
Texture2D texture = new Texture2D((int)width, (int)height);
|
|
|
|
|
for (int i = 0; i < imgList.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] bytes = imgList[i];//资源
|
|
|
|
|
|
|
|
|
|
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(); //一定要清理游离资源。
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Time.timeScale = 1f;
|
|
|
|
|
//PlayImgsAction();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|