应用场景
- Sprite精灵不需要完全显示贴图,仅需要在屏幕显示区域大小的贴图生成的精灵。
- 动态加载一张贴图,同样也是仅需要显示Image或RawImage在屏幕区域大小的。(隐私问题,不再过多赘述)
代码
- 参数输入
Init(float x, float y, Texture2D targetTexture)
- 计算裁剪部分起始坐标和宽高
(1) 首先需要计算出贴图超出屏幕区域的偏移量值 (2) 贴图是以左下角为(0,0)作为起点,RecTransform.anchoredPosition的x和y属性,与偏移量的差,获得裁剪的起始坐标(注意:当RawImage对象,左下角完全在屏幕左下方以外的区域,起点位置既是坐标与偏移量绝对值的和;若左下角完全在屏幕范围内,则差值计算结果小于0,即当差值小于0时,x或y轴是完全在屏幕范围的,需要校准) (3)裁剪宽高最大值既是屏幕的宽高,当RawImage的左下角完全在屏幕范围内,获取的起始坐标既是RawImage左下角到屏幕左下角的距离,做差值计算出裁剪的宽高;若RawImage左下角在屏幕区域外时,起点坐标左边和下边的区域需要裁剪掉,通过贴图宽高与起始坐标作差,但是需要注意最大值是屏幕的宽高。总的来说,RawImage的x和y坐标,要么在屏幕范围内,要么在屏幕范围外面。
private void CalculateTextureRange(int x, int y)
{
m_StartPosX = m_OffsetX - x;
m_StartPosY = m_OffsetY - y;
m_Width = m_ScreenWidth - Mathf.Abs(m_StartPosX);
m_Height = m_ScreenHeight - Mathf.Abs(m_StartPosY);
if (m_StartPosX <= 0)
{
m_Width = m_ScreenWidth - Mathf.Abs(m_StartPosX);
m_StartPosX = 0;
}
else
{
m_Width = m_TargetTexture.width - Mathf.Abs(m_StartPosX);
m_Width = m_Width > m_ScreenWidth ? m_ScreenWidth : m_Width;
}
if (m_StartPosY <= 0)
{
m_Height = m_ScreenHeight - Mathf.Abs(m_StartPosY);
m_StartPosY = 0;
}
else
{
m_Height = m_TargetTexture.height - Mathf.Abs(m_StartPosY);
m_Height = m_Height > m_ScreenHeight ? m_ScreenHeight : m_Height;
}
m_Width = m_Width > m_TargetTexture.width ? m_TargetTexture.width : m_Width;
m_Height = m_Height > m_TargetTexture.height ? m_TargetTexture.height : m_Height;
}
- 贴图裁剪
计算出贴图裁剪的其实坐标和宽高后,需要对贴图进行裁剪操作 (1)方法一:返回值是Sprite精灵(显示效果是裁剪后的贴图,其实Sprite.Texture的贴图并没有被裁剪)
private Texture2D GetTextureResizer()
{
if (m_Width <= 0 || m_Height <= 0)
return null;
Sprite sprite = Sprite.Create(m_TargetTexture, new Rect(new Vector2(m_StartPosX, m_StartPosY),
new Vector2(m_Width, m_Height)), new Vector2(0.5f, 0.5f));
}
(2)方法二:操作像素,返回值是Texture对象(逐像素优化:尽量不要使用for循环嵌套的方式获取像素点的Color对象,效率很低。相同大小贴图块,GetPixels用时40-50ms,逐像素650-700ms)
private Texture2D GetTextureResizer()
{
if (m_Width <= 0 || m_Height <= 0)
return null;
Texture2D resultTexture = new Texture2D(m_Width, m_Height, TextureFormat.RGBA4444, true);
m_Colors = m_TargetTexture.GetPixels(m_StartPosX, m_StartPosY, m_Width, m_Height);
resultTexture.SetPixels(m_Colors);
resultTexture.Apply();
m_TargetTexture = null;
return resultTexture;
}
|