- Home /
Unity Jobs Unknown Length of NativeArray
Hi, i couldn’t figure out how i can return a NativeArray with an unknown Length => i do a copyfrom inside the job little pseudo example:
public struct MyJob : IJob {
[ReadOnly] public NativeArray<Vector3> VectorOut;
public void Execute(){
var MyList = new List<Vector3>();
while(SomeCondition){
MyList.Add(SomeVector3);
}
VectorOut.CopyFrom(MyList);
}
}
public static Vector3[] SomeVoid(this Vector3[] MyArray){
var MyNativeArray = new NativeArray<Vector3>(?,Allocator.Persistant);
//don know what to put in there..
var job = new MyJob(){
VectorOut = MyNativeArray
};
var jobhandle = job.Schedule();
jobhandle.Complete();
MyArray = MyNativeArray.ToArray();
MyNativeArray.Dispose();
return MyArray;
}
right now i need to run the code first to get the index length for setting up the real calculations, but i believe there’s a better way to get to the Result.
if i do this like above it returns an an exception that the in and out nativearrays have to be the same length.
I don't understand the need for an array without knowing the size. You construct an empty NativeArray, then assign the array to a job, and somehow as though your NativeArray contained any information, you assign it to ANOTHER array and convert it to a normal array. Then you return the empty normal array. What exactly are you trying to do? Because you can create a NativeArray using an collection, so I fail to see why you need a size before initializing. The only reason for this is if you are treating the NativeArray like a list.
well i know about nativelists, but they’re IJob only(read this on the forums), just if i implement the job i need to set a nativearray to the jobs nativearray, to get the result of the job. so i could use a nativlist, where i add my values and get the information out of this, but why can’t i use an nativearray like normal array? right now the length of the jobs nativearray have to be the same as the outer array, or am i missing some rudimental things?
fyi i’m beginnig with the jobs system, and have a quite good understanding of c# in Unity.
Edited my Question with a Github Link of the Code in use!
i just don’t get it why this work:
int[] Array = new int[0];
int[] OrherArray = new int[]{1,2,3,4,7,0,42};
Array = OtherArray;
and this not:
var Array = new NativeArray<int>(0,Allocator.Persistent);
var job = new $$anonymous$$yJob(){
JobArray = Array };
var jobhandle = job.Schedule();
jobhandle.Complete();
//use Array as Result of the Job
public struct $$anonymous$$yJob : IJob{
public NativeArray<int> JobArray;
public void Execute(){
int[] OrherArray = new int[]{1,2,3,4,7,0,42};
JobArray.CopyFrom(OtherArray);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
IDevice.InitializeAsync().Wait() throws I/O Exception in System.FileStream 0 Answers
create string array from xml 2 Answers