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

Unity3D 截屏功能

2015-05-23 13:53 218 查看
private Texture tex2D;

private IEnumerator CutScreen()

{

string SavePath = Application.persistentDataPath;

#if UNITY_EDITOR

SavePath = Application.dataPath;

#endif

int width = Screen.width;

int height = Screen.height;

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

yield return new WaitForEndOfFrame();

tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);

tex.Apply();

byte[] bytes = tex.EncodeToPNG();

Destroy(tex);

File.WriteAllBytes(SavePath + "/../SavedScreen.png", bytes);

yield return null;

//读取图片//

string Url = "file://" + SavePath + "/../SavedScreen.png";

WWW www = new WWW(Url);

yield return www;

if (www.error != null)

{

Debug.LogError(www.error);

}

tex2D = www.texture;

****************************************************************************************

//显示图片//

建一个父panel

GameObject photo = new GameObject("ScreenshotObj");

UITexture uiTexture = photo.AddComponent<UITexture>();

uiTexture.mainTexture = tex2D;

photo.layer = NGUI摄像机的layer;

photo.transform.parent = 父panel.transform;

photo.transform.localPosition = xxxxxx;

photo.transform.localScale = xxxxx;

uiTexture.width = xxx;

uiTexture.height=xxx;

uiTexture.depth=NGUIToos.CalculateNextDepth(父panel.gameObject);

******************************************************************************************

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