一、UGUI 屏幕截圖方法
//根據screenshotRectTransform 進行截圖 他的座標點必須爲0.0 不然會有偏差
public IEnumerator CaptureScreen(Action<string> ScreenshotCall)
{
string ssname = "lastss.png";
string sspath = Path.Combine(Application.temporaryCachePath, ssname);
if (File.Exists(sspath))
{
File.Delete(sspath);
}
Texture2D texture1;
byte[] bytes;
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
Rect rect = GetScreenshotRect(null);
texture1 = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
texture1.ReadPixels(rect, 0, 0);
texture1.Apply();
yield return 0;
bytes = texture1.EncodeToPNG();
File.WriteAllBytes(sspath, bytes);
ScreenshotCall.Invoke(sspath);
}
//獲取截圖區域對應的Rect
private Rect GetScreenshotRect(RectTransform screenshotRectTransform)
{
return new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f); //直接截全屏的圖
Rect m_Rect;
float originalVillegas = 1280.00f / 720.00f;
float gap = 0.66f;
float des = Screen.width - originalVillegas * Screen.height;
m_Rect = new Rect();
Vector3[] fourCornersArray = new Vector3[4];
screenshotRectTransform.GetWorldCorners(fourCornersArray);
m_Rect.width = fourCornersArray[2].x - fourCornersArray[0].x;
m_Rect.height = fourCornersArray[2].y - fourCornersArray[0].y;
m_Rect.width = screenshotRectTransform.sizeDelta.x * (Screen.width - des) / 1000.00f * gap;
m_Rect.height = screenshotRectTransform.sizeDelta.y * Screen.height / 1000.00f * 1.05f;
m_Rect.x = Screen.width * 0.5f - m_Rect.width / 2;
m_Rect.y = Screen.height * 0.5f - m_Rect.height / 2;
return m_Rect;
}
使用協程開啓
//微信分享截屏
public void WeChatShareScreen()
{
CoroutineMgr.StartCoroutinee(CaptureScreen(ScreenFinsh));
}
//截屏完成
private void ScreenFinsh(string imagePath)
{
WeChatShareImage(imagePath, "", "", WxShareSceneType.Friend);
}
二、FairyGUI 屏幕截圖方法
/// <summary>
/// 截圖保存,目前是保存到 win 上
/// </summary>
/// <param name="aObject"></param>
/// <param name="contents"></param>
/// <param name="pngName"></param>
/// <returns></returns>
public async ETTask SaveRenderTextureToPNG(GObject aObject)
{
DisplayObject dObject = aObject.displayObject;
dObject.EnterPaintingMode(1024, null);
//紋理將在本幀渲染後才能更新,所以訪問紋理的代碼需要延遲到下一幀執行。
await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
RenderTexture rt = (RenderTexture)dObject.paintingGraphics.texture.nativeTexture;
//得到tex後,你可以使用Unity的方法保存爲圖片或者進行其他處理。具體處理略。
//處理結束後結束繪畫模式。id要和Enter方法的對應。
dObject.LeavePaintingMode(1024);
RenderTexture prev = RenderTexture.active;
RenderTexture.active = rt;
Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
byte[] bytes = png.EncodeToPNG();
string ssname = "lastss.png";
string sspath = Path.Combine(Application.temporaryCachePath, ssname);
Log.Info($"sspath----------->{sspath}");
if (File.Exists(sspath))
{
File.Delete(sspath);
}
File.WriteAllBytes(sspath, bytes);
Texture2D.DestroyImmediate(png);
png = null;
RenderTexture.active = prev;
}
使用方式
// TODO 現在是截圖
await Game.Scene.GetComponent<GlobalComponent>().SaveRenderTextureToPNG(this.fui.GObject);