unity 根据ugui size 指定大小截图

2020-09-18 14:11发布

Unity 根据 ugui某张图片大小的尺寸截图计算,

注意点:根据ugui image尺寸计算出屏幕所占尺寸 ,需要注意 用CanvasScaler 来计算缩放数值

public CanvasScaler canvasScaler;//canvas 的 CanvasScaler 获取 referenceResolution

private IEnumerator ScreenShot(){

yield return new WaitForEndOfFrame();

float localScaleX = Screen.width / canvasScaler.referenceResolution.x;//适配;

float localScaleY = Screen.height / canvasScaler.referenceResolution.y;//适配;

float localScale = localScaleX<localscaley?localscalex:localscaley;< p="">

int width = (int)(bg.rect.width * localScale);

int height = (int)(bg.rect.height * localScale);

int beginx = (int)((Screen.width - width) * 0.5f + bg.anchoredPosition.x * localScale);

int beginy = (int)((Screen.height - height) * 0.5f + bg.anchoredPosition.y * localScale);

Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);

//Read the pixels in the Rect starting at 0,0 and ending at the screen's width and height

texture.ReadPixels (new Rect (beginx, beginy, width, height),0,0);

texture.Apply();

}

本人qq:344810449,欢迎探讨研究。

有unity,shader,小程序等需求也可以联系本人,非常乐于助人。

如果觉得还不错给博主来个小惊喜,纯属自愿,不强求:



作者:unity游侠

链接:https://blog.csdn.net/y90o08u28/article/details/103909131

来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。