简体   繁体   English

Unity 的 Resources.load 是否与 Firebase 不兼容?

[英]Is Unity's Resources.load incompatible with Firebase?

Explanation解释

I'm loading a.txt file into a List and setting one of the elements as a child in my Firebase Database defaultInstance RootReference.我正在将 a.txt 文件加载到列表中,并将其中一个元素设置为我的 Firebase 数据库 defaultInstance RootReference 中的子元素。 It expects the type string and that's what I'm passing.它需要类型字符串,这就是我传递的内容。

Problem问题

Nothing is added to the database when I pass a string that was collected from a Resources.Load or StreamReader command.当我传递从 Resources.Load 或 StreamReader 命令收集的字符串时,没有任何内容添加到数据库中。 Any other string works as expected.任何其他字符串都按预期工作。

I can use Resources.Load in the script on unrelated objects without breaking the Firebase functionality.我可以在不相关对象的脚本中使用 Resources.Load,而不会破坏 Firebase 功能。 It only happens when I try to Resources.Load the specific string that will be passed to the database.只有当我尝试 Resources.Load 将传递给数据库的特定字符串时才会发生。

Code Screenshot代码截图

    string _roomCode = "RoomNotSelected";

    // When using this block, Firebase won't create/write _roomCode in the database.
    // Otherwise _roomCode ("RoomNotSelected") is succeffully added as a child in the database at rootReference.
    TextAsset txtRooms = (TextAsset)Resources.Load("Room Names Files");     // I also tried Streamreader. It didn't work either.
    List<string> roomList = new List<string>(txtRooms.text.Split("\n"[0])); // The text file is one room name per line. Split each line into a list.
    _roomCode = roomList[0]; // Use the first element for testing.
    Debug.Log(_roomCode);    //Prints "TestRoom" 

    FirebaseDatabase.DefaultInstance.RootReference.Child(_roomCode).Child("Match").SetRawJsonValueAsync(jsonMatch);

Question问题

Is there anything in Unity or Firebase that would treat a string differently? Unity 或 Firebase 中是否有任何东西会以不同方式处理字符串? -just because it's collected from resources. - 只是因为它是从资源中收集的。

The answer here turned out to be that "\n" by itself was not enough.这里的答案原来是 "\n" 本身是不够的。 The text resource files themselves were in windows "\r\n" format so splitting by "\n" left invisible "\r" characters (viewable when you stop on this code in debug mode).文本资源文件本身是 windows "\r\n" 格式,所以用 "\n" 分割留下不可见的 "\r" 字符(当你在调试模式下停止这段代码时可以看到)。

Fix option 1:修复选项 1:

Modify the resource text file to have "\n" line endings only.将资源文本文件修改为只有“\n”行结尾。 You can do this in any decent text editor (eg visual studio).您可以在任何像样的文本编辑器(例如 visual studio)中执行此操作。
Beware that on windows if you have the files under source control, often those files will be checked out with windows line endings ("\r\n") even though they are saved in the remote repository with "\n" endings only.请注意,在 windows 上,如果您将文件置于源代码管理之下,通常这些文件将以 windows 行结尾(“\r\n”)检出,即使它们保存在仅以“\n”结尾的远程存储库中。 To fix that, eg for git, modify your.gitattributes file to add custom line ending handling exceptions like:要解决这个问题,例如对于 git,请修改 your.gitattributes 文件以添加自定义行结束处理异常,例如:

Assets/Resources/MyFile.txt eol=lf资产/资源/MyFile.txt eol=lf

Fix option 2:修复选项 2:

If you and all your collaborators will always ever only be on windows, you can modify the code to text.Split("\r\n"[0]) which will properly divide up the characters for windows text files.如果您和您的所有协作者永远只在 windows 上,您可以将代码修改为 text.Split("\r\n"[0]) ,这将正确划分 windows 文本文件的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM