Unity3D游戏开发之截屏保存精彩瞬间

来源:互联网 发布:取个淘宝达人名字 编辑:程序博客网 时间:2024/05/16 05:52

今天我们来做点简单的东西,做个什么呢?答案就是截屏。作为一名热爱单机游戏的玩家,博主每次在玩游戏的同时截取游戏中比较喜欢的画面,特别是学习了Unity3D以后玩游戏的时候更多地是从一个游戏设计者的角度来看待游戏,换句话说,可能关注技术的成分更多一点吧。比如博主在写[Unity3D]Unity3D游戏开发之自由视角下的角色控制[Unity3D]Unity3D游戏开发之角色控制漫谈》 这两篇文章时,碰到博主无法把握的问题的时候就是通过玩游戏来体会的,在玩游戏的过程中博主总是会抓取博主比较喜欢的画面,如图,下面是博主收集的部分截图:

好了,欣赏完美丽的风景,下面我们就来一起学习在Unity3D实现截屏,先给出实现截屏的三种实现方式:

/// <summary>  /// 使用Application类下的CaptureScreenshot()方法实现截图  /// 优点:简单,可以快速地截取某一帧的画面、全屏截图  /// 缺点:不能针对摄像机截图,无法进行局部截图  /// </summary>  /// <param name="mFileName">M file name.</param>  private void CaptureByUnity(string mFileName)  {    Application.CaptureScreenshot(mFileName,0);  }  /// <summary>  /// 根据一个Rect类型来截取指定范围的屏幕  /// 左下角为(0,0)  /// </summary>  /// <param name="mRect">M rect.</param>  /// <param name="mFileName">M file name.</param>  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);        //如果需要可以返回截图    //return mTexture;  }  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);        //如果需要可以返回截图    //return mTexture;  }}

接下来,我们来调用这三个方法实现一个简单的截图的例子:

//定义图片保存路径  private string mPath1;  private string mPath2;  private string mPath3;  //相机  public Transform CameraTrans;  void Start()  {    //初始化路径    mPath1=Application.dataPath+"\\ScreenShot\\ScreenShot1.png";    mPath2=Application.dataPath+"\\ScreenShot\\ScreenShot2.png";    mPath3=Application.dataPath+"\\ScreenShot\\ScreenShot3.png";  }  //主方法,使用UGUI实现  void OnGUI()  {    if(GUILayout.Button("截图方式1",GUILayout.Height(30))){      CaptureByUnity(mPath1);    }    if(GUILayout.Button("截图方式2",GUILayout.Height(30))){      StartCoroutine(CaptureByRect(new Rect(0,0,1024,768),mPath2));    }    if(GUILayout.Button("截图方式3",GUILayout.Height(30))){      //启用顶视图相机      CameraTrans.camera.enabled=true;      //禁用主相机      Camera.main.enabled=false;      StartCoroutine(CaptureByCamera(CameraTrans.camera,new Rect(0,0,1024,768),mPath3));    }  }

在第三中截图方式中,博主在场景里放了一个名为TopCamera的摄像机,它垂直向下投影到游戏场景里,这样可以使玩家看到场景的顶视图。这里我们用这个相机来测试第三个方法,此时需要先激活该相机。场景设置如图:

我们下面来看三种方法截图的效果:

从截图的效果来看,第一种方法的效果是最好的,不过定制化是个问题。第二种方法效果一般吧,感觉这里TextureFormat没有选好吧。第三种效果基本达到了博主想要的要求,不过摄像机的投影范围似乎没有设计好。这里我们发现第二张截图会把编辑器的窗口渲染到里面,博主认为是程序运行的时候,即使将Game窗口放到最大,仍然会受到窗口的影响,后来博主就把程序编译成可执行文件,不过程序运行完之后,博主却没有找到对应的截图。后来查找了官方的API 才知道原因是这样的:

Description

Contains the path to the game data folder (Read Only).

The value depends on which platform you are running on:

Unity Editor: < path to project folder >/Assets Mac player: < path to player app bundle >/Contents iPhone player: <path to player app bundle >/< AppName.app >/Data Win player: <path to executablename_Data folder > Web player: The absolute url to the player data file folder (without the actual data file name)Flash: The absolute url to the player data file folder (without the actual data file name)

Note that the string returned on a PC will use a forward slash as a folder separator

显然,我们从这里可以知道Application.datapath在不同的平台上对应的位置。对于可执行(.exe,Windows平台)的文件,它对应在和应用程序对应的一个文件夹里,例如可执行文件的名字叫做UnityGame,那么对应的位置就是UnityGame_Data这个文件啦。所以博主的问题应该是出在没有在这里建一个ScreenShot的文件夹,希望大家以后做相关项目的时候注意一下吧。好了,这就是今天的内容了,希望大家喜欢啊。

每日箴言: 无所谓好或不好

,人生一场虚空大梦,韶华白首,不过转瞬。惟有天道恒

在,往复循环,不曾更改——慕容紫英。

喜欢我的博客请记住我的名字:秦元培,我博客地址是blog.csdn.net/qinyuanpei。

0 0
原创粉丝点击