【问题标题】:Is Unity's Resources.load incompatible with Firebase?Unity 的 Resources.load 与 Firebase 不兼容吗?
【发布时间】:2022-03-13 04:29:15
【问题描述】:

说明

我正在将 .txt 文件加载到列表中,并将其中一个元素设置为我的 Firebase 数据库 defaultInstance RootReference 中的子元素。它需要类型字符串,这就是我要传递的内容。

问题

当我传递从 Resources.Load 或 StreamReader 命令收集的字符串时,不会向数据库添加任何内容。任何其他字符串都按预期工作。

我可以在脚本中对不相关的对象使用 Resources.Load,而不会破坏 Firebase 功能。仅当我尝试使用 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);

问题

Unity 或 Firebase 中有什么东西可以区别对待字符串吗? - 只是因为它是从资源中收集的。

【问题讨论】:

    标签: string list firebase unity3d firebase-realtime-database


    【解决方案1】:

    这里的答案原来是“\n”本身是不够的。文本资源文件本身是 windows "\r\n" 格式,因此被 "\n" 分割会留下不可见的 "\r" 字符(在调试模式下停止此代码时可见)。

    修复选项 1:

    修改资源文本文件以仅具有“\n”行结尾。您可以在任何体面的文本编辑器(例如 Visual Studio)中执行此操作。
    请注意,在 Windows 上,如果您将文件置于源代码控制之下,通常这些文件将以 Windows 行结尾 ("\r\n") 签出,即使它们保存在远程存储库中仅以 "\n" 结尾。为了解决这个问题,例如对于 git,修改您的 .gitattributes 文件以添加自定义行尾处理异常,例如:

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

    修复选项 2:

    如果您和您的所有合作者永远只在 Windows 上,您可以将代码修改为 text.Split("\r\n"[0]) ,它将正确划分 Windows 文本文件的字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多