很多玩家喜欢把游戏中一些自己喜欢的画面选择截图保存下来,本篇文章就介绍下如何做一个截图的功能,并且玩家可以在相册中看到。

 

分为两个部分来介绍,第一个部分是截图,用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 ("__Internal")]
    private static extern void ImgToAlbum (string path);
    #endif