简体   繁体   中英

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. 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. Any other string works as expected.

I can use Resources.Load in the script on unrelated objects without breaking the Firebase functionality. It only happens when I try to Resources.Load the specific string that will be passed to the database.

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? -just because it's collected from resources.

The answer here turned out to be that "\n" by itself was not enough. 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).

Fix option 1:

Modify the resource text file to have "\n" line endings only. You can do this in any decent text editor (eg 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. To fix that, eg for git, modify your.gitattributes file to add custom line ending handling exceptions like:

Assets/Resources/MyFile.txt eol=lf

Fix option 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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