Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Is there way to read string from NativeArray or NativeSlice?

Discussion in 'Entity Component System' started by Chaoslife, Sep 13, 2022.

  1. Chaoslife

    Chaoslife

    Joined:
    Aug 16, 2016
    Posts:
    13
    I use UnityWebRequest to download a NativeArray<byte>, and use in in Jobsytem, is there way to read string from NativeArray<byte>?

    Code (CSharp):
    1.  
    2. [ReadOnly]
    3. public NativeArray<byte> SrcBytes;
    4.  
    5.  
    6. byte srcNameLen = SrcBytes.Slice ( offset, 1 ).SliceConvert<byte> ()[ 0 ];
    7. offset += 1;
    8. NativeSlice<byte> srcName = SrcBytes.Slice ( offset, srcNameLen ).SliceConvert<byte> ();
    9. offset += srcNameLen;
    10.  
    the scrName is about the string , how to convert to string in mainthread?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
  3. Chaoslife

    Chaoslife

    Joined:
    Aug 16, 2016
    Posts:
    13
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    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.
     
  5. Chaoslife

    Chaoslife

    Joined:
    Aug 16, 2016
    Posts:
    13
    thanks reply,
    I didn't know how long it was until I read it, so how to delcare the variable srcname in job ?
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    It sounds like what you really want is FixedStringXBytes from the Collections package.
     
  7. Chaoslife

    Chaoslife

    Joined:
    Aug 16, 2016
    Posts:
    13
    ok, thanks a lot, i'll check this out