75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using UnityEngine;
|
|
|
|
//public delegate void CaptureOver(Texture2D tex2D, string SavePath);
|
|
|
|
public class CameraCapture : MonoBehaviour {
|
|
/* 2018.09.13 暂时未发现调用处,就不做迁移了
|
|
private bool mStart = false;
|
|
|
|
private CaptureOver m_captureOver;
|
|
private string m_SavePath;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
m_captureOver = null;
|
|
}
|
|
|
|
public void Capture(string savePath)
|
|
{
|
|
m_SavePath = Application.persistentDataPath + "/" + savePath;
|
|
#if UNITY_EDITOR
|
|
m_SavePath = Application.dataPath + "/" + savePath;
|
|
#endif
|
|
mStart = true;
|
|
}
|
|
|
|
public void AddCaptureOver(CaptureOver captureOver)
|
|
{
|
|
m_captureOver += captureOver;
|
|
}
|
|
|
|
Texture2D screenShot;
|
|
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
|
|
{
|
|
if (mStart == false)
|
|
return;
|
|
Camera came = gameObject.GetComponent<Camera>();
|
|
if (came == null)
|
|
return;
|
|
|
|
Rect r = new Rect(0, 0, Screen.width, Screen.height);
|
|
if (came.pixelRect.x != 1 || came.pixelRect.y != 1)
|
|
r = came.pixelRect;
|
|
|
|
RenderTexture.active = sourceTexture;
|
|
screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.ARGB32, false);
|
|
|
|
screenShot.ReadPixels(r, 0, 0);
|
|
screenShot.Apply();
|
|
|
|
RenderTexture.active = null;
|
|
|
|
m_SavePath = m_SavePath.Replace("\\", "/");
|
|
int index = m_SavePath.LastIndexOf("/");
|
|
string Direct = m_SavePath.Substring(0, index);
|
|
if (System.IO.Directory.Exists(Direct))
|
|
{
|
|
byte[] bytes = screenShot.EncodeToPNG();
|
|
System.IO.File.WriteAllBytes(m_SavePath, bytes);
|
|
}
|
|
if(m_captureOver!=null)
|
|
{
|
|
m_captureOver(screenShot, m_SavePath);
|
|
}
|
|
mStart = false;
|
|
enabled = false;
|
|
Object.Destroy(this);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
}
|
|
*/
|
|
}
|