Skip to main content
5 of 5
Cleanup
DMGregory
  • 141k
  • 23
  • 258
  • 401

Passing a string as argument to Unity Job System

I have a Job that needs to take some string from main thread and use it locally.

Considering that the Job System cannot directly accept the string as an argument, is there any workaround to circumvent this limit?

Can we move the string to a NativeArray<bytes> and then repackage it back into the string inside the job and use it?

For now I just have an empty string as output from the Debug.Log below:

string myString = "string";
StringTest job = new StringTest
{
    // first I need a way to copy a string to array of bytes. 
    inStr = new NativeArray<byte>(myString.Length, Allocator.TempJob),
};
JobHandle handle =  joba.Schedule();
handle.Complete();   
public struct StringTest : IJob
{
    public NativeArray<byte> inStr;

    public void Execute(){
        string s = Encoding.ASCII.GetString(inStr.ToArray());
        Debug.Log("debug log from Job " +s);
    }
}