我有以下代码在Windows构建游戏中创建文本文件,但同样不是在Mac构建游戏中创建任何文件。我没有Mac来调试代码,但我可以在外部公共Mac电脑上玩游戏。我也尝试使用Application.dataPath
,但无论如何都没有在MacOS中创建文件
using UnityEngine;
using System;
using System.IO;
/// <summary>
/// This class serves as the single interface for all file writing.
/// The idea is to report all important events so that the game's
/// actions can be coded and compared to viewed reactions from video.
/// </summary>
public static class FileManagement
{
private static bool wasInit = false;
private static string FILENAME;
private static string DUMPNAME;
// Get the date to use as the file name.
public static void init()
{
wasInit = true;
// To ensure files are not overwritten and are easily identifiable, we will name them with the current date and time.
int day = DateTime.Now.Day;
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;
int hour = DateTime.Now.Hour;
int minute = DateTime.Now.Minute;
int second = DateTime.Now.Second;
FILENAME = (GameInfo.gameTitle + "-" + month + "-" + day + "-" + year + "-" + hour + "-" + minute + "-" + second);
// The dump file holds all the emotion measurements for each frame. Put in a separate file to not clog other data.
DUMPNAME = FILENAME + "-ENGAGEMENT-DUMP.txt";
FILENAME += ".txt";
}
// Helper to get timestamp string.
private static string getTime()
{
return "[" + Time.time + "] ";
}
// Helper to open and write to the file. Keeping all the possible errors to one point.
private static void print(string message)
{
if (!wasInit)
{
init();
}
using (StreamWriter file = new StreamWriter(FILENAME, true))
{
// The using command here automatically closes and flushes the file.
file.WriteLine(getTime() + message);
}
}
// Public version to accept any sort of message.
public static void printToFile(string message)
{
print(message);
}
}
答案 0 :(得分:0)
这似乎是在Mac中使用文件写入权限的问题。将文件写入由Application.persistentDataPath
主导的文件夹中修复了问题。