unity 截图 压缩 处理
/****************************************************
*
* unity屏幕截图,并转换成Base64码
* 作者: lyb
* 日期:2017年7月25日
*
*****************************************************/
using System;
using System.IO;
using UnityEngine;
using System.Collections;
public class Tools_TexScreenshot : MonoBehaviour
{
public delegate void WeChatTexScreenShotDelegate(bool isSucc);
public WeChatTexScreenShotDelegate WeChatTexScreenShotCallBack;
private static Tools_TexScreenshot _Instance;
public static Tools_TexScreenshot Instance
{
get { return _Instance; }
}
void Awake()
{
_Instance = this;
}
void OnDestroy()
{
_Instance = null;
Texture_ShotDelete();
}
/// <summary>
/// 截图图片保存路径
/// </summary>
private string texShotPath
{
get
{
return projectQ.PathHelper.PersistentPath + "/screencapture.jpg";
}
}
/// <summary>
/// 压缩后的截图图片保存路径
/// </summary>
private string texZipShotPath
{
get
{
return projectQ.PathHelper.PersistentPath + "/texShare.jpg";
}
}
public byte[] GetCaptureTexture(Texture2D _tex2d)
{
_tex2d.Compress(false);
Texture2D texZip = Tools_TexGzip.TexGzipBegin(_tex2d, 0.5f);
return texZip.EncodeToJPG();
}
/// <summary>
/// 屏幕截图保存
/// </summary>
/// <param name="startPoint">截图起点坐标</param>
/// <param name="shotSize">截图大小</param>
public void Texture_Screenshot(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack)
{
StartCoroutine(GetCapture(startPoint, shotSize, shotBack));
}
IEnumerator GetCapture(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack)
{
// 截屏之前,删除截屏缓存文件
Texture_ShotDelete();
yield return new WaitForEndOfFrame();
int width = (int)shotSize.x;
int height = (int)shotSize.y;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(startPoint.x, startPoint.y, width, height), 0, 0, true);
//对屏幕缓存进行压缩
tex.Compress(false);
//对图片进行压缩
Texture2D texZip = Tools_TexGzip.TexGzipBegin(tex, 0.5f);
//转化为jpg图
byte[] imagebytes = texZip.EncodeToJPG();
//存储png图
File.WriteAllBytes(texShotPath, imagebytes);
yield return null;
if (shotBack != null)
{
shotBack(true);
}
}
/// <summary>
/// 图片压缩
/// </summary>
void Texture_Gzip(int height, int width, WeChatTexScreenShotDelegate shotBack)
{
if (shotBack != null)
{
shotBack(true);
}
}
/// <summary>
/// 将图片数据转换为Base64字符串
/// </summary>
public string Texture_ToBase64()
{
if (!System.IO.File.Exists(texShotPath))
{
Debug.Log(" 图片路径无效: " + texShotPath);
return null;
}
var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texShotPath));
Debug.Log(" #[图片转换成Base64]# 成功 图片信息" + base64Img);
// 分享完成,图片删除
Texture_ShotDelete();
return base64Img.ToString();
}
/// <summary>
/// 将指定路径图片转换为Base64字符串
/// </summary>
public string Texture_FixedToBase64(string texFixedPath)
{
var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texFixedPath));
return base64Img.ToString();
}
/// <summary>
/// 将本地图片转换为Base64字符串
/// </summary>
public string Texture_LocalToBase64(byte[] _bytes)
{
var base64Img = Convert.ToBase64String(_bytes);
return base64Img.ToString();
}
/// <summary>
/// 将保存下来的用于分享的图片删除掉
/// </summary>
public void Texture_ShotDelete()
{
if (File.Exists(texShotPath))
{
Debug.Log(" #[如果截图的图片还在则删掉]# ");
File.Delete(texShotPath);
}
if (File.Exists(texZipShotPath))
{
Debug.Log(" #[图片转换成Base64成功后删除]# ");
File.Delete(texZipShotPath);
}
}
}
unity 截图 压缩 处理的更多相关文章
- Unity --- 纹理压缩基本知识点
1.Unity支持的压缩格式的分类,这里主要指Android平台和IOS平台: DXT格式 --- Nvidia Tegra(图睿)提供ETC --- 安卓原生支持的,OPNEGL2.0都支持,ET ...
- Unity截图
什么都不说了,直接上代码. using UnityEngine; using System.Collections; using System.IO; public class CutImage : ...
- Unity 截图选择框,中间全透明,边缘半透明
效果:点击白色框可拖拽选择区域 代码: using System.Collections; using System.Collections.Generic; using UnityEngine; u ...
- Unity项目接入应用宝SDK实现截图功能
Unity项目接入应用宝SDK实现截图功能 问题由来 点击应用宝悬浮窗 如图所示 左下角有一个截图按钮 需要解决那些问题 截图信息需要由游戏引擎提供 SDK获取截图信息为同步 但是Unity引擎没有提 ...
- 【Unity技巧】调整画质(贴图)质量
写在前面 当我们在Unity中,使用图片进行2D显示时,会发现显示出来的画面有明显的模糊或者锯齿,但是美术给的原图却十分清晰. 要改善这一状况实际上很简单. 造成这样的原因,是Unity在导入图片(或 ...
- Unity骨骼动画资源解析与优化
一,背景 最近发现项目的动画文件有点大,不光内存大,而且文件也很大,所以从这2个方面下手处理 二,动画文件大小优化 为了优化动画文件大小,我们可以先分析下文件,Ctrl+D将动画文件从FBX拷贝出来, ...
- 构造Huffman以及实现
构造Huffman 题目 在作业本上分别针对权值集合W=(6,5,3,4,60,18,77)和W=(7,2,4,5,8)构造哈夫曼树,提交构造过程的照片 错误回答 错误原因:遵循左边小于根右边大于根的 ...
- unity3d代码优化标准
转载自:https://blog.csdn.net/m0_37283423/article/details/84378384 代码优化 ● 尽可能使用for来代替foreach:每次foreach会产 ...
- iOS-格式化金额,三位一逗号
代码地址如下:http://www.demodashi.com/demo/11244.html 项目版本更新迭代中, 新增需求: 所有金额必须用标准会计表示方式(¥94,862.57). 而之前金额展 ...
随机推荐
- JustOj 1486: Hello, world!
题目描述 This is the first problem for test. Since all we know the ASCII code, your job is simple: Input ...
- Python+OpenCV图像处理(六)—— ROI与泛洪填充
一.ROI ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆.不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI. 代码如下 ...
- 现代汽车加入Linux 基金会和 AGL协作平台
1月4日,现代汽车宣布已加入 Linux 基金会和其旗下的非营利协作平台 Automotive Grade Linux(AGL),现代汽车公司副总裁兼信息娱乐技术中心负责人 Paul Choo 表示: ...
- MyEclipse非正常关闭问题
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- IntelliJ IDEA的使用之调试方法
不管学什么要及时复习和练习!!不然就会忘掉,有个输入输出的过程 IntelJ IDEA的使用之断点调试 1)添加断点:直接在代码的最左侧点一下就行,出现红色圈圈. 2)单步运行:mac中F7.F8有其 ...
- mysql复习之一
DROP DATABASE mysql_shiyan;. cd /home/shiyanlou/Desktop git clone https://github.com/shiyanlou/SQL4 ...
- powermock+mockito+testng 单元测试pom文件
0:Supported versions PowerMock version 1.7.0 and upper has experimental support of Mockito 2. A lot ...
- JavaWeb中的资源映射
一./与/* <url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...
- Angular4.x 安装|创建项目|目录结构|创建组件
Angular4.x 安装|创建项目|目录结构|创建组件 安装最新版本的 nodejs node.js 官网:https://nodejs.org/zh-cn/ 去官网下载 node.js,下一步下一 ...
- 关于sql中in 和 exists 的效率问题
在用in的地方可以使用freemark标签代替,例如: 将 <#if assistantList??&& (assistantList?size > 0)> AND ...