unity-game-enginenamespacesjobsassembly-references

ComputeJobOptimization Attribute isnt working. Missing a using directive or an assembly reference?


Trying to use the job system in unity. [ComputeJobOptimization] cant be put in my code.

Assets/GameManager.cs(26,6): error CS0246: The type or namespace name 'ComputeJobOptimization' could not be found (are you missing a using directive or an assembly reference?)

I added Burst package, Jobs package. Put all the possible using.xxx in the code. None of them worked. I am crying.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Jobs;
using Unity.Jobs;
using Unity.Collections;
using Unity.Burst;

public class GameManager : MonoBehaviour
{

    [SerializeField]
    int shipAmount;
    [SerializeField]
    GameObject enemyShipPrefab;

    TransformAccessArray transforms;
    MovementJob movementJob;
    JobHandle movementJobHandle;

    public float fallSpeed = 10.0f;

    public static GameManager GM = null;

    //ComputeJobOptimization
    [ComputeJobOptimization]
    public struct MovementJob : IJobParallelForTransform
    {

        public float fallSpeed;
        public float topBound;
        public float bottomBound;
        public float deltaTime;
        public void Execute(int index, TransformAccess transform)
        {
            Vector3 down = new Vector3(0, -1f, 0);
            Vector3 pos = transform.position;
            pos += down * deltaTime * fallSpeed;
            if (pos.y < bottomBound)
            {
                pos.y = topBound;
            }
            transform.position = pos;
        }
    }

    private void Start()
    {
        if (GM == null)
        {
            GM = this;
        }
        transforms = new TransformAccessArray(0);
    }

    private void OnDisable()
    {
        this.transforms.Dispose();
    }
    // Start is called before the first frame update
    void Update()
    {

        if (Input.GetKeyDown("space"))
            AddShips(shipAmount);

        movementJob = new MovementJob()
        {
            fallSpeed = fallSpeed,
            bottomBound = -3.5f,
            topBound = 5.5f,
            deltaTime = Time.deltaTime
        };

        movementJob.Schedule(transforms);

        JobHandle.ScheduleBatchedJobs();
    }

    private void LateUpdate()
    {
        movementJobHandle.Complete();
    }

    void AddShips(int amount)
    {
        movementJobHandle.Complete();
        transforms.capacity = transforms.length + amount;
        for (int i = 0; i < amount; i++)
        {
            float xVal = Random.Range(-8.4f, 8.4f);
            float yVal = Random.Range(-3.5f, 5.5f);
            float zVal = Random.Range(-2.0f, 10f);

            Vector3 pos = new Vector3(xVal, yVal, zVal);
            Quaternion rot = Quaternion.Euler(0f, 180f, 0f);

            var obj = Instantiate(enemyShipPrefab, pos, rot) as GameObject;
            transforms.Add(obj.transform);
        }
    }

}

Solution

  • As far as I understand this is now the [BurstCompile] attribute. I also had a similar issue when trying to use the new attribute and I had to import the burst package library from the unity package manager window.