Skip to main content
Bumped by Community user
Cleanup
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Passing a string as argument to a Unity Jobs systemJob System

I have a Job that needs to take some string from main thread and Useuse it Locally within itself locally.

Considering that the Job systemSystem cannot directly accept the Stringstring as an argument, is there any workroundworkaround to circumvent this limit?

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

For now iI just have an empty strig?string as output from Debug.Logthe .Debug.Log below:

                string myString = "string";
                StringTest job = new StringTest
                {
                    // first iI 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);}
                  };
                    }

Passing a string as argument to a Unity Jobs system

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                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);}
                  };
                    

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);
    }
}
Rollback to Revision 2
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Solved*

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                string myString = "string";
                byte[]StringTest bytesjob = Encoding.ASCII.GetBytes(myString);
   new StringTest
            StringTest job = new StringTest{
                {
     // first i need a way to copy a string to array of bytes 
                    inStr = new NativeArray<byte>(bytesmyString.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);}
                  };
                    

Solved*

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                string myString = "string";
                byte[] bytes = Encoding.ASCII.GetBytes(myString);
                StringTest job = new StringTest
                {
                   
                    inStr = new NativeArray<byte>(bytes, 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);}
                  };
                    

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                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);}
                  };
                    
added 14 characters in body
Source Link

Solved*

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                string myString = "string";
                StringTestbyte[] jobbytes = new StringTestEncoding.ASCII.GetBytes(myString);
                {StringTest job = new StringTest
                {
    // first i need a way to copy a string to array of bytes  
                    inStr = new NativeArray<byte>(myString.Lengthbytes, 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);}
                  };
                    

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                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);}
                  };
                    

Solved*

I have a Job that needs to take some string from main thread and Use it Locally within itself .

Considering that the Job system cannot directly accept the String as an argument is there any workround to circumvent this limit?

Can we move the string to NativeArray and then repackage it back into the string inside the jobs and use it?

For now i just have an empty strig? as output from Debug.Log .

                string myString = "string";
                byte[] bytes = Encoding.ASCII.GetBytes(myString);
                StringTest job = new StringTest
                {
                   
                    inStr = new NativeArray<byte>(bytes, 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);}
                  };
                    
added 83 characters in body
Source Link
Loading
Source Link
Loading