Unity 之 Texture和Texture2D 分享几个实用的方法,,,
Texture转换成Texture2D,,,
/// <summary>
/// Texture转换成Texture2D...
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
使用Texture2D 的形式截图,,,
/// <summary>
/// 截图...
/// </summary>
/// <param name="rect">截图的区域</param>
/// <returns></returns>
Texture2D CaptureScreenshot(Rect rect)
{
// 先创建一个的空纹理,大小可根据实现需要来设置
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
// 读取屏幕像素信息并存储为纹理数据,
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// 然后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张图片: {0}", filename));
// 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
return screenShot;
}
将Texture保存到本地,,,
/// <summary>
/// 将Texture转为本地PNG...
/// </summary>
/// <param name="filePath"></param>
/// <param name="teture"></param>
/// <returns></returns>
public static bool saveMainTextureToPng(string filePath, Texture teture)
{
if (teture.GetType() != typeof(Texture2D))
{
return false;
}
Texture2D savedTexture = (Texture2D)teture;
try
{
Texture2D newTexture = new Texture2D(savedTexture.width, savedTexture.height, TextureFormat.RGBA32, false);
newTexture.SetPixels(0, 0, savedTexture.width, savedTexture.height, savedTexture.GetPixels());
newTexture.Apply();
byte[] bytes = newTexture.EncodeToPNG();
if (bytes != null && bytes.Length > 0)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
System.IO.File.WriteAllBytes(filePath, bytes);
}
}
catch (IOException ex)
{
return false;
}
return true;
}
将本地图片转换为Byte[]数组,,,
/// <summary>
/// 将图片转换为byte数组...
/// </summary>
/// <param name="filePath">图片路径</param>
/// <returns></returns>
public static byte[] ReadTexture(string filePath)
{
FileStream fileStream = new FileStream(filePath, FileMode.Open, System.IO.FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建byte数组 ...
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
return buffer;
}
压缩并截取中间部分
压缩并截取中间部分图片作为用户图片
流程:传入图片路径 --> 读取为Texture2D --> 对图片进行压缩 --> 对图片进行截取 --> 处理后的图片保存到本地 -->返回图片路径
#region 压缩并截取中间部分图片
/// <summary>
/// 压缩并截取中间部分图片
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
private string CompressTexture(string imagePath)
{
if (string.IsNullOrEmpty(imagePath)) return "";
byte[] fileData = File.ReadAllBytes(imagePath);
Texture2D tex = new Texture2D((int) (Screen.width), (int) (Screen.height), TextureFormat.RGBA32, false);
tex.LoadImage(fileData);
float miniSize = Mathf.Max(tex.width, tex.height);
float scale = 1200.0f / miniSize;
if (scale > 1.0f)
{
scale = 1.0f;
}
Texture2D temp = ScaleTexture(tex, (int) (tex.width * scale), (int) (tex.height * scale));
byte[] pngData = temp.EncodeToJPG();
// 保存图片路径 //imagePath.Replace(".png", "_min.jpg"); // 此路径可测试时使用,便于查看截出图片
string miniImagePath = Application.persistentDataPath + "/_min.jpg";
File.WriteAllBytes(miniImagePath, pngData);
Destroy(tex);
Destroy(temp);
return miniImagePath;
}
private Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
{
// 压缩原图大小
Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
Color[] rpixels = result.GetPixels(0);
float incX = ((float) 1 / source.width) * ((float) source.width / targetWidth);
float incY = ((float) 1 / source.height) * ((float) source.height / targetHeight);
for (int px = 0; px < rpixels.Length; px++)
{
rpixels[px] = source.GetPixelBilinear(incX * ((float) px % targetWidth),
incY * ((float) Mathf.Floor(px / targetWidth)));
}
result.SetPixels(rpixels, 0);
result.Apply();
// 截图大小 (根据长或宽小的,作为截图尺寸)
int targetScale = targetWidth < targetHeight ? targetWidth : targetHeight;
Texture2D mTexture = new Texture2D(targetScale, targetScale);
// 计算偏移(此处是从中间截图)
Vector2 offset = new Vector2((targetWidth - targetScale) / 2, (targetHeight - targetScale) / 2);
// 按照偏移和大小截图
mTexture.SetPixels(result.GetPixels((int) offset.x, (int) offset.y, targetScale, targetScale));
mTexture.Apply();
return mTexture;
}
#endregion