Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question LayerMask in an editor script.

Discussion in 'Scripting' started by petey, Oct 8, 2021.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    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
     
  2. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    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!
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hey thanks @SunnyValleyStudio!
    Maybe I’m doing something wrong but layerfield seems to only let me select one layer at a time.
     
  4. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    I have this in my editor script -
    Code (CSharp):
    1. baseScript.collisionLayer = EditorGUILayout.LayerField(baseScript.collisionLayer);
    And this on my editor script -
    Code (CSharp):
    1. public LayerMask collisionLayer;
    In the editor it seems to work (although it only lets me select one layer)
    Screen Shot 2021-10-11 at 8.48.11 am.png
    But if I switch to debug, the result is actually different.
    Screen Shot 2021-10-11 at 8.48.26 am.png

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

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You need to use MaskField not LayerField to select more than one layer.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    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.
     
  7. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    This will do
    Code (CSharp):
    1.  
    2. string[] stringz = new string[32];
    3. for (int i = 0; i < 32; i++) stringz[i] = ("A Layer "+i);
    4. baseScript.collisionLayer = EditorGUILayout.MaskField(baseScript.collisionLayer, stringz);