Esempio n. 1
0
    private IEnumerator EncodeTooltipPNG(bool asClipboard = false)
    {
        // Set up res change
        bool resChanged = ResolutionSetup();

        // Set-up scene, turn off unneeded canvases
        tooltipCanvas.SetActive(true);
        tooltipSetupEvent.Invoke();

        // We should only read the screen buffer after rendering is complete
        yield return(new WaitForEndOfFrame());

        // Destroy existing image
        if (texTooltip != null)
        {
            Destroy(texTooltip);
        }

        // Read Source Template
        float   canvasLocalScale = rootCanvas.transform.localScale.x;
        Vector2 sourceScreen     = tooltipSource.position;
        Vector2 sourceSizeScreen = tooltipSource.rect.size * canvasLocalScale;

        // Create a texture the size of the screen, RGB24 format
        int width  = (int)(sourceSizeScreen.x);
        int height = (int)(sourceSizeScreen.y);

        texTooltip = tooltipTransparent.value ?
                     new Texture2D(width, height, TextureFormat.ARGB32, false, true) :
                     new Texture2D(width, height, TextureFormat.ARGB32, false, true);

        // Read screen contents into the texture
        texTooltip.ReadPixels(new Rect(sourceScreen.x, sourceScreen.y, sourceSizeScreen.x, sourceSizeScreen.y), 0, 0);
        texTooltip.Apply();

        // fix tooltip transparency
        if (tooltipTransparent.value == false)
        {
            for (int y = 15; y < texTooltip.height - 15; y++)
            {
                for (int x = 15; x < texTooltip.width - 15; x++)
                {
                    Color color = new Color(texTooltip.GetPixel(x, y).r, texTooltip.GetPixel(x, y).g, texTooltip.GetPixel(x, y).b, 1);
                    texTooltip.SetPixel(x, y, color);
                }
            }
            texTooltip.Apply();
        }

        // Return Res
        if (resChanged)
        {
            yield return(new WaitForEndOfFrame());

            ResolutionReturn();
        }

        // Reset Canvases
        tooltipCanvas.SetActive(false);
        tooltipFinishEvent.Invoke();

        // Copy into clipboard and return if stated
        if (asClipboard)
        {
            MemoryStreamToClipboard(texTooltip);
            yield break;
        }

        // Encode texture into PNG
        byte[] bytes = texTooltip.EncodeToPNG();

        // Get Active KW Data
        CustomKeywordData kw = KWData[KWTabIndex.value];

        // DETERMINE NAME
        string fileName = "tooltip_download";

        if (!string.IsNullOrEmpty(kw.label))
        {
            fileName = "tooltip_" + kw.label;
        }

        fileName = FixFileName(fileName);

        // Platform dependent file saving
#if UNITY_EDITOR
        SaveDialog(fileName, bytes);
        yield break;
#endif

#if UNITY_STANDALONE
        SaveDialog(fileName, bytes);
        yield break;
#endif

#if UNITY_WEBGL
        DownloadScreenshot(bytes, fileName + ".png");
        yield break;
#endif
    }