Hi everyone,
Compile a Job with the burst compiler
To start with simple optimizations i followed this small tutorial.
https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html#standalone-player-support
This simple example in the docs use a native array to run a job over the NativeArray****. That works fine.
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class MyBurst2Behavior : MonoBehaviour
{
void Start()
{
var input = new NativeArray<float>(10, Allocator.Persistent);
var output = new NativeArray<float>(1, Allocator.Persistent);
for (int i = 0; i < input.Length; i++) input[i] = 1.0f * i;
var job = new MyJob
{
Input = input,
Output = output
};
job.Schedule().Complete();
Debug.Log("The result of the sum is: " + output[0]);
input.Dispose(); output.Dispose();
}
[BurstCompile(CompileSynchronously = true)]
private struct MyJob : IJob
{
[ReadOnly]
public NativeArray<float> Input;
[WriteOnly]
public NativeArray<float> Output;
public void Execute()
{
float result = 0.0f;
for (int i = 0; i < Input.Length; i++)
{
result += Input[i];
}
Output[0] = result;
}
}
}
But how to use NativeArray****. in this case?
- Struct types
- Burst supports regular structs with any field with supported types.
This following example use a NativeArray**** instead of NativeArray****
```csharp
**using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using Unity.Entities;
public class Test_NativeArrayStruct : MonoBehaviour {
[Serializable]
public struct theStruct {
public int int1;
}
public NativeArray<theStruct> structNative = new NativeArray<theStruct>();
private void Awake() {
structNative = new NativeArray<theStruct>(10000, Allocator.Persistent);
}
private void Update() {
var job = new MyJob {
StructNative = structNative, // <------------ Fail ---------------
};
job.Schedule().Complete();
}
[BurstCompile(CompileSynchronously = true)]
private struct MyJob : IJob {
public theStruct[] StructNative;
public void Execute() {
for (int i = 0; i < StructNative.Length; i++) {
StructNative[i].int1 = 77;
}
}
}
private void OnDestroy() {
structNative.Dispose();
}
}**
```
But it fail with the data conversion message:
StructNative = structNative,
Any ideas how this can be solved?
