簡體   English   中英

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

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

解釋

我正在將 a.txt 文件加載到列表中,並將其中一個元素設置為我的 Firebase 數據庫 defaultInstance RootReference 中的子元素。 它需要類型字符串,這就是我傳遞的內容。

問題

當我傳遞從 Resources.Load 或 StreamReader 命令收集的字符串時,沒有任何內容添加到數據庫中。 任何其他字符串都按預期工作。

我可以在不相關對象的腳本中使用 Resources.Load,而不會破壞 Firebase 功能。 只有當我嘗試 Resources.Load 將傳遞給數據庫的特定字符串時才會發生。

代碼截圖

    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 中是否有任何東西會以不同方式處理字符串? - 只是因為它是從資源中收集的。

這里的答案原來是 "\n" 本身是不夠的。 文本資源文件本身是 windows "\r\n" 格式,所以用 "\n" 分割留下不可見的 "\r" 字符(當你在調試模式下停止這段代碼時可以看到)。

修復選項 1:

將資源文本文件修改為只有“\n”行結尾。 您可以在任何像樣的文本編輯器(例如 visual studio)中執行此操作。
請注意,在 windows 上,如果您將文件置於源代碼管理之下,通常這些文件將以 windows 行結尾(“\r\n”)檢出,即使它們保存在僅以“\n”結尾的遠程存儲庫中。 要解決這個問題,例如對於 git,請修改 your.gitattributes 文件以添加自定義行結束處理異常,例如:

資產/資源/MyFile.txt eol=lf

修復選項 2:

如果您和您的所有協作者永遠只在 windows 上,您可以將代碼修改為 text.Split("\r\n"[0]) ,這將正確划分 windows 文本文件的字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM