Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity DataPath property w/ hard coded macOS path
/// <summary>
/// The root data folder. Used by the entire application to read and write
/// data, not just used by the Settings class. This is an absolute path.
/// </summary>
#if UNITY_EDITOR
// Each product has its own folder in tmp when running in the editor.
// This allows audio and other debug settings to be separate from
// standalone builds on the same development machine.
// NOTE: The hard-coded forward slash should work on Windows, Linux, and OSX.
public static string DataPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "tmp/settings/" + Application.productName);
#else
// macOS
// ~/Library/Application Support/Your\ Company\ Name/Jammer/
//
// Linux
// ~/.config/unity3d/Your\ Company\ Name/Jammer/
//
// Windows
// ~/AppData/LocalLow/Your\ Company\ Name/Jammer/
#if UNITY_STANDALONE_OSX
public static string DataPath = GetOSXDataPath();
private static string GetOSXDataPath() {
if (string.IsNullOrEmpty(macOSDataPath)) {
macOSDataPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library/Application Support/" + Application.companyName + "/" + Application.productName);
try {
string filename = System.IO.Path.Combine(macOSDataPath, ".contents");
string contents = Application.productName;
Log.Debug(string.Format("Settings.GetOSXDataPath() writing to canary file {0}", filename));
if (!System.IO.Directory.Exists(macOSDataPath)) {
System.IO.Directory.CreateDirectory(macOSDataPath);
}
System.IO.File.WriteAllText(filename, contents);
}
catch (System.Exception ex) {
macOSDataPath = Application.persistentDataPath;
Log.Error(string.Format("Settings.GetOSXDataPath() failed: {0}, falling back to Application.persistentDataPath {1}", ex.Message, macOSDataPath));
}
}
else {
macOSDataPath = DataPath;
}
return macOSDataPath;
}
private static string macOSDataPath;
#else
public static string DataPath = Application.persistentDataPath;
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment