protected override void OnUpdate()
    {
        NativeIntArray myArray = new NativeIntArray(100, Allocator.TempJob);

        // Fill myArray with normal distribution values.
        JobHandle jobHandle = new ParallelWriteNormalDistributionJob()
        {
            random = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)),
            array  = myArray.AsParallelWriter()
        }.Schedule(10000, 64);         // Run our job a 10000 times in batches of 64 (values chosen randomly).

        jobHandle.Complete();

        // Draw each element in myArray as a bar graph where it's value is the height of the bar.
        Job.WithName("DrawBarGraph")
        .WithReadOnly(myArray)
        .WithoutBurst()
        .WithCode(() =>
        {
            for (int i = 0; i < myArray.Length; i++)
            {
                float barWidth  = 1.0f;
                float barHeight = (myArray[i] / 40.0f) * 10.0f;
                DrawBar(new float2(i * barWidth, 0), new float2(barWidth, barHeight));
            }
        }).Run();


        myArray.Dispose();
    }
Example #2
0
    static void Allocate(int length, Allocator allocator, out NativeIntArray array)
    {
        // Calculate how many bytes are needed.
        long size = UnsafeUtility.SizeOf <int>() * (long)length;

        // Check if this is a valid allocation.
#if ENABLE_UNITY_COLLECTIONS_CHECKS
        if (allocator <= Allocator.None)
        {
            throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator));
        }

        if (length < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(length), "Length must be >= 0");
        }

        if (size > int.MaxValue)
        {
            throw new ArgumentOutOfRangeException(nameof(length), $"Length * sizeof(int) cannot exceed {(object)int.MaxValue} bytes");
        }

        // There are other checks you might want to perform when working with templated containers.

        /*
         * if (!UnsafeUtility.IsBlittable<T>())
         * throw new ArgumentException(string.Format("{0} used in NativeCustomArray<{0}> must be blittable", typeof(T)));
         *
         * if (!UnsafeUtility.IsValidNativeContainerElementType<T>())
         * throw new InvalidOperationException($"{typeof(T)} used in NativeCustomArray<{typeof(T)}> must be unmanaged (contain no managed types) and cannot itself be a native container type.");
         */
#endif

        array = default(NativeIntArray);
        // Allocate memory for our buffer.
        array.m_Buffer         = UnsafeUtility.Malloc(size, UnsafeUtility.AlignOf <int>(), allocator);
        array.m_Length         = length;
        array.m_AllocatorLabel = allocator;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
        // By default the job can operate over the entire range.
        array.m_MinIndex = 0;
        array.m_MaxIndex = length - 1;

        // Create a dispose sentinel to track memory leaks.
        // An atomic safety handle is also created automatically.
        DisposeSentinel.Create(out array.m_Safety, out array.m_DisposeSentinel, 1, allocator);
#endif
    }