Is this a decent way to avoid the pesky can only be executed from the main thread error?

What I have below is so far so good, but is it okay to actually do this on any platform / OS?

Here’s the error I was getting : argumentexception: layertoname can only be called from the main thread.

and here’s what I did (I am just calling this static method via my class constructor)

		public static string[] GetLayers(){
			string[] ret = new string[32];
			if (System.Threading.Thread.CurrentThread.ManagedThreadId == 1) {
				for (int i = 0; i < ret.Length; i++) {
					ret [i] = LayerMask.LayerToName (i);
				}
			}
			return ret;
		}

Thanks for your input.

What about allocating the array from the main thread and keeping a reference to it instead of generating a new one with each call to the method ?

And if re-allocating is mandatory for your process, you can clone the array you created in the main thread.