Unity手动选择文件夹保存文件
发表于2017-12-06
很多人打包文件时候都不会太注意文件的保存位置,为此下面就给大家介绍下如何手动选择文件夹保存文件。
1: 找到unity自带的 System.Windows.Forms.dll 文件,我的是在(C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\System.Windows.Forms.dll )
2:在项目中新建文件夹,名字叫 Plugins , 将 System.Windows.Forms.dll 添加进去。
3:设置unity :File - Build Settings... - Player Setting... - Other Settings - Api Compatibility Level 设置成 .NET 2.0
4:创建脚本 System_Windows_Froms.cs
using System; using System.Collections; using System.Text.RegularExpressions; using System.Windows.Forms; using UnityEngine; public class System_Windows_Froms : MonoBehaviour { string CompentPath; //接受选择文件的路径 string UnityPath; //接受转成功后的路径 也就是Unity所需要的路径 //自定义文件保存文件夹; public void SaveCutScreenPath() { FolderBrowserDialog fb = new FolderBrowserDialog(); //创建控件并实例化 fb.Description = "选择文件夹"; fb.RootFolder = Environment.SpecialFolder.MyComputer; //设置默认路径 fb.ShowNewFolderButton = false; //创建文件夹按钮关闭 //如果按下弹窗的OK按钮 if (fb.ShowDialog() == DialogResult.OK) { //接受路径 CompentPath= fb.SelectedPath; } //将路径中的 \ 替换成 / 由于unity路径的规范必须转 UnityPath= CompentPath.Replace(@"\", "/"); print(UnityPath); //如果 \ 比较多的话 //if (UnityPath.IndexOf("/") > 2) //{ //UnityPath = CompentPath+ "/"; //print("大于了"); //} //else { //print("小于了"); //} } }