Is there way to read string from NativeArray or NativeSlice?

I use UnityWebRequest to download a NativeArray, and use in in Jobsytem, is there way to read string from NativeArray?

[ReadOnly]
public NativeArray<byte> SrcBytes;


byte srcNameLen = SrcBytes.Slice ( offset, 1 ).SliceConvert<byte> ()[ 0 ];
offset += 1;
NativeSlice<byte> srcName = SrcBytes.Slice ( offset, srcNameLen ).SliceConvert<byte> ();
offset += srcNameLen;

the scrName is about the string , how to convert to string in mainthread?

https://stackoverflow.com/questions/1003275/how-to-convert-utf-8-byte-to-string

NativeArray has a ToArray() method.

Ok, but i found in job system that don’t allow managed types, so how do i return string to main thread?

8436368--1117547--upload_2022-9-13_22-7-14.png

You run the Job from the main thread. I suppose that’s where you also pass in the NativeArray (or Slice). So after the job is complete, you’d get the job.srcName (assuming it’s a public field or property) and use that.

thanks reply,
I didn’t know how long it was until I read it, so how to delcare the variable srcname in job ?

It sounds like what you really want is FixedStringXBytes from the Collections package.

ok, thanks a lot, i’ll check this out