Esempio n. 1
0
    public IEnumerator CaptureScreenAndShare(object uiTexture)
    {
        bool toFriend = m_bToFriend;
        Rect rect;

        if (uiTexture is UIPanel)
        {
            rect = Utility.GetUIScreenRect((UIPanel)uiTexture);
        }
        else
        {
            rect = Utility.GetUIScreenRect((UIWidget)uiTexture);
        }
        yield return(new WaitForEndOfFrame());

        Texture2D cutImage = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);

        cutImage.ReadPixels(rect, 0, 0, false);
        cutImage.Apply();

        const int   MAX_WIDTH   = 1920;
        const int   MAX_HEIGHT  = (int)(MAX_WIDTH / 1.7777777f);
        const int   JPG_QUALITY = 100;
        const float Scaling     = 0.8f;
        const int   MAX_SIZE    = 1024 * 1024 * 1;
        const int   MIN_SIZE    = 1024 * 32;

        if (cutImage.width > MAX_WIDTH || cutImage.height > MAX_HEIGHT)
        {
            float x = (float)MAX_WIDTH / cutImage.width;
            float y = (float)MAX_HEIGHT / cutImage.height;
            cutImage = Utility.ScaleTexture(cutImage, Mathf.Max(x, y));
        }
        byte[] byt = cutImage.EncodeToJPG(JPG_QUALITY);

        //保存原图
        while (byt.Length > MAX_SIZE) //最大1M
        {
            //图片太大,继续缩放
            cutImage = Utility.ScaleTexture(cutImage, Scaling);
            byt      = cutImage.EncodeToJPG(JPG_QUALITY);
        }
        string imgstr = "cutimage.jpg";
        string path   = RuntimeInfo.GetLocalPath(imgstr);

        System.IO.File.WriteAllBytes(path, byt);

        /*
         * //保存缩略图
         * while (byt.Length > MIN_SIZE) //最大32 k
         * {
         *  //图片太大,继续缩放
         *  cutImage = Utility.ScaleTexture(cutImage, Scaling);
         *  byt = cutImage.EncodeToJPG(JPG_QUALITY);
         * }
         * string path = RuntimeInfo.GetLocalPath(imgstr);
         * System.IO.File.WriteAllBytes(path, byt);
         */
        NativeInterface.BeginShare(toFriend, ServerSetting.ShareTxt, path);
        yield break;
    }