2017-06-26 39 views

回答

0

我唯一想到的就是使用Texture2D.FromStream(),但它會讀取一個完整的圖像文件,所以它不會真正起作用。

我爲我的一款遊戲做的是創建一個圍繞Texture2D的包裝,它只使用接受源矩形和目標矩形的SpriteBatch.Draw()重載繪製紋理的特定部分。

+0

我在我的遊戲中使用了sourceRectangle解決方案,所以現在,我正在爲每個圖塊上的遊戲繪製整個圖集,但是使用sourceRectangle裁剪它。 – emil1000123

0

您是否想過使用GetDataSetData

我只是寫用於測試目的的擴展方法:

public static class TextureExtension 
{ 
    /// <summary> 
    /// Creates a new texture from an area of the texture. 
    /// </summary> 
    /// <param name="graphics">The current GraphicsDevice</param> 
    /// <param name="rect">The dimension you want to have</param> 
    /// <returns>The partial Texture.</returns> 
    public static Texture2D CreateTexture(this Texture2D src, GraphicsDevice graphics, Rectangle rect) 
    { 
     Texture2D tex = new Texture2D(graphics, rect.Width, rect.Height); 
     int count = rect.Width * rect.Height; 
     Color[] data = new Color[count]; 
     src.GetData(0, rect, data, 0, count); 
     tex.SetData(data); 
     return tex; 
    } 
} 

現在,您可以調用它像這樣:

newTexture = sourceTexture.CreateTexture(GraphicsDevice, new Rectangle(50, 50, 100, 100)); 

如果你只是想繪製紋理的一部分,你可以建議使用像domi1819那樣的SpriteBatch過載。

+0

我在我的遊戲中使用了sourceRectangle解決方案,所以現在我正在爲每個圖塊上的遊戲繪製整個圖集,但使用sourceRectangle進行裁剪。不過感謝這個解決方案,如果我需要它,我可以使用它。 – emil1000123