C# 未在Unity MAC build中创建文件
我有下面一段代码,在Windows build游戏中创建文本文件,但在Mac build游戏中不创建任何文件。我没有Mac电脑来调试代码,但我可以在外部的公用Mac电脑上玩这个游戏。我也尝试过使用C# 未在Unity MAC build中创建文件,c#,macos,file,unity3d,C#,Macos,File,Unity3d,我有下面一段代码,在Windows build游戏中创建文本文件,但在Mac build游戏中不创建任何文件。我没有Mac电脑来调试代码,但我可以在外部的公用Mac电脑上玩这个游戏。我也尝试过使用Application.dataPath,但在MacOS中无法创建该文件 using UnityEngine; using System; using System.IO; /// <summary> /// This class serves as the single interfac
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);
}
}
使用UnityEngine;
使用制度;
使用System.IO;
///
///此类用作所有文件写入的单一接口。
///这样做的目的是报告所有重要事件,以便比赛顺利进行
///可以对动作进行编码,并与视频中观看的反应进行比较。
///
公共静态类文件管理
{
私有静态boolwasinit=false;
私有静态字符串文件名;
私有静态字符串转储名;
//获取要用作文件名的日期。
公共静态void init()
{
wasInit=true;
//为确保文件不被覆盖且易于识别,我们将使用当前日期和时间对其进行命名。
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;
文件名=(GameInfo.gameTitle+“-”+月+“-”+日+“-”+年+“-”+小时+“-”+分钟+“-”+秒);
//转储文件保存每个帧的所有情绪测量值。放在一个单独的文件中,以避免阻塞其他数据。
DUMPNAME=FILENAME+“-ENGAGEMENT-DUMP.txt”;
文件名+=“.txt”;
}
//帮助器获取时间戳字符串。
私有静态字符串getTime()
{
返回“[”+Time.Time+”];
}
//帮助器打开并写入文件。将所有可能的错误保留在一个点上。
私有静态无效打印(字符串消息)
{
if(!wasInit)
{
init();
}
使用(StreamWriter文件=新StreamWriter(文件名,true))
{
//此处的using命令将自动关闭并刷新文件。
file.WriteLine(getTime()+消息);
}
}
//公共版本以接受任何类型的消息。
公共静态无效打印文件(字符串消息)
{
打印(信息);
}
}
这是我的mac和pc版本的文件夹结构>>
这似乎是在Mac中使用文件写入权限的问题。在由
应用程序管理的文件夹中写入文件。persistentDataPath
修复了该问题。使用应用程序。persistentDataPath
修复了该问题:)