x


How to create LayerMask field in a custom EditorWindow?

Is there a way to manually invoke the LayerMask field in an EditorWindow? Like you can with TagField or LayerField...

more ▼

asked Jan 15 '11 at 04:28 AM

Alex Chouls gravatar image

Alex Chouls
428 11 15 25

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Hmm, it looks like they forgot to implement it / make it public. I've found that little helperclass which can convert a layer into the corresponding layername and reverse: LayerMask. With this you can try to implement something similar but it won't look like the popup in the inspector.

btw. if you add a public var with this type to a script the default inspector will show that popup, but it seems that we can't invoke it manually.

more ▼

answered Jan 15 '11 at 04:52 AM

Bunny83 gravatar image

Bunny83
45.1k 11 49 206

(comments are locked)
10|3000 characters needed characters left

Here is an implementation which looks quite similar to the built in one. But it shows unnamed layers as well due to technical limitations. To prevent too much memory usage is updates the layer name list only every second... but that should be enough. I have an implementation for Unity 3.4 and earlier as well, but the code for that is a lot more hackish. But I can post it if anyone is interested.

public static List<string> layers;
public static List<int> layerNumbers;
public static string[] layerNames;
public static long lastUpdateTick;

/** Displays a LayerMask field.
 * \param showSpecial Use the Nothing and Everything selections
 * \param selected Current LayerMask
 * \version Unity 3.5 and up will use the EditorGUILayout.MaskField instead of a custom written one.
 */
public static LayerMask LayerMaskField (string label, LayerMask selected, bool showSpecial) {

    //Unity 3.5 and up

    if (layers == null || (System.DateTime.Now.Ticks - lastUpdateTick > 10000000L && Event.current.type == EventType.Layout)) {
        lastUpdateTick = System.DateTime.Now.Ticks;
        if (layers == null) {
            layers = new List<string>();
            layerNumbers = new List<int>();
            layerNames = new string[4];
        } else {
            layers.Clear ();
            layerNumbers.Clear ();
        }

        int emptyLayers = 0;
        for (int i=0;i<32;i++) {
            string layerName = LayerMask.LayerToName (i);

            if (layerName != "") {

                for (;emptyLayers>0;emptyLayers--) layers.Add ("Layer "+(i-emptyLayers));
                layerNumbers.Add (i);
                layers.Add (layerName);
            } else {
                emptyLayers++;
            }
        }

        if (layerNames.Length != layers.Count) {
            layerNames = new string[layers.Count];
        }
        for (int i=0;i<layerNames.Length;i++) layerNames[i] = layers[i];
    }

    selected.value =  EditorGUILayout.MaskField (label,selected.value,layerNames);

    return selected;
}
more ▼

answered Jul 26 '12 at 07:18 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3677
x168
x42

asked: Jan 15 '11 at 04:28 AM

Seen: 1470 times

Last Updated: Jul 26 '12 at 07:23 PM