在我重新启动游戏之前,streamWriter 不会应用更改
streamWriter does not apply changes until I restart my game
我正在 Unity 中制作一款游戏,其编辑器关卡将关卡数据写入 .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 中的资源
不过
When the player creates a new level i use this
请注意,其中 none 将在构建的应用程序中运行!
构建应用后,Resources
为 read-only。不管怎样,来自 Best practices -> Resources
Don't use it!
您可能更愿意将默认文件存储在 StreamingAssets
and use Application.streamingassetsPath
and then later in a build use Application.persistentDataPath
中,然后回退到 streamingAssetsPath
以供只读(同样 StreamingAssets
在构建后是 read-only)
比如喜欢
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);
}
}
我正在 Unity 中制作一款游戏,其编辑器关卡将关卡数据写入 .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
.
不过
When the player creates a new level i use this
请注意,其中 none 将在构建的应用程序中运行!
构建应用后,Resources
为 read-only。不管怎样,来自 Best practices -> Resources
Don't use it!
您可能更愿意将默认文件存储在 StreamingAssets
and use Application.streamingassetsPath
and then later in a build use Application.persistentDataPath
中,然后回退到 streamingAssetsPath
以供只读(同样 StreamingAssets
在构建后是 read-only)
比如喜欢
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);
}
}