Using Parallel.For in a Unity3d C# script

Hi

I’m trying to use Parallel.For in a script file, but I get the following error:

error CS0234: The type or namespace name Tasks' does not exist in the namespace System.Threading’. Are you missing an assembly reference?

Here is my code for that section (see the commented out for loop I’m trying to replace just below it):

	private void DisplaceHexagon(GameObject hexagon, Texture2D heightmap, float displacementStrength)
	{
		Mesh mesh = ((MeshFilter)hexagon.GetComponent("MeshFilter")).mesh;
		Vector3[] vertices = new Vector3[mesh.vertices.Length];
		mesh.vertices.CopyTo(vertices, 0);
		
		int[] textureCoordX = mesh.uv.Select(item => (int)(item.x * heightmap.width)).ToArray();
		int[] textureCoordY = mesh.uv.Select(item => (int)(item.y * heightmap.width)).ToArray();
		
		Parallel.For(0, vertices.Length, i =>
		{
			vertices<em>.z = -heightmap.GetPixel(textureCoordX<em>, textureCoordY_).grayscale * displacementStrength;_</em></em>

* });*

_ /for (int i = 0; i < vertices.Length; i++)
{
vertices.z = -heightmap.GetPixel(textureCoordX, textureCoordY).grayscale * displacementAmount;
}/_

* mesh.vertices = vertices;*
* }*
I’ve set my mono project target framework to Mono / .Net 4 in the general build options. The project builds fine in Mono Develop from the Build menu and I’ve also cleaned the solution and re-built. Also, the Unity game runs fine when targeting Mono / .Net 4 if I’m not using Parallel.For and use the normal for loop instead.
I have the following using directives at the top of my file:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading;
using System.Threading.Tasks;
and I have the following references in my project:
UnityEditor.dll
UnityEngine.dll
System
System.Core
System.Xml
If I type it out in the code intellisense even picks up the path to Parallel.For correctly and I can right-click on Parallel and “resolve” which adds the correct “using” line at the top of my file. Yet Unity gives me the above error.
Does anyone know what I’m doing wrong here? Thank you in advance for any assistance.

The answer is self explanatory:

“error CS0234: The type or namespace name Tasks' does not exist in the namespace System.Threading’. Are you missing an assembly reference?”

The mono framework does not contain the ‘Tasks’ assembly [last I check any-who]. Furthermore threading within unity is a fickle business I’m 90% sure the line you’re trying to thread is going to throw you an exception [can’t use unity types in threading - e.g. Texture2D].

You asked me why the code I’ve posted previously worked - answer is that code would not have been Unity code.

The possible solutions are:

  • Keep it simple.
  • Use coroutines. [Single threaded, work split over frames].
  • Use traditional threading [slightly uglier, but same stuff].
  • See if mono have finally built the tasks library, and see if unity will play nice with it.
  • Write/find your own Parallel.For wrapper for traditional threading.

I’ve set my mono project target framework to Mono / .Net 4

Set it to .Net 3.5 instead.

For the benefit of future readers, Threading.Tasks is not included in the verison of Mono that Unity uses, but there is a backport to older versions of .Net. Download the .dll and stick it in your Plugins; the dll should should be compatible with all platforms.