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). 而之前金额展 ...
随机推荐
- MySQL性能测试工具sysbench的安装和使用
sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQL.Oracle和PostgreSQL.当前 ...
- 删去k个数字后的最小值
public static String removeKDigits(String num,int k) { //新整数的最终长度=原长度 - k int newLength=num.length() ...
- windows下使用命令行运行PHP
之前一直想,在命令行下能不能运行PHP程序,像C语言一样可以通过命令行拿到参数.今天尝试了一下发现可感觉挺有意思的,平时写着程序玩的时候就可以这样用,下面让咱么来看看怎么做的.我的环境是 php5.6 ...
- sql存储过程算法
MSSQL执行 : exec proc_NAME ORACLE : beginproc_NAME;commit;end; 1.求素数 MSSQL; CREATE proc [dbo].[EXEC003 ...
- 论文阅读笔记 Improved Word Representation Learning with Sememes
论文阅读笔记 Improved Word Representation Learning with Sememes 一句话概括本文工作 使用词汇资源--知网--来提升词嵌入的表征能力,并提出了三种基于 ...
- kali拿取路由器pin码
前序:拿pin码就可以直接跑出WPA PSK, 下面开始记录我的操作过程. 网卡监控模式 airmon-ng start wlan0 扫描 wash -i wlan0mon 破pin 网卡 物理地址 ...
- windows线程池之I/O完成端口(IOCP)
对于这个学习主要参考博客 http://blog.csdn.net/neicole/article/details/7549497
- java常用类-StringBuffer,Integer,Character
* StringBuffer: * 线程安全的可变字符串. * * StringBuffer和String的区别? * 前者长度和内容可变,后者不可变. * 如果使用前者做字符串的拼接,不会浪费太多的 ...
- poj 2942 Knights of the Round Table - Tarjan
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- 使用SimpleDateFormat时的日期和时间模式
日期和时间模式 日期和时间格式由日期和时间模式 字符串指定.在日期和时间模式字符串中,未加引号的字母 ‘A’ 到’Z’ 和’a’ 到’z’ 被解释为模式字母,用来表示日期或时间字符串元素.文本可以使用 ...