Unity3D-截屏操作

来源:互联网 发布:淘宝怎样申请退款 编辑:程序博客网 时间:2024/06/07 15:10
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
原创粉丝点击