Skip to content

Unity + async/await support #83

@JasonSpine

Description

@JasonSpine

I need to run CoACD in realtime, but it lags, whenever I run the algorithm.
So I thought about using Threading:

        public async void SetColliderMesh(Mesh rendererMesh)
        {
            BoxCollider tmpCollider = gameObject.AddComponent<BoxCollider>();
            Bounds tmpColliderBounds = myRenderer.localBounds;
            tmpCollider.center = tmpColliderBounds.center;
            tmpCollider.size = tmpColliderBounds.size;

            List<Mesh> convexMeshes = await Task.Run(() => coACD.RunACD(rendererMesh));

            Destroy(tmpCollider);

            for (int i = 0; i < convexMeshes.Count; i++)
            {
                MeshCollider mc = gameObject.AddComponent<MeshCollider>();
                mc.convex = true;
                mc.sharedMesh = convexMeshes[i];
            }
        }

But I get the error:

UnityException: get_canAccess can only be called from the main thread.

ChatGPT says that TaskRun() shouldn't touch any Unity objects so I would need to convert Unity objects to simple arrays and pass them to ACD method that would take raw arguments and this method would return arrays instead of unity objects....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions