Hi Unity Community,
I have created hundreds of levels in my own level editor and I have them stored in a list inside a custom class. I then serialize this class out to a binary file which I load in my main project and retrieve the list of levels.
Now I’m ready to start deploying, but when I build and test on iOS, I am having difficulty accessing the level file. I thought I might be able to store it in Resources, but since the custom class I made is serializable, I can’t make it a subclass of UnityEngine.Object or MonoBehaviour. As far as I’ve been able to find out, this means I can’t use Resources.Load to pull it back out of Resources.
Is there a way I can store a non-UnityEngine.Object class file in my build? Feels like there must be… I’m thinking I can redesign my level structure to somehow live inside an object in my scene, or maybe have the program download the level file from a server, but it would be cleaner if I could just have it be part of the main deployment.
Thanks much in advance for any help/advice!
Well, found some info in another post and came up with a solution. I’m storing my file in Resources with the .bytes extension. When loading, my game checks Application.PersistentDataPath to see if the file is there. If it is not, it loads the file from Resources as a TextAsset (if you can believe that). Then I write a file to PersistentDataPath from the TextAsset.bytes and voila.
Still seems a bit much to go through, but I’m storing a <1MB in Resources that should only be loaded one time.
Here’s a little code:
public void CreateLevelFile()
FileStream file = File.Create(fileName);
TextAsset tempAsset = Resources.Load<TextAsset>("LevelData");
Debug.Log("WRITING FILE " + fileName);
If anyone has a nicer way to do this, would be great to hear. For now I’ll run with this.
Here’s the other post I found for some more elaboration:
Read Binary Files From Resources