unity手动选择文件夹保存文件

来源:互联网 发布:卷积神经网络 算法 编辑:程序博客网 时间:2024/06/06 02:52

闲话不扯,直接开始正题:

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("小于了");
        //}


    }

}

阅读全文
0 0
原创粉丝点击