原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0123/354.html
常州手游开发-U3D截图并保存相册
很多玩家喜欢把游戏中一些自己喜欢的画面选择截图保存下来,本篇文章我们常州网站开发建设就介绍下如何做一个截图的功能,并且其他人可以在相册中看到。
分为两个部分来介绍,第一个部分是截图,用Application.CaptureScreenshot方法即可实现;第二部分就是保存到相册,获取图片保存的路径即可。
private static string _TextureUrl;
public static string TextureUrl
{
set { _TextureUrl = value; }
get
{
if (_TextureUrl == null)
{
// "/sdcard/Pictures/Test";
#if UNITY_ANDROID && !UNITY_EDITOR
string temp =Application.persistentDataPath;
DirectoryInfo diInfo = new DirectoryInfo(temp);
_TextureUrl=diInfo.Parent.Parent.Parent.Parent.FullName+"/Pictures/Test";
#elif UNITY_IPHONE && !UNITY_EDITOR
_TextureUrl = Application.persistentDataPath+"/Test";
#else
_TextureUrl = Application.dataPath + "/Test";
#endif
System.IO.Directory.CreateDirectory(_TextureUrl);
}
return _TextureUrl;
}
}
public static string GetFileNameToTime
{
get
{
return "tb_" + DateTime.Now.ToFileTime().ToString() + "游戏开发运营.jpg";
}
}
public void ScreenShotStart (string parem)
{
Debug.Log ("This is param: " + parem);
string fileName = "";
if (string.IsNullOrEmpty (parem))
fileName = AppConfig.TextureUrl + "/" + AppConfig.GetFileNameToTime;
else
fileName = AppConfig.TextureUrl + "/" + parem;
StartCoroutine (CaptureScreenshot (MainCamera, fileName));
}
/// <summary>
/// 截图操作
/// </summary>
/// <returns>The screenshot.</returns>
/// <param name="mCamera">相机</param>
/// <param name="mFileName">完整常州手游开发路径</param>
IEnumerator CaptureScreenshot (Camera mCamera, string mFileName)
{
Rect rect = new Rect ();
rect.width = Screen.width;
rect.height = Screen.height;
//等待渲染线程结束
yield return new WaitForEndOfFrame ();
//初始化RenderTexture
RenderTexture mRender = new RenderTexture ((int)rect.width, (int)rect.height, 0);
//设置相机的渲染目标
mCamera.targetTexture = mRender;
//开始渲染
mCamera.Render ();
//激活渲染贴图读取信息
RenderTexture.active = mRender;
Texture2D mTexture = new Texture2D ((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
//读取屏幕像素信息并存储为纹理数据
mTexture.ReadPixels (rect, 0, 0);
//应用
mTexture.Apply ();
//释放相机,销毁常州游戏开发培训渲染贴图
mCamera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy (mRender);
//将图片信息编码为字节信息
byte[] bytes = mTexture.EncodeToPNG ();
//保存
File.WriteAllBytes (mFileName, bytes);
yield return bytes;
#if UNITY_IPHONE && !UNITY_EDITOR
CallIosMeth.ShowImgToAlbum(mFileName);
#endif
}
/// <summary>
/// IOS 保存相册操作
/// </summary>
/// <param name="typeid">Typeid.</param>
public static void ShowImgToAlbum (string path)
{
#if UNITY_IPHONE
ImgToAlbum(path);
#endif
}
#if UNITY_IPHONE
[DllImport ("手机App外包__Internal")]
private static extern void ImgToAlbum (string path);
#endif
上篇:上一篇:常州游戏开发-U3D数据结构Queue
下篇:下一篇:常州手游开发-U3D用实现本地数据加密