The code below provides a handy helper method for drawing GUI textures via GUI.DrawTextureWithTexCoords.
///
/// Draws a image image.
///
/// The destination image.
/// The x position in the destination image.
/// The y position in the destination image.
/// The destination width of the drawn image.
/// The destination height of the drawn image.
/// If set to true the image will be drawn fliped horizontally.
/// If set to true the image will be drawen flipped vertically.
/// If set to true the image will be tiled across the destination area.
/// image
/// If width, height, sourceWidth or sourceHeight are less then 1.
public static void Draw(Texture2D image, float x, float y, float width, float height, bool flipHorizontally, bool flipVertically, bool tile)
{
Draw(image, x, y, width, height, 0, 0, image.width, image.height, false, false, false);
}
///
/// Draws a image image.
///
/// The destination image.
/// The x position in the destination image.
/// The y position in the destination image.
/// The destination width of the drawn image.
/// The destination height of the drawn image.
/// The x position in the source image.
/// The y position in the source image.
/// The source width.
/// The source height.
/// image
/// If width, height, sourceWidth or sourceHeight are less then 1.
public static void Draw(Texture2D image, float x, float y, float width, float height, float sourceX, float sourceY, float sourceWidth, float sourceHeight)
{
Draw(image, x, y, width, height, sourceX, sourceY, sourceWidth, sourceHeight, false, false, false);
}
///
/// Draws a image image.
///
/// The destination image.
/// The x position in the destination image.
/// The y position in the destination image.
/// The destination width of the drawn image.
/// The destination height of the drawn image.
/// The x position in the source image.
/// The y position in the source image.
/// The source width.
/// The source height.
/// If set to true the image will be drawn fliped horizontally.
/// If set to true the image will be drawen flipped vertically.
/// If set to true the image will be tiled across the destination area.
/// image
/// If width, height, sourceWidth or sourceHeight are less then 1.
/// This method has issues with Clamped textures.
public static void Draw(Texture2D image, float x, float y, float width, float height, float sourceX, float sourceY, float sourceWidth, float sourceHeight,
bool flipHorizontally, bool flipVertically, bool tile)
{
// perform input validation
if (image == null)
{
throw new ArgumentNullException("image");
}
if (sourceWidth < float.Epsilon)
{
throw new ArgumentOutOfRangeException("sourceWidth");
}
if (sourceHeight < float.Epsilon)
{
throw new ArgumentOutOfRangeException("sourceHeight");
}
if (width < float.Epsilon)
{
return;
}
if (height < float.Epsilon)
{
return;
}
var imgWidth = (float)image.width;
var imgHeight = (float)image.height;
var srcWidth = sourceWidth / imgWidth;
var srcHeight = sourceHeight / imgHeight;
var position = new Rect(x, y + height, width, -height);
if (tile)
{
srcWidth = width / imgWidth;
srcHeight = height / imgHeight;
}
srcWidth = flipHorizontally ? -srcWidth : srcWidth;
srcHeight = !flipVertically ? -srcHeight : srcHeight;
var texCoords = new Rect(sourceX / imgWidth, imgHeight - (sourceY / imgHeight), srcWidth, srcHeight);
GUI.DrawTextureWithTexCoords(position, image, texCoords, true);
}