您的位置:首页 > 移动开发 > Unity3D

unity截取图片并合成

2016-03-11 16:07 513 查看
1.首先创建一个UI摄像机,并调整好Layer层,遮罩层之类的,拼拼界面。

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: