简体   繁体   中英

write / add / rewrite a class at runtime c# unity

in my project i have some sort of level builder that create a new text file and save all my wanted data as readable json string in 1 line.

while the project run in web build or in unity it self i can read the levels from that text file and every thing working great, in mobile builds that doesn't work.

my question is: is there a way to create or add lines to a class at runtime?

for example write a new string in the class at run time that will stay there after run time is over?

No, there isn't. At least not at mobile platforms.

But you should be able to parse JSON on mobile platforms if you set the Api Compatibility Level to .Net 2.0, not .Net 2.0 Subset and disable Strip Engine Code in the Player Setting .

As @Tijmen said there is no way to change a C# class at runtime. But I see no reason to do so. Instead you should change the JSON string, write it to the file and recreate the level instance.

Looking at your code reveals that you are writing to Application.dataPath which is not writable in iOS player. So it should work when your are using Application.persistentDataPath .

Further on I would refrain from calling the folder Resources as this has a special meaning in Unity.

No there isn't, but you can make an ArrayList instead and put the info from the text file into the ArrayList. Then extract information from the ArrayList.

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