Wanzyee Studio
TextureExtension

Extension methods for UnityEngine.Texture and UnityEngine.Color. More...

Static Public Member Functions

static bool IsEncodable (this Texture2D texture)
 Determine if the texture is able to encode to PNG or JPG. More...
 
static string SaveFile (this Texture2D texture, string path)
 Save a UnityEngine.Texture2D, with supported format and readable flag, to a file. More...
 
static void SetPixels (this Texture2D texture, RenderTexture source)
 Set pixels from given UnityEngine.RenderTexture source. More...
 
static void SetPixels (this Texture2D texture, Camera camera, int depthBuffer=24, int antiAliasing=1)
 Set pixels from given UnityEngine.Camera source. More...
 
static RenderTextureFormat GetReadableFormat (this Texture2D texture)
 Get the UnityEngine.RenderTextureFormat by given texture format. More...
 
static TextureFormat GetReadableFormat (this RenderTexture texture)
 Get the UnityEngine.TextureFormat by given render texture format. More...
 
static Color32[] GetPixels32 (this RenderTexture texture)
 Get a block of pixel colors. More...
 
static Color[] GetPixels (this RenderTexture texture)
 Get a block of pixel colors. More...
 
static void SetPixels32 (this RenderTexture texture, Color32[] colors)
 Set a block of pixel colors. More...
 
static void SetPixels (this RenderTexture texture, Color[] colors)
 Set a block of pixel colors. More...
 
static void SetPixels (this RenderTexture texture, Camera camera)
 Set pixels from given UnityEngine.Camera source. More...
 
static void Resize (this RenderTexture texture, int width, int height)
 Resize the UnityEngine.RenderTexture with the new width and height. More...
 
static void Clear (this RenderTexture texture)
 Clear the content of the UnityEngine.RenderTexture. More...
 
static void Flip (this Color[] colors, int width, bool horizontal, bool vertical)
 Flip image colors by the specified axes and width, this will modify the original array sorting. More...
 
static void Flip (this Color32[] colors, int width, bool horizontal, bool vertical)
 Flip image colors by the specified axes and width, this will modify the original array sorting. More...
 
static Color[] GetPartial (this Color[] colors, int width, Rect rect)
 Get partial colors within the rect inside image pixel size. More...
 
static Color32[] GetPartial (this Color32[] colors, int width, Rect rect)
 Get partial colors within the rect inside image pixel size. More...
 
static Color[] GetScaled (this Color[] colors, int width, Vector2 size)
 Get the colors of scaled image with antialiasing. More...
 
static Color GetBlurred (this Color[] colors, int width, Vector2 center, Vector2 extend)
 Get blurred color at the specified position in image colors. More...
 

Detailed Description

Extension methods for UnityEngine.Texture and UnityEngine.Color.

Member Function Documentation

static bool IsEncodable ( this Texture2D  texture)
static

Determine if the texture is able to encode to PNG or JPG.

Note, this only checks the texture format, not check the readable flag. Unity will throw exception if not readable, but only log if format wrong when encode.

Parameters
textureTexture.
Returns
true if is encodable; otherwise, false.
static string SaveFile ( this Texture2D  texture,
string  path 
)
static

Save a UnityEngine.Texture2D, with supported format and readable flag, to a file.

Encode to JPG if the file path ends with ".jpg" extension, otherwise to PNG. Note, this directly overwrite original file if exists.

Parameters
textureTexture.
pathFile path.
Returns
The file full path.
static void SetPixels ( this Texture2D  texture,
RenderTexture  source 
)
static

Set pixels from given UnityEngine.RenderTexture source.

As Texture2D.SetPixels(), you should make sure the size fit. To get a screenshot by assigning RenderTexture.active. This doesn't call Texture2D.Apply().

Parameters
textureTexture.
sourceSource texture.
static void SetPixels ( this Texture2D  texture,
Camera  camera,
int  depthBuffer = 24,
int  antiAliasing = 1 
)
static

Set pixels from given UnityEngine.Camera source.

Passing through rendering into temporary UnityEngine.RenderTexture. This doesn't call Texture2D.Apply().

Parameters
textureTexture.
cameraSource camera.
depthBufferDepth buffer bits, allows 0, 16 or 24 only.
antiAliasingAnti-aliasing, allows 1, 2, 4 or 8 only.
static RenderTextureFormat GetReadableFormat ( this Texture2D  texture)
static

Get the UnityEngine.RenderTextureFormat by given texture format.

Used to create UnityEngine.RenderTexture for this to read pixels from.

Parameters
textureTexture.
Returns
The UnityEngine.RenderTextureFormat.
static TextureFormat GetReadableFormat ( this RenderTexture  texture)
static

Get the UnityEngine.TextureFormat by given render texture format.

Used to create UnityEngine.Texture2D to read pixels from this.

Parameters
textureTexture.
Returns
The UnityEngine.TextureFormat.
static Color32 [] GetPixels32 ( this RenderTexture  texture)
static

Get a block of pixel colors.

Process pass through UnityEngine.Texture2D in fact.

Parameters
textureTexture.
Returns
The pixels.
static Color [] GetPixels ( this RenderTexture  texture)
static

Get a block of pixel colors.

Process pass through UnityEngine.Texture2D in fact.

Parameters
textureTexture.
Returns
The pixels.
static void SetPixels32 ( this RenderTexture  texture,
Color32[]  colors 
)
static

Set a block of pixel colors.

Process pass through UnityEngine.Texture2D in fact.

Parameters
textureTexture.
colorsSource colors.
static void SetPixels ( this RenderTexture  texture,
Color[]  colors 
)
static

Set a block of pixel colors.

Process pass through UnityEngine.Texture2D in fact.

Parameters
textureTexture.
colorsSource colors.
static void SetPixels ( this RenderTexture  texture,
Camera  camera 
)
static

Set pixels from given UnityEngine.Camera source.

Temporary set as target texture to render into.

Parameters
textureTexture.
cameraSource camera.
static void Resize ( this RenderTexture  texture,
int  width,
int  height 
)
static

Resize the UnityEngine.RenderTexture with the new width and height.

Parameters
textureTexture.
widthNew width.
heightNew height.
static void Clear ( this RenderTexture  texture)
static

Clear the content of the UnityEngine.RenderTexture.

Parameters
textureTexture.
static void Flip ( this Color[]  colors,
int  width,
bool  horizontal,
bool  vertical 
)
static

Flip image colors by the specified axes and width, this will modify the original array sorting.

Parameters
colorsColor array.
widthImage width.
horizontalHorizontal.
verticalVertical.
static void Flip ( this Color32[]  colors,
int  width,
bool  horizontal,
bool  vertical 
)
static

Flip image colors by the specified axes and width, this will modify the original array sorting.

Parameters
colorsColor array.
widthImage width.
horizontalHorizontal.
verticalVertical.
static Color [] GetPartial ( this Color[]  colors,
int  width,
Rect  rect 
)
static

Get partial colors within the rect inside image pixel size.

You can use Rect.NormalizedToPoint() to convert the rect from UV space in need.

Parameters
colorsColor array.
widthImage width.
rectPartial rect.
Returns
The partial colors.
static Color32 [] GetPartial ( this Color32[]  colors,
int  width,
Rect  rect 
)
static

Get partial colors within the rect inside image pixel size.

You can use Rect.NormalizedToPoint() to convert the rect from UV space in need.

Parameters
colorsColor array.
widthImage width.
rectPartial rect.
Returns
The partial colors.
static Color [] GetScaled ( this Color[]  colors,
int  width,
Vector2  size 
)
static

Get the colors of scaled image with antialiasing.

This cost performance, not to use too often.

Parameters
colorsColor array.
widthImage width.
sizeNew size.
Returns
The scaled image colors.
static Color GetBlurred ( this Color[]  colors,
int  width,
Vector2  center,
Vector2  extend 
)
static

Get blurred color at the specified position in image colors.

Calculate average color surround center within extend range.

Parameters
colorsColor array.
widthImage width.
centerCenter position.
extendExtend size.
Returns
The blurred color.