Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System.Collections;
/// <summary>
/// Texture2D extensions.
/// Set of utilities for manipulating Texture2D.
/// </summary>
public class Texture2DExtensions
{
/// <summary>
/// Crop the specified original Texture2D on the specified Rect frame.
/// </summary>
/// <param name="original">Original Texture2D.</param>
/// <param name="frame">Frame.</param>
public static Texture2D crop(Texture2D original, Rect frame)
{
int x = Mathf.Clamp((int)frame.center.x, (int)(frame.width*.5), original.width);
int y = Mathf.Clamp((int)frame.center.y, (int)(frame.height*.5), original.height);
Color[] crop = original.GetPixels(
x-(int)(frame.width*.5),
y-(int)(frame.height*.5),
(int)frame.width,
(int)frame.height);
Texture2D cropped = new Texture2D((int)frame.width, (int)frame.height);
cropped.SetPixels(crop);
return cropped;
}
/// <summary>
/// Flips horizontally a Texture2D.
/// </summary>
/// <returns>The flipped Texture2D.</returns>
/// <param name="original">Original Texture2D.</param>
public static Texture2D flipHorizontal(Texture2D original)
{
int width = original.width;
int height = original.height;
Texture2D flipped = new Texture2D(width, height);
for(int iCol=0; iCol<width; iCol++)
{
for(int iRow=0; iRow<height; iRow++)
{
flipped.SetPixel(width-iCol-1,
iRow,
original.GetPixel(iCol, iRow));
}
}
flipped.Apply();
return flipped;
}
/// <summary>
/// Flips vertically a Texture2D.
/// </summary>
/// <returns>The flipped Texture2D.</returns>
/// <param name="original">Original Texture2D.</param>
public static Texture2D flipVertical(Texture2D original)
{
int width = original.width;
int height = original.height;
Texture2D flipped = new Texture2D(width, height);
for(int iCol=0; iCol<width; iCol++)
{
for(int iRow=0; iRow<height; iRow++)
{
flipped.SetPixel(iCol,
height-iRow-1,
original.GetPixel(iCol, iRow));
}
}
flipped.Apply();
return flipped;
}
/// <summary>
/// Rotate the specified Texture2D. It can only rotate at 90° (both clockwise
/// and counter clockwise) and 180°
/// </summary>
/// <param name="original">Original Texture2D.</param>
/// <param name="angle">Angle of rotation.</param>
/// <param name="clockwise">If set to <c>true</c> rotate clockwise.</param>
public static Texture2D rotate(Texture2D original, float angle, bool clockwise)
{
int width = original.width;
int height = original.height;
if(angle != 90 && angle != 180)
{
Debug.Log("I can only rotate at 90 or 180 degrees");
return null;
}
Texture2D rotated;
if(angle==90)
{
rotated = new Texture2D(height, width);
if(!clockwise)
{
for(int iCol=0; iCol<width; iCol++)
{
for(int iRow=0; iRow<height; iRow++)
{
rotated.SetPixel(height-iRow-1, iCol,
original.GetPixel(iCol, iRow));
}
}
}
else
{
for(int iCol=0; iCol<width; iCol++)
{
for(int iRow=0; iRow<height; iRow++)
{
rotated.SetPixel(iRow, width-iCol-1,
original.GetPixel(iCol, iRow));
}
}
}
}
else //angle 180
{
rotated = new Texture2D(width, height);
rotated = flipTexture(original);
}
rotated.Apply();
return rotated;
}
private static Texture2D flipTexture(Texture2D original)
{
int width = original.width;
int height = original.height;
Texture2D flipped = new Texture2D(width, height);
for(int iCol=0; iCol<width; iCol++)
{
for(int iRow=0; iRow<height; iRow++)
{
flipped.SetPixel(width-iCol-1,
height-iRow-1,
original.GetPixel(iCol, iRow));
}
}
flipped.Apply();
return flipped;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment