private IEnumerator TrimScreenShotAndShare() { yield return(new WaitForEndOfFrame()); Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture(); QuitShareHUD(); Debug.LogFormat("截屏图片大小[{0},{1}]", texture.width, texture.height); int shareHUDWidth = (int)((shareContainer.transform as RectTransform).rect.width / CommonData.scalerToPresetResulotion); int shareHUDHeight = (int)((shareContainer.transform as RectTransform).rect.height / CommonData.scalerToPresetResulotion); int offsetX = (int)((texture.width - shareHUDWidth) / 2); int offsetY = (int)((texture.height - shareHUDHeight) / 2); Texture2D newT2d = new Texture2D(shareHUDWidth, shareHUDHeight); for (int i = offsetX; i < texture.width - offsetX; i++) { for (int j = offsetY; j < texture.height - offsetY; j++) { Color c = texture.GetPixel(i, j); newT2d.SetPixel(i - offsetX, j - offsetY, c); } } newT2d.Apply(); Texture2D resizedT2d = ScaleTexture(newT2d, texture.width, texture.height); byte[] trimImgData = resizedT2d.EncodeToPNG(); if (!DataHandler.DirectoryExist(Application.persistentDataPath + "/tempPics")) { DataHandler.CreateDirectory(Application.persistentDataPath + "/tempPics"); } string trimImgPath = Application.persistentDataPath + "/tempPics/shareImage.png"; File.WriteAllBytes(trimImgPath, trimImgData); #if UNITY_EDITORtemp #elif UNITY_IOS || UNITY_ANDROID Share(); #endif }
/// <summary> /// 截屏并截取分享部分的图片 /// </summary> /// <returns>The screen shot and share.</returns> private IEnumerator TrimScreenShotAndShare() { yield return(new WaitForEndOfFrame()); // 截屏 Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture(); Debug.LogFormat("截屏图片大小[{0},{1}]", texture.width, texture.height); float transferScaler = 1f; // 按照分辨率来确认转换系数 if (Camera.main.pixelWidth < 1080f) { if (CommonData.HWScalerOfCurrentScreen < 1.7f) { transferScaler = CommonData.scalerToPresetH; } else { transferScaler = CommonData.scalerToPresetW; } } // 获取分享区域的大小 int shareHUDWidth = (int)((shareShotcutRect.transform as RectTransform).rect.width * transferScaler); int shareHUDHeight = (int)((shareShotcutRect.transform as RectTransform).rect.height * transferScaler); Debug.LogFormat("实际图片大小:[{0},{1}]", shareHUDWidth, shareHUDHeight); // 分享区域在y方向的偏移 int sharePlaneFixY = (int)(sharePlane.localPosition.y * transferScaler); Debug.LogFormat("3y:{0}", sharePlane.localPosition.y); // 分享区域的X方向offset和Y方向offset int offsetYFix = (int)(shareShotcutRect.localPosition.y * transferScaler); int offsetX = (texture.width - shareHUDWidth) / 2; // 实际分享区域的最小y值 int offsetYMin = (texture.height - shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY; // 实际分享区域的最大y值 int offsetYMax = (texture.height + shareHUDHeight) / 2 + offsetYFix + sharePlaneFixY; //int offsetYMin = offsetYFix; //int offsetYMax = offsetYFix + shareHUDHeight; Debug.LogFormat("实际最小y{0},最大y{1}", offsetYMin, offsetYMax); // 按照分享图片的大小创建新的空纹理 Texture2D newT2d = new Texture2D(shareHUDWidth, shareHUDHeight); // 像素处理 for (int i = offsetX; i < texture.width - offsetX; i++) { for (int j = offsetYMin; j < offsetYMax; j++) { Color c = texture.GetPixel(i, j); newT2d.SetPixel(i - offsetX, j - offsetYMin, c); } } // 纹理应用 newT2d.Apply(); // 纹理转化为jpg格式二进制数据 byte[] trimImgData = newT2d.EncodeToJPG(); // 检查临时分享文件夹是否存在,不存在创建文件夹 if (!DataHandler.DirectoryExist(Application.persistentDataPath + "/tempPics")) { DataHandler.CreateDirectory(Application.persistentDataPath + "/tempPics"); } // 临时图片保存位置 string trimImgPath = Application.persistentDataPath + "/tempPics/shareImage.jpg"; // 保存图片 File.WriteAllBytes(trimImgPath, trimImgData); // 清理工作 Destroy(texture); Destroy(newT2d); texture = null; newT2d = null; trimImgData = null; Resources.UnloadUnusedAssets(); GC.Collect(); #if UNITY_EDITOR //DataHandler.DeleteFile(Application.persistentDataPath + "/tempPics/shareImage.jpg"); QuitShareView(); #elif UNITY_IOS || UNITY_ANDROID Share(); #endif }