protected Vector2 GetRenderSize(ScreenshotResolution resolution, float zoom, PreviewConfigAsset.GalleryDisplayMode mode) { int displayWidth, displayHeight; int width = resolution.ComputeTargetWidth(); int height = resolution.ComputeTargetHeight(); if (resolution.m_Texture != null) { width = resolution.m_Texture.width; height = resolution.m_Texture.height; } if (width <= 0 || height <= 0) { EditorGUILayout.LabelField("Invalid dimensions for resolution " + resolution.ToString()); return(Vector2.zero); } // Compute the box dimensions depending on the display mode if (mode == PreviewConfigAsset.GalleryDisplayMode.RATIOS) { displayWidth = (int)((m_WindowWidth - m_ConfigAsset.m_MarginHorizontal) * zoom); displayHeight = (int)(displayWidth * height / width); } else if (mode == PreviewConfigAsset.GalleryDisplayMode.PIXELS || resolution.m_PPI <= 0) { displayWidth = (int)(width * zoom / PixelsPerPoint()); displayHeight = (int)(height * zoom / PixelsPerPoint()); } else { displayWidth = (int)(width * zoom / resolution.m_PPI * m_ConfigAsset.m_ScreenPPI / PixelsPerPoint()); displayHeight = (int)(height * zoom / resolution.m_PPI * m_ConfigAsset.m_ScreenPPI / PixelsPerPoint()); } // Invert the scaling displayWidth = (int)((float)displayWidth / resolution.m_Scale); displayHeight = (int)((float)displayHeight / resolution.m_Scale); return(new Vector2(displayWidth, displayHeight)); }
protected IEnumerator CaptureMasks(ScreenshotResolution res) { if (m_ConfigAsset.m_DeviceRendererCamera == null) { yield break; } // Init camera m_Camera = GameObject.Instantiate(m_ConfigAsset.m_DeviceRendererCamera); m_Camera.clearFlags = CameraClearFlags.Color; m_Camera.backgroundColor = EditorGUIUtility.isProSkin ? new Color32(56, 56, 56, (byte)(m_ConfigAsset.m_TransparentDeviceBackground ? 0 : 255)) : new Color32(194, 194, 194, (byte)(m_ConfigAsset.m_TransparentDeviceBackground ? 0 : 255)); // Init canvas instance m_Canvas = res.m_DeviceCanvas; if (m_Canvas == null) { m_Canvas = m_ConfigAsset.m_DefaultDeviceCanvas; } if (m_Canvas == null) { yield break; } m_Canvas = GameObject.Instantiate(m_Canvas); // Texture MaskRenderer mask = m_Canvas.GetComponent <MaskRenderer>(); mask.m_Texture.texture = res.m_Texture; if (m_ConfigAsset.m_DrawingMode == PreviewConfigAsset.DrawingMode.SCREEN_MASK) { mask.m_Mask.rectTransform.anchoredPosition = Vector2.zero; mask.m_Device.gameObject.SetActive(false); if (mask.m_BorderTop) { mask.m_BorderTop.gameObject.SetActive(false); mask.m_BorderLeft.gameObject.SetActive(false); mask.m_BorderRight.gameObject.SetActive(false); mask.m_BorderBottom.gameObject.SetActive(false); } } // Scale the UI Vector3 panelScale = Vector3.one; int widthWithDevice = res.ComputeTargetWidth(); int heightWithDevice = res.ComputeTargetHeight(); if (res.m_Orientation == ScreenshotResolution.Orientation.PORTRAIT) { // Resize the whole canvas panelScale.x = (float)res.ComputeTargetWidth() / (float)mask.m_Mask.GetComponent <RectTransform>().rect.width; panelScale.y = (float)res.ComputeTargetHeight() / (float)mask.m_Mask.GetComponent <RectTransform>().rect.height; mask.m_Panel.localScale = panelScale; // Add the border margin if (m_ConfigAsset.m_DrawingMode == PreviewConfigAsset.DrawingMode.FULL_DEVICE) { widthWithDevice = (int)(mask.m_Device.GetComponent <RectTransform>().rect.width *panelScale.x); heightWithDevice = (int)(mask.m_Device.GetComponent <RectTransform>().rect.height *panelScale.y); } } else { // Rotate the whole canvas mask.m_Panel.localRotation = Quaternion.AngleAxis(90f, Vector3.forward); // Resize the whole canvas panelScale.x = (float)res.ComputeTargetWidth() / (float)mask.m_Mask.GetComponent <RectTransform>().rect.height; panelScale.y = (float)res.ComputeTargetHeight() / (float)mask.m_Mask.GetComponent <RectTransform>().rect.width; mask.m_Panel.localScale = new Vector3(panelScale.y, panelScale.x, 1f); // Rotate the texture and scale it to match the screen size mask.m_Texture.transform.localRotation = Quaternion.AngleAxis(-90f, Vector3.forward); float tmp = mask.m_Texture.rectTransform.sizeDelta.x; mask.m_Texture.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, mask.m_Texture.rectTransform.sizeDelta.y); mask.m_Texture.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tmp); // Add the border margin if (m_ConfigAsset.m_DrawingMode == PreviewConfigAsset.DrawingMode.FULL_DEVICE) { widthWithDevice = (int)(mask.m_Device.GetComponent <RectTransform>().rect.height *panelScale.x); heightWithDevice = (int)(mask.m_Device.GetComponent <RectTransform>().rect.width *panelScale.y); } } // Compute target resolution if (m_ConfigAsset.m_DrawingMode == PreviewConfigAsset.DrawingMode.SCREEN_MASK) { mask.m_Device.gameObject.SetActive(false); } else { mask.m_Device.gameObject.SetActive(true); } // Capture the device Texture2D texClone = new Texture2D(1, 1); yield return(m_ScreenshotTaker.StartCoroutine(m_ScreenshotTaker.CaptureToTextureCoroutine(texClone, widthWithDevice, heightWithDevice, new List <Camera> { m_Camera }, new List <Canvas> { m_Canvas }, ScreenshotTaker.CaptureMode.GAMEVIEW_RESIZING, 8, false, ScreenshotTaker.ColorFormat.RGBA, false))); // Replace the texture GameObject.DestroyImmediate(res.m_Texture); res.m_Texture = texClone; // Clean GameObject.DestroyImmediate(m_Camera.gameObject); GameObject.DestroyImmediate(m_Canvas.gameObject); }