unity 分割大图成若干个小图
2019-07-25 本文已影响0人
雾妖夜战先锋
最近碰到一个功能,需要将一张大图切割成若干个小图在拼成大图。
首先定义一些基础变量
//小图的宽和高
public int m_iMinWidth;
public int m_iMinHeight;
//一行有多少个小图
public int m_iMinPicRowCount;
//一列有多少个小图
public int m_iMinPicColumnCount;
//需要切割的大图
public Texture2D m_texPlayer;
//小图数组
private Texture2D[,] m_texPlayers;
初始化一些变量
//初始化行数列数
m_iMinPicRowCount = 22;
m_iMinPicColumnCount = 12;
//根据行数和列数构造储存小图的数组
m_texPlayers = new Texture2D[m_iMinPicRowCount, m_iMinPicRowCount];
核心方法 分割大图,核心思想就是在大图上去像素点赋值给小图
//切图
void DePackTexture(int i, int j)
{
//通过大图宽高计算出每次切图的大小
m_iMinHeight = m_texPlayer.height / m_iMinPicColumnCount;
m_iMinWidth = m_texPlayer.width / m_iMinPicRowCount;
int cur_x = i * m_iMinWidth;
int cur_y = j * m_iMinHeight;
Texture2D newTexture = new Texture2D(m_iMinWidth, m_iMinHeight);
for (int m = cur_y; m < cur_y + m_iMinHeight; m++)
{
for (int n = cur_x; n < cur_x + m_iMinWidth; n++)
{
//核心 通过从大图拿到的像素点赋值给小图
newTexture.SetPixel(n - cur_x, m - cur_y, m_texPlayer.GetPixel(n, m));
}
}
//Apply一下 更新贴图
newTexture.Apply();
//储存到数组
m_texPlayers[i, j] = newTexture;
}
有时候会碰到说大图不能读的情况,这是Unity为了节省性能默认的为不可读了,可以在设置中更改,也可以使用下面这个方法:
private Texture2D duplicateTexture(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
调用下这个方法
m_texPlayer = duplicateTexture(m_texPlayer);
根据行列数分割大图
//切割图片
for (int i = 0; i < m_iMinPicRowCount; i++)
{
for (int j = 0; j < m_iMinPicColumnCount; j++)
{
DePackTexture(i, j);
}
}
最后创建下Quad看下效果
//创建Quad
void CreatQuad()
{
GameObject go = Resources.Load<GameObject>("Quad");
for (int i = 0; i < m_iMinPicRowCount; i++)
{
for (int j = 0; j < m_iMinPicColumnCount; j++)
{
GameObject g = Instantiate(go, new Vector3(i, j, 0), Quaternion.identity);
//将材质赋给对应的Quad
g.gameObject.GetComponent<MeshRenderer>().material.mainTexture = m_texPlayers[i, j];
}
}
}
效果展示
image.png
但是这样会有一个问题,就是图片之间有很明显的缝隙感,这是由于创建的Texture2D大小并不能完美贴合所创建出来的Quad。
所以我们需要修改wrapMode,代码如下:
newTexture.wrapMode = TextureWrapMode.Clamp;
运行效果如下:
image.png
是不是就很完美了。