38 lines
1.1 KiB
C#
Raw Normal View History

2025-04-21 16:04:59 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScreenShot : MonoBehaviour
{
public Texture2D screenImage;
public void Shot(Action<string> callBack)
{
//if (MVC.GetModel<KunChongModel>().IsKaoHe)
//{
// StartCoroutine(ScrrenCapture(callBack));
//}
}
IEnumerator ScrrenCapture(Action<string> callBack)
{
yield return new WaitForEndOfFrame();
// 创建一个新的Texture2D对象尺寸为屏幕当前分辨率
screenImage = new Texture2D(Screen.width, Screen.height);
// 读取屏幕上当前帧的像素
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
//生成Base64
string base64 = "data:image/png;base64," + Texture2DToBase64(screenImage);
// 清理
callBack?.Invoke(base64);
}
public string Texture2DToBase64(Texture2D texture)
{
byte[] data = texture.EncodeToPNG();
return Convert.ToBase64String(data);
}
}