Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Last active October 26, 2016 04:41
Show Gist options
  • Save birdinforest/ff7697825d38ff6328f3264aab656815 to your computer and use it in GitHub Desktop.
Save birdinforest/ff7697825d38ff6328f3264aab656815 to your computer and use it in GitHub Desktop.
EditorTools. Customized layerMaskFiled. Create layermask on inpsector. InternalEditorUtility.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal;
public class EditorTools {
private static List<int> layerValues = new List<int>();
private static long lastUpdateTick;
private static string[] layers;
public static LayerMask LayerMaskField(string label, LayerMask layerMask)
{
// Prevent unnecessary update.
if ((System.DateTime.Now.Ticks - lastUpdateTick > 10000000L && Event.current.type == EventType.Layout || layers == null)) {
// Get all avaliable layers.
layers = InternalEditorUtility.layers;
layerValues.Clear();
for (int i = 0; i < layers.Length; i++)
layerValues.Add(LayerMask.NameToLayer(layers[i]));
lastUpdateTick = System.DateTime.Now.Ticks;
}
int maskWithoutEmpty = 0;
for (int i = 0; i < layerValues.Count; i++)
{
// Loop all avaliable layers. If one avaliable layer is allready in current layer mask. Copy its index in avaliable layer list into the mask that will display on inspector. That mask will also receive new layers index later.
if(((1 << layerValues[i]) & layerMask.value) != 0)
maskWithoutEmpty |= (1 << i);
}
// Display current layer and receive new layers.
maskWithoutEmpty = UnityEditor.EditorGUILayout.MaskField(label, maskWithoutEmpty, layers);
int mask = 0;
for (int i = 0; i < layerValues.Count; i++)
{
// Loop all avaliable layers. If one layer is allready in display mask. Copy its layer value into output mask.
if ((maskWithoutEmpty & (1 << i)) != 0)
mask |= (1 << layerValues[i]);
}
layerMask.value = mask;
return layerMask;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment