fxvSortingLayerAttribute.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace FXV.Internal
  6. {
  7. public class fxvSortingLayerAttribute : PropertyAttribute
  8. {
  9. #if UNITY_EDITOR
  10. [CustomPropertyDrawer(typeof(fxvSortingLayerAttribute))]
  11. public class SortingLayerDrawer : PropertyDrawer
  12. {
  13. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  14. {
  15. string[] sortingLayerNames = new string[SortingLayer.layers.Length];
  16. for (int a = 0; a < SortingLayer.layers.Length; a++)
  17. sortingLayerNames[a] = SortingLayer.layers[a].name;
  18. if (property.propertyType != SerializedPropertyType.String)
  19. {
  20. EditorGUI.HelpBox(position, property.name + "{0} is not an string but has [SortingLayer].", MessageType.Error);
  21. }
  22. else if (sortingLayerNames.Length == 0)
  23. {
  24. EditorGUI.HelpBox(position, "There is no Sorting Layers.", MessageType.Error);
  25. }
  26. else if (sortingLayerNames != null)
  27. {
  28. EditorGUI.BeginProperty(position, label, property);
  29. // Look up the layer name using the current layer ID
  30. string oldName = property.stringValue;
  31. // Use the name to look up our array index into the names list
  32. int oldLayerIndex = -1;
  33. for (int a = 0; a < sortingLayerNames.Length; a++)
  34. if (sortingLayerNames[a].Equals(oldName)) oldLayerIndex = a;
  35. // Show the popup for the names
  36. int newLayerIndex = EditorGUI.Popup(position, label.text, oldLayerIndex, sortingLayerNames);
  37. // If the index changes, look up the ID for the new index to store as the new ID
  38. if (newLayerIndex != oldLayerIndex)
  39. {
  40. property.stringValue = sortingLayerNames[newLayerIndex];
  41. }
  42. EditorGUI.EndProperty();
  43. }
  44. }
  45. }
  46. #endif
  47. }
  48. }