コード例 #1
0
    public static IEnumerator SaveTexture(Texture2D _tex, string _name)
    {
        float ratio = ( float )_tex.width / _tex.height;
        int   width = 0, height = 0;

        // 如果比例大于阈值,则使用Fix width方案
        // 反之使用Fix Height方案
        if (ratio > FIX_HEIGHT_THRESHOLD)
        {
            width  = FIX_WIDTH;
            height = Mathf.RoundToInt(width / ratio);
        }
        else
        {
            height = FIX_HEIGHT;
            width  = Mathf.RoundToInt(height * ratio);

            // 如果使用Fix Height方案后,宽度还是超标
            // 则再转位Fix Width方案
            if (width > FIX_WIDTH)
            {
                width  = FIX_WIDTH;
                height = Mathf.RoundToInt(width / ratio);
            }
        }

        Debug.Log("Saved Texture Sizes: " + width + ", " + height);

        _tex = TextureScaler.ResizeTexture(_tex, width, height);

        byte[] bytes = _tex.EncodeToPNG();

        string path = TGPaths.FullScreenshotPath(_name);

        Debug.Log("截图路径: " + path);

        // 写入文件
        LMFileWriter.Write(path, bytes);

        // 写到Dicionary里,好之后写入ret.txt
        TGData.SaveScreenshot(_name);

        yield return(null);
    }