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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Slicing NativeText

Discussion in 'C# Job System' started by CarePackage17, Apr 12, 2023.

  1. CarePackage17

    CarePackage17

    Joined:
    Dec 6, 2014
    Posts:
    21
    I'm playing around with the Job system to see if I can generate msbuild projects. One of the things I get as input is the list of source files that make up an assembly, which I get as file system paths.

    Initially I tried to pass them into the job as NativeArray<NativeText>, but that gave me this output:
    InvalidOperationException: Unity.Collections.NativeText used in NativeArray<Unity.Collections.NativeText> must be unmanaged (contain no managed types) and cannot itself be a native container type.

    So I'm trying to use a single NativeText member in my job struct and delimit the paths with a special character instead.
    Now inside the job's Execute() method I need access paths one by one again to do some string formatting, which brings me to my question:

    Is there a safe way of slicing NativeText, i.e. getting a sub-range of the entire thing (like GetSubArray for NativeArray)? The only solution I could come up with involves using GetUnsafePtr() and copying to a temporary, then doing the string formatting via NativeText.AppendFormat.
    I tried to get a Span<byte> from the raw data, but that doesn't help me because I can't pass a span to AppendFormat later.

    The collections package version I'm currently using is 1.4.0.
     
    Last edited: Apr 12, 2023
  2. CarePackage17

    CarePackage17

    Joined:
    Dec 6, 2014
    Posts:
    21
    To answer my own question: Collections 2.x got substring methods (but doesn't help me because it's incompatible with 2021 LTS).

    Instead I'm using GetUnsafePtr() and a temporary variable (which is not safe, but works for now).
     
    DevDunk likes this.