I have a problem use Application.dataPath
我在使用Application.dataPath时遇到问题
The Error:
UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'SaveAndLoadMenu' on game object 'Save And Load Menu'.
See "Script Serialization" page in the Unity Manual for further details.
FilePaths..cctor () (at Assets/_MAIN/Scripts/Core/IO/FilePaths.cs:8)
Rethrow as TypeInitializationException: The type initializer for 'FilePaths' threw an exception.
不允许从MonoBehaviour构造函数(或实例字段初始值设定项)调用错误:UnityException:Get_DataPath,请改为在唤醒或启动中调用它。从游戏对象“保存并加载菜单”上的MonoBehaviour“SaveAndLoadMenu”调用。有关更多详细信息,请参阅Unity手册中的“脚本序列化”页面。FilePath..cctor()(位于Assets/_Main/Script/Core/IO/FilePath s.cs:8)作为TypeInitializationException重新引发:‘FilePath’的类型初始值设定项引发异常。
The Error indicate FilePaths.cs at
public static readonly string root = $"{Application.dataPath}/gameData/"; this line
该错误指示FilePath s.cs位于公共静态只读字符串根为$“{Application.dataPath}/gameData/”;此行
And Menupage.cs inherited MonoBehaviour
和Menupage.cs继承了MonoBehaviour
public class FilePaths
{
private const string HOME_DIRECTORY_SYMBOL = "~/";
* public static readonly string root = $"{Application.dataPath}/gameData/";* // Error
//Runtime Path
public static readonly string gameSaves = $"{runtimePath}Save Files/";
public static readonly string resources_font = "Fonts/";
public static readonly string resources_graphics = "Graphics/";
public static readonly string resources_backgroundImages = $"{resources_graphics}BG/";
public static readonly string resources_backgroundVideos = $"{resources_graphics}BG Videos/";
public static readonly string resources_blendTextures = $"{resources_graphics}Transition Effects/";
public static readonly string resources_audio = "Audio/";
public static readonly string resources_sfx = $"{resources_audio}SFX/";
public static readonly string resources_voices = $"{resources_audio}Voices/";
public static readonly string resources_bgm = $"{resources_audio}BGM/";
public static readonly string resources_ambience = $"{resources_audio}Ambience/";
public static readonly string resources_dialogueFiles = $"Dialogue Files/";
public static string GetPathToResource(string defaultPath, string resourceName)
{
if(resourceName.StartsWith(HOME_DIRECTORY_SYMBOL))
{
return resourceName.Substring(HOME_DIRECTORY_SYMBOL.Length);
}
return defaultPath + resourceName;
}
public static string runtimePath
{
get
{
#if UNITY_EDITOR
return "Assets/appdata/";
#else
return Application.persistentDataPath + "/appdata/";
#endif
}
}
}
public class SaveAndLoadMenu : MenuPage
{
public static SaveAndLoadMenu Instance {get; private set;}
const int MAX_FILES = 99;
string savePath = FilePaths.gameSaves;
int currentPage = 1;
bool loadedFilesForFirstTime = false;
public enum MenuFunction {save, load}
public MenuFunction menuFunction = MenuFunction.save;
public SaveLoadSlot[] saveSlots;
int slotsPerPage => saveSlots.Length;
public Texture emptyFileImage;
void Awake()
{
Instance = this;
}
public override void Open()
{
base.Open();
if(!loadedFilesForFirstTime)
PopulateSaveSlotsForPage(currentPage);
}
void PopulateSaveSlotsForPage(int pageNumber)
{
int startingFile = ((currentPage - 1) * slotsPerPage) + 1;
int endingFile = startingFile + slotsPerPage -1;
for(int i = 0; i < slotsPerPage; i++)
{
int fileNum = startingFile + i;
SaveLoadSlot slot = saveSlots[i];
if(fileNum < MAX_FILES)
{
slot.root.SetActive(true);
string filePath = $"{FilePaths.gameSaves}{fileNum}{VNGameSave.FILE_TYPE}";
slot.fileNumber = fileNum;
slot.filePath = filePath;
slot.PopulateDetails(menuFunction);
}
else
{
slot.root.SetActive(false);
}
}
}
}
I was tried
我被试过了
public static string Root { get; private set; }
private void Awake()
{
Root = $"{Application.dataPath}/gameData/";
}
更多回答
Do you still have the same error after trying?
尝试之后还会有同样的错误吗?
@shingo yeah it still error
@Shingo是的它仍然是错误的
@shingo Do you mean delete that line and add public static string Root { get; private set; } private void Awake() { Root = $"{Application.dataPath}/gameData/"; } right? i will try it thank you for reply
@Shingo您的意思是删除该行并添加公共静态字符串Root{Get;Private Set;}私有空醒(){Root=$“{Application.dataPath}/gameData/”;}对吗?我会试试看的,谢谢你的回复
... My first question is: Do you still have the same error after trying? If it changed, you need to show the new error instead of the one before trying.
...我的第一个问题是:你在尝试后仍然有同样的错误吗?如果它改变了,你需要显示新的错误,而不是尝试之前的错误。
@shingo I solved this problem! Thank you so much
@Shingo我解决了这个问题!非常感谢
优秀答案推荐
As the error is telling you you can not use Application.dataPath
at field initialization
错误提示您无法在字段初始化时使用Application.dataPath
public static readonly string root = $"{Application.dataPath}/gameData/";
what you can do though is e.g. using
不过,你可以做的是,例如使用
public static string root { get; private set ;}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Init()
{
root = $"{Application.dataPath}/gameData/";
}
See [RuntimeInitializeOnLoadMethod]
请参见[RounmeInitializeOnLoadMethod]
更多回答
我是一名优秀的程序员,十分优秀!