Posts Tagged nativearray

NativeArray2D

Tags: , , ,

Unity provides exactly one collection: NativeArray<T>. Compared to managed arrays in C#, these must be one-dimensional. So today we’re building a two-dimensional version of it: NativeArray<T>. We’ll add this to the NativeCollections GitHub repository for easy inclusion into any project. Read on to learn more about the collection!

Read the rest of this article »

5 Comments

Simple Formula Evaluator Revisited

Tags: , , , , , ,

A couple years ago, I wrote an article showing how to empower game designers with the ability to write simple formulas like PlayerLevel*100+100. These are much more useful than just constants and don’t require any of the complexity of a real programming language. Today we’ll bring it into the Burst-compatible world and also improve its ability to handle more complex formulas.

Read the rest of this article »

5 Comments

NativeHashSet<T>

Tags: , , ,

Unity’s Native Collections package, currently in Preview, provides a hash map but not a hash set. Today we’ll supplement NativeHashMap<TKey, TValue> with our own NativeHashSet<T> as part of the NativeCollections repo. Read on for performance results and to see how to use it!

Read the rest of this article »

3 Comments

Supporting ParallelFor Jobs in Ranged Native Collections

Tags: , ,

Native collections are funny things. On one hand they’re structs, which are supposed to be value types that get copied on assignment. On the other hand, they act like reference types because they contain a hidden pointer internally. This can make using and implementing them difficult to understand, especially in the context of a ParallelFor job. Today we’ll examine more closely how to properly support ParallelFor jobs, especially with ranged containers like NativeList<T>.

Read the rest of this article »

No Comments

NativeChunkedList<T>: Part 2

Tags: , ,

Last week we looked at a new native collection type: NativeChunkedList<T>. This type saved us a lot of memory and gave us a faster way to dynamically grow an array. Unfortunately, iterating over it was quite a lot slower. Today we’ll speed it up for both IJob and IJobParallelFor. In doing so, we’ll learn more about how to create custom Unity job types and about how IEnumerable and IEnumerator work.

Read the rest of this article »

No Comments

NativeChunkedList<T>

Tags: , ,

Today’s article is about a new native collection type: NativeChunkedList<T>. This type is great when you need a dynamically-resizable array that’s fast to add to and doesn’t waste a lot of memory. Read on to see how it’s implemented, see the performance report, and get the source code.

Read the rest of this article »

2 Comments

Enumerables Without the Garbage: Part 8

Tags: , , , , , , ,

NativeArray<T> is great, but very limited in functionality. We can fix this surprisingly easily! Today we revive a two year old series that created the iterator project. Iterators are like a no-GC version of IEnumerable<T> and LINQ which have a lot of power but only support managed arrays (T[]) and List<T>. Today we’ll add support for NativeArray<T> and inherit support for the same functionality. We’ll also spruce up the project with proper unit tests, assembly definitions, and runtime tests to confirm that zero garbage is created. Read on to see how this was done and how to use iterators with NativeArray<T>.

Read the rest of this article »

3 Comments

How to Make Custom Native Collections

Tags: , ,

We’ve seen how NativeArray works, but what if we want more kinds of native collections? Unity 2018.1 only has that one, but you can make your own! Today’s article shows exactly how to do that.

Read the rest of this article »

11 Comments

How NativeArray Works

Tags: ,

NativeArray<T> is a new type introduced recently in Unity 2018.1. It’s like List<T> except it’s backed by an unmanaged array instead of a managed array. It’s also a struct instead of a class. This means it creates no garbage for the GC to later collect. That’s the surface level description, but today we’ll go in depth to find out how it really works and learn some interesting facts along the way.

Read the rest of this article »

9 Comments