我已经完成了一个导入和导出脚本,该脚本导入/导出一个包含所有当前生成的对象及其XYZ坐标的文件.然而,仍然存在一个问题.我现在在导出时所做的是将文件自动保存在"Application.persistentDataPath"中,但我希望弹出一个窗口,请求您将文件保存在PC上的什么位置,以及导入文件的位置.

以下是导入和导出的代码:

public void WriteString(string message)
{
    //    string path = Application.persistentDataPath + "/"+display.text+".txt";
    string path = Application.persistentDataPath + "/txt.txt";
    //Write some text to the test.txt file
    StreamWriter writer = new StreamWriter(path, true);
    writer.WriteLine(message);
    writer.Close();
    StreamReader reader = new StreamReader(path);

    //Print the text from the file
    Debug.Log(reader.ReadToEnd());
    reader.Close();
}
public void ReadString()
{
    // string path = Application.persistentDataPath + "/"+display.text+".txt";
    string path = Application.persistentDataPath + "/txt.txt";
    StreamReader reader = new StreamReader(path);
    string line = "";
    ...
}

推荐答案

有一个内置的编辑器实用程序,名为EditorUtility.SaveFilePanel请参阅文档here.

对于导入,请使用EditorUtility.OpenFilePanel.

请注意,这些方法将一直阻止,直到用户 Select 路径并在 Select 路径时返回该路径.如果取消操作,则返回空字符串.

Csharp相关问答推荐

C#相同名称的枚举方法和normal方法,参数类型不同

如何在NServicebus中配置学习传输的文件夹(NService bus 8)

. NET在上一个操作完成之前,在此上下文实例上启动了第二个操作

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

将现有字段映射到EFCore中的复杂类型

最新的Mediatr和具有同步方法的处理程序Handle:并非所有代码路径都返回值"

如何解决提交按钮后 Select 选项错误空参照异常

ASP.NET核心MVC SqlException:违反主键约束';PK_USER';.无法在对象';数据库中插入重复的密钥.用户';

.NET:从XPath定位原始XML文档中的 node

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

HttpClient SendAsync与多线程环境中的ArchiveZip

.NET 6:如何防止系统生成的日志(log)?

未在Windows上运行的Maui项目

在同一个捕获中可以有多种类型的异常吗?

当我手动停止和关闭系统并打开时,Windows服务未启动

忽略Visual Studio代码中的StyleCop规则

如何在特定时间间隔运行多个后台任务?

.NET6最小API:操作.MapGet之后的响应

将ValueTask发送给调用者