I have a regular NativeArray of colors. I want to efficiently sample and get a specific portion of data from that array depending on the given rect.
Let's say I have this 5x5 (25 in length) size 1D color array and I need to get data from this rect (x:2, y:1, w:3, h:2) as shown in the below picture

How can I do that?
If I try to use the function NativeArray<>.Copy it copies from index to length. So if I specify index 7 and 6 length it would copy from 7 to 12, which wouldn't work for me.
NativeArray.Copy as you found out can take a start index where the copy should begin and an end index where it should end. Your scenario is more complicated. You may treat the Native array as a 2D array and use a nested for loop to copy elements from it over to the destination. The Copy method does the same, it is not a magic thing.
You may use following formula to convert 2D x, y indexes to 1D index
i = y * width + x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With