美术那边会出一张海报图,尺寸为1280x720,包装成预设,预设上还有叠加一些其他UI,比如头像、昵称、二维码、宣传语、logo等等。
如果把海报图铺满全屏,那么如果手机屏幕宽高比不是1280:720,那么海报图就会被拉伸变形,如果等比例缩放海报图,那海报图就会有部分超出屏幕外,无法被截屏到。
解决办法:
海报图示例化后不缩放不拉伸,如果是在1280:720的宽高比的手机上,就刚好铺满全屏,如果是其他宽高比的屏幕,则海报图四周有可能会有留空,没有铺满全屏。没关系,截屏的时候,只读取海报图所在的像素位置即可,这里需要进行一些换算,具体看下面代码的region 2的部分的逻辑
public void cutScreen()
{
RenderTexture rt = new RenderTexture(Screen.width,Screen.height,24);
// 赋值各个摄像机的targetTexture,注意摄像机的渲染顺序
// m_mainCam、m_auxCam、m_nguiCam是我这边定义的三个摄像机
m_mainCam.targetTexture = rt;
m_mainCam.Render();
m_auxCam.targetTexture = rt;
m_auxCam.Render();
m_nguiCam.targetTexture = rt;
m_nguiCam.Render();
RenderTexture.active = rt;
#region 1
// 全屏读取像素
//Texture2D tex = new Texture2D(Screen.width,TextureFormat.RGB24,false);
//tex.ReadPixels(new Rect(0,Screen.width,Screen.height),0);
#endretion 1
#region 2
// 读取1280x720宽高比的像素,适用于1280x720宽高比的海报截屏分享
float factor_x = Screen.width * 1.0f / Screen.height/ (1280 * 1.0f / 720);
factor_x = factor_x < 1.0f ? 1.0f : factor_x;
float factor_y = 1280 * 1.0f / 720 / (Screen.width * 1.0f / Screen.height);
factor_y = factor_y < 1.0f ? 1.0f : factor_y;
Texture2D tex = new Texture2D((int)(width / factor_x),(int)(height / factor_y),false);
tex.ReadPixels(new Rect((width - width / factor_x) / 2,(height - height / factor_y) / 2f,width / factor_x,height / factor_y),0);
#endregion 2
tex.Apply();
RenderTexture.active = null;
m_mainCam.targetTexture = null;
m_auxCam.targetTexture = null;
m_nguiCam.targetTexture = null;
Destroy(rt);
GameObject photo = new GameObject("photo");
UITexture uiTex = photo.AddComponent<UITexture>();
uiTex .mainTexture = tex;
photo.transform.parent = m_gamePanel.transform;
photo.transform.localPosition = new Vector3();
photo.transform.localScale = Vector3.one;
uiTex.width = 611;
uiTex.height = 344;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。