Multi threading to Main thread

I follow Sebastian Legue’s procedural terrain generation tutorials and it work. and I’m try to use it for webgl and for that it don’t work. I think it’s because of Multi Threading.

so can you tell me how to edit for only main thread?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;

public class ThreadedDataRequester : MonoBehaviour {

static ThreadedDataRequester instance;
Queue<ThreadInfo> dataQueue = new Queue<ThreadInfo>();

void Awake() {
	instance = FindObjectOfType<ThreadedDataRequester> ();
}

public static void RequestData(Func<object> generateData, Action<object> callback) {
	ThreadStart threadStart = delegate {
		instance.DataThread (generateData, callback);
	};

	new Thread (threadStart).Start ();
}

void DataThread(Func<object> generateData, Action<object> callback) {
	object data = generateData ();
	lock (dataQueue) {
		dataQueue.Enqueue (new ThreadInfo (callback, data));
	}
}
	

void Update() {
	if (dataQueue.Count > 0) {
		for (int i = 0; i < dataQueue.Count; i++) {
			ThreadInfo threadInfo = dataQueue.Dequeue ();
			threadInfo.callback (threadInfo.parameter);
		}
	}
}

struct ThreadInfo {
	public readonly Action<object> callback;
	public readonly object parameter;

	public ThreadInfo (Action<object> callback, object parameter)
	{
		this.callback = callback;
		this.parameter = parameter;
	}

}

}

this is his code.

video link : Procedural Landmass Generation (E08: Threading) - YouTube
his Github : GitHub - SebLague/Procedural-Landmass-Generation: Procedural Landmass Generation in Unity

Well, just ignore that class completely. I haven’t watched the tutorial, so I don’t know how the code looks like that is using this class. However instead of doing this:

ThreadedDataRequester.RequestData(YourGenerateMethod, YourCallback);

you could simply do this:

YourCallback(YourGenerateMethod());

so it just happens synchronously. Though I can’t say if executing the code synchronously may cause any issues. You probably have to try it. Though be warned that if the generation is that heavy so it got outsourced into a thread, chances are high that WebGL can’t even handle the amount of data.

edit
if this class is used in several places, you can also do the “fix” right inside the “RequestData” method like this:

public static void RequestData(Func<object> generateData, Action<object> callback)
{
#if UNITY_WEBGL
    callback(generateData());
#else
    ThreadStart threadStart = delegate {
        instance.DataThread (generateData, callback);
    };
    new Thread (threadStart).Start ();
#endif
}