Unity截图并保存任意目录
本帖最后由 杨建辉 于 2013-2-26 16:40 编辑截图并保存至任意目录在游戏或者软件需求中经常会用到,现在就来分享下具体实现:
1.前提条件:仅限于--Pc and Mac Standalone
2.准备工作: ①找到System.Windows.Forms.dll:在unity的安装目录中找到它,如E:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0
②设置.NET 2.0集:Untiy默认是.NET 2.0 Subset。在Edit->Project Settings->Player->OtherSettings中修改。
3.具体实现:①任意打开一项目,新建Plugins文件夹,将找到的System.Windows.Forms.dll复制进去
②新建一脚本Screenshot.cs并拽至任一物体上。
③运行后,按Z键进行截图并保存。
4.Screenshot.cs:
using UnityEngine;
using System.Windows.Forms;
public class Screenshot : MonoBehaviour {
void Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
SaveFileDialog saveLog = new SaveFileDialog();
saveLog.InitialDirectory = "c:\\";
saveLog.Filter = "Image Files(*.JPG;*.BMP;*.PNG)|*.JPG;*.BMP;*.PNG|All files (*.*)|*.*";
DialogResult result = saveLog.ShowDialog();
if (result == DialogResult.OK) {
string path = saveLog.FileName;
UnityEngine.Application.CaptureScreenshot(path);
}
}
}
}
5.注意事项:①代码中所有的API均可在Msdn上查阅
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.savefiledialog.aspx
② EditorUtility.SaveFilePanel也可以实现相同功能,只不过必须在编辑器下才可以。
楼主很专业,写得很好! 犀利啊,楼主,赞一个{:5_412:} 牛B的一塌糊涂! 很不错 楼主是超人 难得一见的好帖 不错不错 LZ真是人才 楼主是超人
页:
[1]