unity截取图片并合成
2016-03-11 16:07
513 查看
1.首先创建一个UI摄像机,并调整好Layer层,遮罩层之类的,拼拼界面。
2.制作成预制体。(代码中的路径可能需要改)加载的地方改下Resource.Load();
3.先点字一个GUIBUtton,再点第二个才会好使。
下面给出代码自己看看吧,嘿嘿:
2.制作成预制体。(代码中的路径可能需要改)加载的地方改下Resource.Load();
3.先点字一个GUIBUtton,再点第二个才会好使。
下面给出代码自己看看吧,嘿嘿:
using UnityEngine; using System.Collections; using System.IO; public class GameUISharePopUp : MonoBehaviour { private string imagePath = ""; void OnGUI() { if (GUI.Button(new Rect(10, 10, 200, 20), "first capture")) { StartCoroutine(GenerateShareScreenshot()); } if (GUI.Button(new Rect(10, 30, 200, 20),"cut")) { string fileName = Application.persistentDataPath+"wwb"; Texture2D shareBgImg = LoadImageFromBytes(File.ReadAllBytes(fileName)); if (shareBgImg == null) { Debug.LogErrorFormat("Can not load Screenshot Image {0}.", fileName); return; } StartCoroutine(GenerateShareImage("www", "hahaha", shareBgImg)); } } private IEnumerator GenerateShareScreenshot() { string fileName = Application.persistentDataPath + "wwb"; Debug.Log("fileName= "+fileName); if (File.Exists(fileName)) File.Delete(fileName); yield return new WaitForEndOfFrame(); Application.CaptureScreenshot(fileName); while (!File.Exists(fileName)) yield return null; yield return new WaitForEndOfFrame(); } private Texture2D LoadImageFromBytes(byte[] bytes) { Texture2D imgData = new Texture2D(512,512,TextureFormat.ARGB32,false); imgData.LoadImage(bytes); return imgData; } public IEnumerator GenerateShareImage(string typeName,string saveName,Texture2D shareBgImg=null) { GUISharePic.Instance.Refresh(typeName, "aaa", shareBgImg); yield return null; Texture2D imgData = GUISharePic.Instance.TakeSnapShot(); byte[] bytes = imgData.EncodeToJPG(100);// 1 ~ 100 string savePath = Application.persistentDataPath+"/Share/1.jpg"; string dirName = Path.GetDirectoryName(savePath); if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName); File.WriteAllBytes(savePath, bytes); imagePath = savePath; GUISharePic.Instance.DestroySelf(); } }还有另一个:
using UnityEngine; using System.Collections; public class GUISharePic : MonoBehaviour { private static GUISharePic mInstance; private float screenScale; public static GUISharePic Instance { get { if (mInstance == null) { CreateInstance(); } return mInstance; } } private static void CreateInstance() { if (mInstance != null) { return; } GameObject prefab = Res.LoadGUI("GUI/GUISharePic"); if (prefab == null) { Debug.LogError("Res.Load GUI/GUISharePic error"); return; } GameObject go = NGUITools.AddChild(GameUIManager.mInstance.uiRoot.gameObject, prefab); if (go == null) { Debug.LogError("AddChild GUISharePic error"); return; } mInstance = go.AddComponent<GUISharePic>(); int screenHeight = Screen.height; float scale = GameUIManager.mInstance.uiRoot.GetPixelSizeAdjustment(screenHeight); screenHeight = Mathf.CeilToInt(screenHeight * scale); scale = (float)screenHeight / 640; go.transform.localPosition = new Vector3(0, 0, 10000); go.transform.localScale = new Vector3(scale, scale, scale); mInstance.screenScale = scale; } private UITexture mFg; private UITexture mBg; private UISprite mDescSp; private UILabel mServerName; private UILabel mID; private UILabel mBattleNum; private UILabel mFromDesc; private GameObject mTxt0; private GameObject mTxt1; private GameObject mTxt2; private Camera mRenderCamera; private RenderTexture mRenderTexture; void Awake() { mRenderCamera = transform.GetComponent<Camera>(); mRenderTexture = new RenderTexture(1136, 640, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); mRenderCamera.targetTexture = mRenderTexture; Transform winBg = transform.Find("UIMiddle"); mFg = winBg.Find("Fg").GetComponent<UITexture>(); mBg = winBg.Find("Bg").GetComponent<UITexture>(); mDescSp = mFg.transform.Find("desc").GetComponent<UISprite>(); mServerName = winBg.Find("txt0/num").GetComponent<UILabel>(); mID = winBg.Find("txt1/num").GetComponent<UILabel>(); mBattleNum = winBg.Find("txt2/num").GetComponent<UILabel>(); mFromDesc = winBg.Find("from").GetComponent<UILabel>(); mTxt0 = winBg.Find("txt0").gameObject; mTxt1 = winBg.Find("txt1").gameObject; mTxt2 = winBg.Find("txt2").gameObject; } public void Refresh(string shareTxt,string fromTxt,Texture2D shareBgImg) { if (shareBgImg != null) { mFg.mainTexture = shareBgImg; mFg.transform.localPosition = new Vector3(-78,60,0); float scale = screenScale * 400 / shareBgImg.height; int width = Mathf.CeilToInt(shareBgImg.width * scale); int height = Mathf.CeilToInt(shareBgImg.height * scale); if (width < 600) width = 600; mFg.width = width; mFg.height = height; mDescSp.spriteName = null; } else { mDescSp.spriteName = shareTxt; } mServerName.text = "houhou"; mID.text = "hahahahaha"; mBattleNum.text = "10000000000"; if (!string.IsNullOrEmpty(fromTxt)) mFromDesc.text = fromTxt; else mFromDesc.gameObject.SetActive(false); } public Texture2D TakeSnapShot() { RenderTexture.active = mRenderTexture; Texture2D imgData = new Texture2D(mRenderTexture.width, mRenderTexture.height, TextureFormat.ARGB32, false); imgData.ReadPixels(new Rect(0f, 0f, imgData.width, imgData.height), 0, 0); RenderTexture.active = null; return imgData; } public void DestroySelf() { Destroy(mInstance.gameObject); mInstance = null; } }
相关文章推荐
- Unity3d Toggle onValueChanged 传参数 错误(始终为最后一个实例的值)
- Unity 命令行打包
- 【转】unity面试题(问题篇)
- Unity3D研究院之初探PoolManager插件
- Unity3d中添加json的system.web.script.serialization
- Unity3d外包(北京)公司(长年承接U3D外包)
- 项目记录29--unity-tolua框架 c#导出定时器lua
- UNITY3D shader代码例子<二> 双面显示
- 如何在Unity5.0 下实现贴图材质的Animation动画功能
- UNITY3D shader代码例子<一> 法线颜色
- 使用Andorid Studio 创建unity 插件
- unity3d 尝试 基于地理定位的 增强现实
- Unity插件DOTween教程
- Unity中关于作用力方式ForceMode的功能注解
- 【Unity】关于游戏中的暂停
- unity自带寻路Navmesh
- unity3d脚本之鸡零狗碎
- Unity中用触摸控制物体旋转和放大
- [GUI] 图片背景是黑色的,在unity中怎么变成透明
- Unity原生实现录音功能