我正在制作一个统一的游戏,它有一个编辑器级别,将级别数据写入一个.txt

我的问题是,对文件的更改没有正确应用,因为我只有在重新启动游戏时才能看到它们.

当玩家创建一个新关卡时,我会使用:

TextAsset txtFile = Resources.Load<TextAsset>("Levels");
StreamWriter streamWriter = new StreamWriter(new FileStream("Assets/Resources/" + levelsManager.filename + ".txt", FileMode.Truncate, FileAccess.Write));

在方法的末尾,我关闭了StreamWriter.

    streamWriter.Flush();
    streamWriter.Close();
    streamWriter.Dispose();

此外,如果用户创建多个级别,则只保存最后一个级别.

将文件模式更改为apend不起作用,因为在重新启动游戏并创建关卡后,它还会再次创建存储的关卡.

有没有办法刷新文档,这样我就不必每次创建关卡都重新启动游戏?

提前谢谢你.

推荐答案

只需使用AssetDatabase.Refresh刷新Unity中的assets资源 .


However

当玩家创建一个新关卡时,我使用这个

请注意,这些都不适用于内置应用程序!

构建应用程序后,Resourcesread-only.不管怎样,请注意Best practices -> Resources

Don't use it!

您可能更希望将默认文件存储在StreamingAssets中,使用Application.streamingassetsPath,然后在构建中使用Application.persistentDataPath,并回退到streamingAssetsPath以实现只读(同样,构建后StreamingAssets是只读的)

比如说

public string ReadlevelsFile()
{
    try
    {
#if UNITY_EDITOR
        // In the editor always use "Assets/StreamingAssets"
        var filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
#else
        // In a built application use the persistet data
        var filePath = Path.Combine(Application.persistentDataPath, "Levels.txt");
        
        // but for reading use the streaming assets on as fallback
        if (!File.Exists(filePath))
        {
            filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
        }
#endif
        return File.ReadAllText(filePath);
    }
    catch (Exception e)
    {
        Debug.LogException(e);
        return "";
    }
}

public void WriteLevelsFile(string content)
{
    try
    {
#if UNITY_EDITOR
        // in the editor always use "Assets/StreamingAssets"
        var filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
        // mke sure to create that directory if not exists yet
        if(!Directory.Exists(Application.streamingAssetsPath))
        {
            Directory.CreateDirectory(Application.streamingAssetsPath);
        }
#else
        // in a built application always use the persistent data for writing
        var filePath = Path.Combine(Application.persistentDataPath, "Levels.txt");
#endif
        
        File.WriteAllText(filePath, content);

#if UNITY_EDITOR
        // in Unity need to refresh the data base
        UnityEditor.AssetDatabase.Refresh();
#endif
    }
    catch(Exception e)
    {
        Debug.LogException(e);
    }
}

Csharp相关问答推荐

当通过Google的Gmail Api发送邮件时,签名会产生dkim = neutral(正文散列未验证)'

Blazor-从数据库内部服务器提取错误

如何定义EFCore中的多个穿透

更新产品但丢失产品ASP.NET Core的形象

.NET HttpClient、JsonSerializer或误用的Stream中的内存泄漏?

与C#中的Zip列表并行

有没有办法在WPF文本框中添加复制事件的处理程序?

静态对象构造顺序

MS Graph v5.42.0:在寻呼消息时更改页面大小

如何通过属性初始化器强制初始化继承记录内的属性?

正在try 从Blazor中的API读取JSON

RabbitMQ群集的MassTransit配置问题

BFF到具有AAD/Entra ID B2C的内部API(.NET/ASP.NET核心/标识.Web)

Azure函数-在外部启动类中生成配置时出错

如何从另一个类的列表中按ID取值

ReadOnlyMemory访问基础索引的替代方案

使用DI实例化带有动态参数的服务?

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果

SqlException:无法打开数据库.升级到Dotnet 8后-数据库兼容性版本-非EFCore兼容性级别

使用生产环境调试我的应用程序的快速方法