132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using RenderHeads.Media.AVProMovieCapture;
|
|
public class ScreenCaptureManager : MonoBehaviour
|
|
{
|
|
public static ScreenCaptureManager instance;
|
|
public CaptureBase m_Capture;
|
|
public CaptureFromScreen captureFromScreen;
|
|
private string m_FileNamePrefix;
|
|
public string m_FilePath;
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
CaptureInit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置视频格式
|
|
/// </summary>
|
|
public void CaptureInit()
|
|
{
|
|
m_Capture = GameObject.Find("ScreenCapture").GetComponent<CaptureBase>();
|
|
captureFromScreen = GameObject.Find("ScreenCapture").GetComponent<CaptureFromScreen>();
|
|
m_Capture._useMediaFoundationH264 = true;
|
|
m_Capture._autoFilenameExtension = "mp4";
|
|
string[] strList = File.ReadAllLines(Application.streamingAssetsPath + "/VideoPlayBackConfig.ini");
|
|
if (strList.Length != 0)
|
|
{
|
|
m_Capture._downScale = (CaptureBase.DownScale)Enum.Parse(typeof(CaptureBase.DownScale), strList[1].Split('=')[1].Trim());
|
|
string str = strList[3].Split('=')[1];
|
|
int x = int.Parse(str.Split('*')[0]);
|
|
int y = int.Parse(str.Split('*')[1]);
|
|
m_Capture._maxVideoSize = new Vector2(x, y);
|
|
m_Capture._frameRate = CaptureBase.FrameRate.Ten;
|
|
}
|
|
|
|
}
|
|
|
|
public void StartCapture(string path, string videoMame)
|
|
{
|
|
m_Capture._outputFolderPath = path;
|
|
m_Capture._autoFilenamePrefix = videoMame;
|
|
if (!m_Capture.IsCapturing())
|
|
{
|
|
m_FilePath = path;//m_Capture.LastFilePath;
|
|
m_Capture.StartCapture();
|
|
}
|
|
}
|
|
|
|
public void PauseCapture()
|
|
{
|
|
m_Capture.PauseCapture();
|
|
}
|
|
|
|
public void ResumeCapture()
|
|
{
|
|
m_Capture.ResumeCapture();
|
|
}
|
|
|
|
public void StopCapture()
|
|
{
|
|
if (m_Capture != null && m_Capture.IsCapturing())
|
|
{
|
|
m_Capture.StopCapture();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置输出视频前缀
|
|
/// </summary>
|
|
private void FilenamePrefix(string traintype, string shiptype, string subjectname, string user)
|
|
{
|
|
m_FileNamePrefix = string.Format("${0}${1}${2}${3}$", traintype, shiptype, subjectname, user);
|
|
//m_Capture._autoFilenamePrefix = string.Format("${0}${1}${2}${3}$", traintype,shiptype,subjectname, user);
|
|
m_Capture._autoFilenamePrefix = m_FileNamePrefix;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 输出文件路径
|
|
/// </summary>
|
|
private void FolderPath()
|
|
{
|
|
string date = string.Format("{0}_{1}_{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
|
|
m_Capture._outputFolderPath = Path.Combine("ScreenCaptures", date);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
StopCapture();
|
|
}
|
|
|
|
|
|
[ContextMenu("Start ScreenShoot")]
|
|
private void StartScreenShoot()
|
|
{
|
|
string[] str = m_FilePath.Split('\\');
|
|
string fileName = str[str.Length - 1].Split('.')[0];
|
|
string date = string.Format("{0}_{1}_{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
|
|
string projectFolder = Path.GetFullPath(Path.Combine(Application.dataPath, "..", "PrintScreens", date));
|
|
if (!Directory.Exists(projectFolder))
|
|
Directory.CreateDirectory(projectFolder);
|
|
StartCoroutine(ScreenShoot(Path.Combine(projectFolder, fileName + ".png")));
|
|
}
|
|
|
|
private IEnumerator ScreenShoot(string filePath)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
|
|
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
|
|
Camera ca = GameObject.FindGameObjectWithTag("ThirdCamera").GetComponent<Camera>();
|
|
ca.targetTexture = rt;
|
|
ca.Render();
|
|
ca.targetTexture = null;
|
|
RenderTexture.active = rt;
|
|
screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
|
ca.targetTexture = null;
|
|
RenderTexture.active = null;
|
|
Destroy(rt);
|
|
yield return 0;
|
|
byte[] bytes = screenShot.EncodeToPNG();
|
|
File.WriteAllBytes(filePath, bytes);
|
|
}
|
|
}
|