Unity Texture2D只获取一部分

1..............

    Texture2D newTexture;
    Color[] sourceColors;
    int sourceWidth;
    int sourceHeight;
    Color[] newColors;
    int newWidth;
public void SplitImg(Texture2D t2d)
{
        // 读取源Texture
        sourceColors = t2d.GetPixels();
        sourceWidth = t2d.width;
        sourceHeight = t2d.height;

        // 创建新Texture的颜色数组
        newColors = new Color[sourceColors.Length];

        // 复制源Texture的一半到新Texture
        newWidth = sourceWidth / 2;
        for (int y = 0; y < sourceHeight; y++)
        {
            for (int x = 0; x < newWidth; x++)
            {
                int sourceIndex = y * sourceWidth + x;
                int newIndex = y * newWidth + x;
                newColors[newIndex] = sourceColors[sourceIndex];
            }
        }

        // 创建新Texture
        newTexture = new Texture2D(newWidth, sourceHeight);
        newTexture.SetPixels(newColors);
        newTexture.Apply();
}

2................

Texture2D newTexture;
void Start
{
      newTexture = new Texture2D(615, 615);
}
public void SplitImg(Texture2D t2d)
{
        newColors = t2d.GetPixels(0, 0, 615, 615);
        //将数据设置到新纹理中
        newTexture.SetPixels(newColors);
        //应用修改
        newTexture.Apply();
}
        
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容