Unity3d中使用UGUI和 FairyGUI屏幕截图

一、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);
发布了69 篇原创文章 · 获赞 10 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/mhtqq809201/article/details/102463064
今日推荐