LayerMask in an editor script.

Hi all!
I’m really struggling to get a LayerMask working in an editor script.

I have the variable
public LayerMask collisionLayer;
It looks like EditorGUILayout.MaskField is the way to go but I can’t figure out the ‘displayedOption’. Do you have to manually enter each layer in?

Could someone point me in the right direction?
Thanks,
Pete

Hey!

EditorGUILayout.MaskField seems to be used to create a custom list and the displayedOption seems to be the custom list/array of options that you provide.

Maybe LayerField is what you look for to create LayerMask selection?

I hope it helps!

Hey thanks @SunnyValleyStudio !
Maybe I’m doing something wrong but layerfield seems to only let me select one layer at a time.

I have this in my editor script -

baseScript.collisionLayer = EditorGUILayout.LayerField(baseScript.collisionLayer);

And this on my editor script -

public LayerMask collisionLayer;

In the editor it seems to work (although it only lets me select one layer)
7562143--935635--Screen Shot 2021-10-11 at 8.48.11 am.png
But if I switch to debug, the result is actually different.
7562143--935638--Screen Shot 2021-10-11 at 8.48.26 am.png

Any ideas what I might be stuffing up there?
Thanks!
Pete

You need to use MaskField not LayerField to select more than one layer.

Okay cool, but with EditorGUILayout.MaskField, it seems that you have to manually add all of the layers with a string array.
Is there a way to automate that? It seems a bit tedious, and potentially problematic.

This will do

string[] stringz = new string[32];
for (int i = 0; i < 32; i++) stringz[i] = ("A Layer "+i);
baseScript.collisionLayer = EditorGUILayout.MaskField(baseScript.collisionLayer, stringz);