|
|
@@ -10,6 +10,7 @@ using System;
|
|
|
using System.IO;
|
|
|
using UnityEngine.Events;
|
|
|
using ToneTuneToolkit.Common;
|
|
|
+using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -20,31 +21,13 @@ namespace ToneTuneToolkit.Media
|
|
|
/// </summary>
|
|
|
public class ScreenshotMaster : SingletonMaster<ScreenshotMaster>
|
|
|
{
|
|
|
- public static UnityAction<Texture2D> OnScreenshotFinished;
|
|
|
+ public static UnityAction<Texture2D, int> OnScreenshotFinished;
|
|
|
|
|
|
[Header("DEBUG - Peek")]
|
|
|
[SerializeField] private Texture2D peekTexture;
|
|
|
|
|
|
// ==================================================
|
|
|
|
|
|
- // private void Update()
|
|
|
- // {
|
|
|
- // if (Input.GetKeyDown(KeyCode.Q))
|
|
|
- // {
|
|
|
- // SaveTest();
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- // public RectTransform Area;//用来取景的ui,设置为透明的
|
|
|
-
|
|
|
- // public void SaveTest()
|
|
|
- // {
|
|
|
- // string fullPath = $"{Application.streamingAssetsPath}/IMAGE/{SpawnTimeStamp()}.png";
|
|
|
- // TakeScreenshot(Area, fullPath, CanvasType.ScreenSpaceOverlay);
|
|
|
- // }
|
|
|
-
|
|
|
- // ==================================================
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 传入用于标定范围的Image
|
|
|
/// 独立功能
|
|
|
@@ -52,19 +35,15 @@ namespace ToneTuneToolkit.Media
|
|
|
/// <param name="screenshotArea">标定范围</param>
|
|
|
/// <param name="fullFilePath">保存路径</param>
|
|
|
/// <param name="canvasType">截图类型</param>
|
|
|
- public void TakeScreenshot(RectTransform screenshotArea, string fullFilePath, CanvasType canvasType)
|
|
|
- {
|
|
|
- StartCoroutine(TakeScreenshotAction(screenshotArea, fullFilePath, canvasType));
|
|
|
- return;
|
|
|
- }
|
|
|
- private IEnumerator TakeScreenshotAction(RectTransform screenshotArea, string fullFilePath, CanvasType canvasType)
|
|
|
+ public void TakeScreenshot(RectTransform screenshotArea, CanvasType canvasType, int flag = 0, string fullFilePath = null) => StartCoroutine(TakeScreenshotAction(screenshotArea, canvasType, flag, fullFilePath));
|
|
|
+ private IEnumerator TakeScreenshotAction(RectTransform screenshotArea, CanvasType canvasType, int flag = 0, string fullFilePath = null)
|
|
|
{
|
|
|
yield return new WaitForEndOfFrame(); // 等待渲染帧结束
|
|
|
|
|
|
int width = (int)screenshotArea.rect.width;
|
|
|
int height = (int)screenshotArea.rect.height;
|
|
|
|
|
|
- Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA64, false);
|
|
|
+ Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
|
|
|
|
|
|
// 原点
|
|
|
float leftBottomX = 0;
|
|
|
@@ -77,30 +56,29 @@ namespace ToneTuneToolkit.Media
|
|
|
leftBottomX = screenshotArea.transform.position.x + screenshotArea.rect.xMin;
|
|
|
leftBottomY = screenshotArea.transform.position.y + screenshotArea.rect.yMin;
|
|
|
break;
|
|
|
- case CanvasType.ScreenSpaceCamera: // 如果是camera需要额外加上偏移值
|
|
|
-
|
|
|
- // leftBottomX = Screen.width / 2;
|
|
|
- // leftBottomY = Screen.height / 2;
|
|
|
- // 相机画幅如果是1920x1080,设置透视、Size540可让UI缩放为111
|
|
|
-
|
|
|
- // Debug.Log(Screen.width / 2 + "/" + Screen.height / 2);
|
|
|
+ case CanvasType.ScreenSpaceCamera: // 如果是camera需要额外加上偏移值 // 相机画幅如果是1920x1080,设置透视、Size=540可让UI缩放为111
|
|
|
+ leftBottomX = screenshotArea.transform.position.x + (Screen.width / 2 + screenshotArea.rect.xMin);
|
|
|
+ leftBottomY = screenshotArea.transform.position.y + (Screen.height / 2 + screenshotArea.rect.yMin);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
texture2D.ReadPixels(new Rect(leftBottomX, leftBottomY, width, height), 0, 0);
|
|
|
texture2D.Apply();
|
|
|
|
|
|
- // 保存至本地
|
|
|
- byte[] bytes = texture2D.EncodeToPNG();
|
|
|
- File.WriteAllBytes(fullFilePath, bytes);
|
|
|
- Debug.Log($"[ScreenshotMasterLite] <color=green>{fullFilePath}</color>...[OK]");
|
|
|
- // Destroy(texture2D);
|
|
|
+ if (fullFilePath != null)
|
|
|
+ {
|
|
|
+ // 保存至本地
|
|
|
+ byte[] bytes = texture2D.EncodeToPNG();
|
|
|
+ File.WriteAllBytes(fullFilePath, bytes);
|
|
|
+ Debug.Log($"[SM] <color=green>{fullFilePath}</color>");
|
|
|
+ // Destroy(texture2D);
|
|
|
+ }
|
|
|
|
|
|
peekTexture = texture2D;
|
|
|
|
|
|
if (OnScreenshotFinished != null)
|
|
|
{
|
|
|
- OnScreenshotFinished(texture2D);
|
|
|
+ OnScreenshotFinished(texture2D, flag);
|
|
|
}
|
|
|
yield break;
|
|
|
}
|
|
|
@@ -115,7 +93,7 @@ namespace ToneTuneToolkit.Media
|
|
|
/// </summary>
|
|
|
/// <param name="screenshotCamera"></param>
|
|
|
/// <param name="screenshotRT">新建的RT宽高色彩模式都要设置妥当 // RGBA8_SRGB</param>
|
|
|
- public static Texture2D OffScreenshot(Camera screenshotCamera, RenderTexture screenshotRT, string fullFilePath)
|
|
|
+ public static Texture2D OffScreenshot(Camera screenshotCamera, RenderTexture screenshotRT, string fullFilePath = null)
|
|
|
{
|
|
|
screenshotCamera.clearFlags = CameraClearFlags.SolidColor;
|
|
|
screenshotCamera.backgroundColor = Color.clear;
|
|
|
@@ -127,9 +105,12 @@ namespace ToneTuneToolkit.Media
|
|
|
t2d.ReadPixels(new Rect(0, 0, screenshotRT.width, screenshotRT.height), 0, 0);
|
|
|
t2d.Apply();
|
|
|
|
|
|
- byte[] bytes = t2d.EncodeToPNG();
|
|
|
- File.WriteAllBytes(fullFilePath, bytes);
|
|
|
- Debug.Log(@$"[SM] <color=green>{fullFilePath}</color>");
|
|
|
+ if (fullFilePath != null)
|
|
|
+ {
|
|
|
+ byte[] bytes = t2d.EncodeToPNG();
|
|
|
+ File.WriteAllBytes(fullFilePath, bytes);
|
|
|
+ Debug.Log(@$"[SM] <color=green>{fullFilePath}</color>");
|
|
|
+ }
|
|
|
|
|
|
RenderTexture.active = null;
|
|
|
screenshotRT.Release();
|
|
|
@@ -139,7 +120,7 @@ namespace ToneTuneToolkit.Media
|
|
|
#endregion
|
|
|
// ==================================================
|
|
|
#region 实验性功能
|
|
|
- public Texture2D InstantTakeScreenshot(Camera renderCamera, string fullFilePath)
|
|
|
+ public Texture2D InstantTakeScreenshot(Camera renderCamera, string fullFilePath = null)
|
|
|
{
|
|
|
// 创建一个RenderTexture
|
|
|
RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
|
|
|
@@ -156,10 +137,13 @@ namespace ToneTuneToolkit.Media
|
|
|
texture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
|
|
texture2D.Apply();
|
|
|
|
|
|
- // 保存至本地
|
|
|
- byte[] bytes = texture2D.EncodeToPNG();
|
|
|
- File.WriteAllBytes(fullFilePath, bytes);
|
|
|
- Debug.Log($"[ScreenshotMasterLite] <color=green>{fullFilePath}</color>...[OK]");
|
|
|
+ if (fullFilePath != null)
|
|
|
+ {
|
|
|
+ // 保存至本地
|
|
|
+ byte[] bytes = texture2D.EncodeToPNG();
|
|
|
+ File.WriteAllBytes(fullFilePath, bytes);
|
|
|
+ Debug.Log($"[SM] <color=green>{fullFilePath}</color>");
|
|
|
+ }
|
|
|
|
|
|
peekTexture = texture2D;
|
|
|
|
|
|
@@ -171,6 +155,22 @@ namespace ToneTuneToolkit.Media
|
|
|
return texture2D;
|
|
|
}
|
|
|
|
|
|
+ public Texture2D TakeScreenshot2T2d(Camera screenshotCamera, RectTransform screenshotArea)
|
|
|
+ {
|
|
|
+ Vector2 size = Vector2.Scale(screenshotArea.rect.size, screenshotArea.lossyScale);
|
|
|
+ RenderTexture rt = new RenderTexture((int)size.x, (int)size.y, 24);
|
|
|
+
|
|
|
+ screenshotCamera.targetTexture = rt;
|
|
|
+ screenshotCamera.Render();
|
|
|
+
|
|
|
+ Texture2D t2d = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
|
|
|
+ Graphics.CopyTexture(rt, t2d);
|
|
|
+
|
|
|
+ screenshotCamera.targetTexture = null;
|
|
|
+
|
|
|
+ return t2d;
|
|
|
+ }
|
|
|
+
|
|
|
#endregion
|
|
|
// ==================================================
|
|
|
#region Tools
|