android - 应用程序持久数据路径失败

标签 android file-writing persistent-storage

我已将 Unity 更新到 2017.4.0f1,并且我有关于保存一些信息的脚本 在类似文本的文件中。我用过

public static string savePath = Application.persistentDataPath + "/";

但我收到此错误

get_persistentDataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FileHandler' on game object 'DisplayHigh'.
See "Script Serialization" page in the Unity Manual for further details.
0x00000001412F26E6 (Unity) StackWalker::GetCurrentCallstack
0x00000001412F328F (Unity) StackWalker::ShowCallstack
0x00000001411B1BE0 (Unity) GetStacktrace
0x00000001406DB5F3 (Unity) DebugStringToFile
0x00000001406DBDD1 (Unity) DebugStringToFile
0x0000000140A267CF (Unity) ReportError
0x0000000140A27EEA (Unity) ThreadAndSerializationSafeCheckReportError
0x00000001414173D0 (Unity) Application_Get_Custom_PropPersistentDataPath
0x000000000DDC32EA (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Application:get_persistentDataPath ()
0x000000000DDC3145 (Mono JIT Code) [FileHandler.cs:9] FileHandler:.cctor () 
0x000000000DD511DE (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x00007FFFBEAF64CF (mono) [mini.c:4937] mono_jit_runtime_invoke 
0x00007FFFBEA48A49 (mono) [object.c:2623] mono_runtime_invoke 
0x00007FFFBEA4C39E (mono) [object.c:359] mono_runtime_class_init_full 
0x00007FFFBEAF5C09 (mono) [mini.c:4500] mono_jit_compile_method_inner 
0x00007FFFBEAF5E45 (mono) [mini.c:4556] mono_jit_compile_method_with_opt 
0x00007FFFBEAF60BB (mono) [mini.c:4775] mono_jit_runtime_invoke 
0x00007FFFBEA48A49 (mono) [object.c:2623] mono_runtime_invoke 
0x00000001407ECD19 (Unity) mono_runtime_invoke_profiled
0x0000000140A0EF95 (Unity) mono_runtime_object_init_exception
0x0000000140A2FBBB (Unity) scripting_unity_engine_object_new_and_invoke_default_constructor
0x00000001409EC099 (Unity) MonoBehaviour::RebuildMonoInstance
0x0000000140A08445 (Unity) RebuildAllScriptCaches
0x0000000140A0BD86 (Unity) MonoManager::EndReloadAssembly
0x0000000140A0C695 (Unity) MonoManager::ReloadAssembly
0x0000000140C9CC57 (Unity) ReloadAllUsedAssemblies
0x0000000140C8C628 (Unity) EditorSceneManager::RestoreSceneBackups
0x00000001411E8FBF (Unity) PlayerLoopController::EnterPlayMode
0x00000001411E98A6 (Unity) PlayerLoopController::SetIsPlaying
0x00000001411EA6F6 (Unity) Application::TickTimer
0x00000001414101DF (Unity) MainMessageLoop
0x0000000141411A9C (Unity) WinMain
0x0000000141E662B8 (Unity) __tmainCRTStartup

还有这个

UnityException: get_persistentDataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FileHandler' on game object 'DisplayHigh'.
See "Script Serialization" page in the Unity Manual for further details.
FileHandler..cctor () (at Assets/Scripts/FileHandler.cs:9)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for FileHandler
StatCheck.Update () (at

我真的很困惑和沮丧,你知道吗。

如有任何帮助,我们将不胜感激。

最佳答案

public static string savePath;
void Awake()
{

   MyClass.savePath = Application.persistentDataPath + "/";
}

我的类是包含此变量的 MonoBehavior,由于它是一个静态变量,我们需要将类名放在前面。

关于android - 应用程序持久数据路径失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043394/

相关文章:

android - Google Cast SDK RemoteMediaPlayer 网络视频转换,例如YouTube

matlab - 如何使用两个向量变量格式化打印语句?

python - 如何将数据保存到下一列

blackberry - 在黑莓中删除应用程序时删除持久对象

Android:防止嗅探(例如使用 CharlesProxy)SSL 流量

android - fragment 中的动画在旋转后不会重新启动

javascript - localStorage 的持久性如何?

javascript - 跨浏览器刷新在 JavaScript 对象中保留值

android - 当我使用 libwebp 和 ndk 在 android 2.x 上显示 webp 图像时出现内存不足错误

c++ - 在 C++ 中将整数写入 .txt 文件