Question

I need to convert a byte [] to string in C# and Unity without losing much of a Frame Per Second. I'm currently using the WWW class to fetch the text file, but when I use WWW.text, as the file is too large and causes a drop in FPS. Is there any way to make the conversion incrementally, without delay too long.

WWW file = new WWW(path);
yield return file;
string text= file.text;

I use this to read an full file(one .OBJ) and i need to read all text. Line to line or all text to one string to split after. To read text without drop in FPS, i make this.

WWW file = new WWW(path);
yield return file;
byte[] text= file.bytes;

if file are to much big, WWW.text, drop FPS, then i need use WWW.bytes. In Unity useyield return null; to process another frame. If process need too much time to execute then drop FPS(in case WWW.text);

Ex file: http://people.sc.fsu.edu/~jburkardt/data/obj/cessna.obj

Was it helpful?

Solution

I guess WWW store the request result as a bytes array internally, and convert it to a string when you access the text field (is this really a field then ?).

So, no matter what, converting that bytes to string will require to allocate memory and use the proper encoding to process that byte array.

Your best bet is to extract the data directly from the byte array, without converting it to a big string first.

EDIT

Alternatively, you can wrap the byte array in a MemoryStream, and wrap it again in a StreamReader with the appropriate encoding (OBJ files are plain ASCII I guess).

Your file is gonna be converted incrementally, as you read it.

Use that yield pattern in your processing loop, and it should be good !

OTHER TIPS

Try in this way:

WWW file = new WWW(path);
while(!file.isDone)
{
    yield return new WaitForSeconds(0.1f);
}
string text = file.text;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top