• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

常州手游开发-Unity截屏

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0328/429.html

常州手游开发-Unity截屏


在Unity中可能会遇到截图然后保管到材质中作为某种显现运用,下面常州平台运营就给大家引见三种截取屏幕的方式,大家可以根据自己的理论需求选择对应的截图方式。
 
直接上代码:
 //截图方式一
    private void CaptureUnity(string mFileName)
    {
        //全屏截图,图片保管途径,进步分辨率系数
        Application.CaptureScreenshot(mFileName, 0);
    }
    //截图方式二
    // 依据一个Rect类型来截取指定范围的屏幕  
   private IEnumerator CaptureByRect(Rect mRect, string mFileName)
    {
        //等候渲染线程完毕  
        yield return new WaitForEndOfFrame();
        //初始化Texture2D  
        Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据  
        mTexture.ReadPixels(mRect, 0, 0);
        mTexture.Apply();
        //将图片信息编码为字节信息  
        byte[] bytes = mTexture.EncodeToPNG();
        //常州微信公众平台保管  
        System.IO.File.WriteAllBytes(mFileName, bytes);
     }
    //截图方式三
    //以某一camera截图
    private IEnumerator CaptureByCamera(Camera mCamera, Rect mRect, string mFileName)
    {
        yield return new WaitForEndOfFrame();
        //初始化RenderTexture  
        RenderTexture mRender = new RenderTexture((int)mRect.width, (int)mRect.height, 0);
        //设置相机的渲染目的  
        mCamera.targetTexture = mRender;
        //开端渲染  
        mCamera.Render();
        //激活渲染贴图读取信息  
        RenderTexture.active = mRender;
        Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据  
        mTexture.ReadPixels(mRect, 0, 0);
        mTexture.Apply();
        //释放常州微信小程序开发相机,销毁渲染贴图  
        mCamera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(mRender);
        byte[] bytes = mTexture.EncodeToPNG();
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }  
 

上篇:上一篇:在运转时脚本中运用UnityEditor命名空间
下篇:下一篇:Unity Shader场景切换过度效果完成