我用unity想將一張圖片放到hierarchy後,分割為W*H數量的【物件】,
在https://www.jianshu.com/p/dba6a03d5213 看到需求的功能,
只是他寫得很明白,我看得很糊塗,弄不懂這些程式碼要怎麼個組合...反正沒反應就是了
請問~下面這些該怎麼修改
public class Cut : MonoBehaviour
{
//切圖
//小图的宽和高
public int m_iMinWidth;
public int m_iMinHeight;
//一行有多少个小图
public int m_iMinPicRowCount;
//一列有多少个小图
public int m_iMinPicColumnCount;
//需要切割的大图
public Texture2D m_texPlayer;
//小图数组
private Texture2D[,] m_texPlayers;
//核心方法 分割大图,核心思想就是在大图上去像素点赋值给小图
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;
}
// Start is called before the first frame update
void Start()
{
//初始化行数列数
m_iMinPicRowCount = 22;
m_iMinPicColumnCount = 12;
//根据行数和列数构造储存小图的数组
m_texPlayers = new Texture2D[m_iMinPicRowCount, m_iMinPicRowCount];
m_texPlayer = duplicateTexture(m_texPlayer);
for (int i = 0; i < m_iMinPicRowCount; i++)
{
for (int j = 0; j < m_iMinPicColumnCount; j++)
{
DePackTexture(i, j);
}
}
}
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];
}
}
}
// Update is called once per frame
void Update()
{
}