Синтаксис:
Используется csharp
public Canvas canvas;
public RectTransform cropArea;
Texture2D croppedTexture;
Texture2D originalTexture;
public enum ImageFormat
{
jpg,
png
}
private bool _isRecording = false;
[DllImport("__Internal")]
private static extern void DownloadFileJsLib(byte[] byteArray, int byteLength, string fileName);
public void DownloadFile(byte[] bytes, string fileName, string fileExtension)
{
if (fileName == "") fileName = "UnnamedFile";
#if UNITY_EDITOR
string path = UnityEditor.EditorUtility.SaveFilePanel("Save file...", "", fileName, fileExtension);
System.IO.File.WriteAllBytes(path, bytes);
Debug.Log("File saved: " + path);
#elif UNITY_WEBGL
DownloadFileJsLib(bytes, bytes.Length, fileName + "." + fileExtension);
#endif
}
public void GetScreenshot(ImageFormat imageFormat, int screenshotUpscale, string fileName = "")
{
if (!_isRecording) StartCoroutine(RecordUpscaledFrame(imageFormat, screenshotUpscale, fileName));
}
IEnumerator RecordUpscaledFrame(ImageFormat imageFormat, int screenshotUpscale, string fileName)
{
_isRecording = true;
cropArea.gameObject.SetActive(false);
yield return new WaitForEndOfFrame();
try
{
if (fileName == "")
{
int resWidth = Camera.main.pixelWidth * screenshotUpscale;
int resHeight = Camera.main.pixelHeight * screenshotUpscale;
string dateFormat = "yyyy-MM-dd-HH-mm-ss";
fileName = resWidth.ToString() + "x" + resHeight.ToString() + "px_" + System.DateTime.Now.ToString(dateFormat);
int width = (int)(cropArea.rect.width * canvas.scaleFactor);
int height = (int)(cropArea.rect.height * canvas.scaleFactor);
int x = (int)(cropArea.anchoredPosition.x * canvas.scaleFactor);
int y = (int)(cropArea.anchoredPosition.y * canvas.scaleFactor);
Texture2D screenShot = new Texture2D(width, height);
Texture2D originalTexture = ScreenCapture.CaptureScreenshotAsTexture(screenshotUpscale);
if (x + width <= originalTexture.width && y + height <= originalTexture.height)
{
screenShot.SetPixels(originalTexture.GetPixels(x, y, width, height));
if (imageFormat == ImageFormat.jpg) DownloadFile(screenShot.EncodeToJPG(), fileName, "jpg");
else if (imageFormat == ImageFormat.png) DownloadFile(screenShot.EncodeToPNG(), fileName, "png");
}
Destroy(screenShot);
Destroy(screenShot);
cropArea.gameObject.SetActive(true);
}
}
catch (System.Exception e)
{
Debug.Log("Original error: " + e.Message);
}
_isRecording = false;
}
}
public RectTransform cropArea;
Texture2D croppedTexture;
Texture2D originalTexture;
public enum ImageFormat
{
jpg,
png
}
private bool _isRecording = false;
[DllImport("__Internal")]
private static extern void DownloadFileJsLib(byte[] byteArray, int byteLength, string fileName);
public void DownloadFile(byte[] bytes, string fileName, string fileExtension)
{
if (fileName == "") fileName = "UnnamedFile";
#if UNITY_EDITOR
string path = UnityEditor.EditorUtility.SaveFilePanel("Save file...", "", fileName, fileExtension);
System.IO.File.WriteAllBytes(path, bytes);
Debug.Log("File saved: " + path);
#elif UNITY_WEBGL
DownloadFileJsLib(bytes, bytes.Length, fileName + "." + fileExtension);
#endif
}
public void GetScreenshot(ImageFormat imageFormat, int screenshotUpscale, string fileName = "")
{
if (!_isRecording) StartCoroutine(RecordUpscaledFrame(imageFormat, screenshotUpscale, fileName));
}
IEnumerator RecordUpscaledFrame(ImageFormat imageFormat, int screenshotUpscale, string fileName)
{
_isRecording = true;
cropArea.gameObject.SetActive(false);
yield return new WaitForEndOfFrame();
try
{
if (fileName == "")
{
int resWidth = Camera.main.pixelWidth * screenshotUpscale;
int resHeight = Camera.main.pixelHeight * screenshotUpscale;
string dateFormat = "yyyy-MM-dd-HH-mm-ss";
fileName = resWidth.ToString() + "x" + resHeight.ToString() + "px_" + System.DateTime.Now.ToString(dateFormat);
int width = (int)(cropArea.rect.width * canvas.scaleFactor);
int height = (int)(cropArea.rect.height * canvas.scaleFactor);
int x = (int)(cropArea.anchoredPosition.x * canvas.scaleFactor);
int y = (int)(cropArea.anchoredPosition.y * canvas.scaleFactor);
Texture2D screenShot = new Texture2D(width, height);
Texture2D originalTexture = ScreenCapture.CaptureScreenshotAsTexture(screenshotUpscale);
if (x + width <= originalTexture.width && y + height <= originalTexture.height)
{
screenShot.SetPixels(originalTexture.GetPixels(x, y, width, height));
if (imageFormat == ImageFormat.jpg) DownloadFile(screenShot.EncodeToJPG(), fileName, "jpg");
else if (imageFormat == ImageFormat.png) DownloadFile(screenShot.EncodeToPNG(), fileName, "png");
}
Destroy(screenShot);
Destroy(screenShot);
cropArea.gameObject.SetActive(true);
}
}
catch (System.Exception e)
{
Debug.Log("Original error: " + e.Message);
}
_isRecording = false;
}
}