SortingLayerDropDown.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.Universal;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. internal class SortingLayerDropDown
  8. {
  9. private class LayerSelectionData
  10. {
  11. public SerializedObject serializedObject;
  12. public Object[] targets;
  13. public int layerID;
  14. public System.Action<SerializedObject> onSelectionChanged;
  15. public LayerSelectionData(SerializedObject so, int lid, Object[] tgts, System.Action<SerializedObject> selectionChangedCallback)
  16. {
  17. serializedObject = so;
  18. layerID = lid;
  19. targets = tgts;
  20. onSelectionChanged = selectionChangedCallback;
  21. }
  22. }
  23. private static class Styles
  24. {
  25. public static GUIContent sortingLayerAll = EditorGUIUtility.TrTextContent("All");
  26. public static GUIContent sortingLayerNone = EditorGUIUtility.TrTextContent("None");
  27. public static GUIContent sortingLayerMixed = EditorGUIUtility.TrTextContent("Mixed...");
  28. }
  29. SortingLayer[] m_AllSortingLayers;
  30. GUIContent[] m_AllSortingLayerNames;
  31. List<int> m_ApplyToSortingLayersList;
  32. SerializedProperty m_ApplyToSortingLayers;
  33. public void OnEnable(SerializedObject serializedObject, string propertyName)
  34. {
  35. m_ApplyToSortingLayers = serializedObject.FindProperty(propertyName);
  36. m_ApplyToSortingLayersList = new List<int>(m_ApplyToSortingLayers.arraySize);
  37. m_AllSortingLayers = SortingLayer.layers;
  38. m_AllSortingLayerNames = m_AllSortingLayers.Select(x => new GUIContent(x.name)).ToArray();
  39. }
  40. void UpdateApplyToSortingLayersArray(object layerSelectionDataObject)
  41. {
  42. LayerSelectionData layerSelectionData = (LayerSelectionData)layerSelectionDataObject;
  43. m_ApplyToSortingLayers.ClearArray();
  44. for (int i = 0; i < m_ApplyToSortingLayersList.Count; ++i)
  45. {
  46. m_ApplyToSortingLayers.InsertArrayElementAtIndex(i);
  47. m_ApplyToSortingLayers.GetArrayElementAtIndex(i).intValue = m_ApplyToSortingLayersList[i];
  48. }
  49. if (layerSelectionData.onSelectionChanged != null)
  50. layerSelectionData.onSelectionChanged(layerSelectionData.serializedObject);
  51. layerSelectionData.serializedObject.ApplyModifiedProperties();
  52. if (layerSelectionData.targets is Light2D[])
  53. {
  54. foreach (Light2D light in layerSelectionData.targets)
  55. {
  56. if (light != null && light.lightType == Light2D.LightType.Global)
  57. Light2DManager.ErrorIfDuplicateGlobalLight(light);
  58. }
  59. }
  60. }
  61. void OnNoSortingLayerSelected(object selectionData)
  62. {
  63. m_ApplyToSortingLayersList.Clear();
  64. UpdateApplyToSortingLayersArray(selectionData);
  65. }
  66. void OnAllSortingLayersSelected(object selectionData)
  67. {
  68. m_ApplyToSortingLayersList.Clear();
  69. m_ApplyToSortingLayersList.AddRange(m_AllSortingLayers.Select(x => x.id));
  70. UpdateApplyToSortingLayersArray(selectionData);
  71. }
  72. void OnSortingLayerSelected(object layerSelectionDataObject)
  73. {
  74. LayerSelectionData layerSelectionData = (LayerSelectionData)layerSelectionDataObject;
  75. int layerID = (int)layerSelectionData.layerID;
  76. if (m_ApplyToSortingLayersList.Contains(layerID))
  77. m_ApplyToSortingLayersList.RemoveAll(id => id == layerID);
  78. else
  79. m_ApplyToSortingLayersList.Add(layerID);
  80. UpdateApplyToSortingLayersArray(layerSelectionDataObject);
  81. }
  82. public void OnTargetSortingLayers(SerializedObject serializedObject, Object[] targets, GUIContent labelContent, System.Action<SerializedObject> selectionChangedCallback)
  83. {
  84. Rect totalPosition = EditorGUILayout.GetControlRect();
  85. GUIContent actualLabel = EditorGUI.BeginProperty(totalPosition, labelContent, m_ApplyToSortingLayers);
  86. Rect position = EditorGUI.PrefixLabel(totalPosition, actualLabel);
  87. m_ApplyToSortingLayersList.Clear();
  88. int applyToSortingLayersSize = m_ApplyToSortingLayers.arraySize;
  89. for (int i = 0; i < applyToSortingLayersSize; ++i)
  90. {
  91. int layerID = m_ApplyToSortingLayers.GetArrayElementAtIndex(i).intValue;
  92. if (SortingLayer.IsValid(layerID))
  93. m_ApplyToSortingLayersList.Add(layerID);
  94. }
  95. GUIContent selectedLayers;
  96. if (m_ApplyToSortingLayersList.Count == 1)
  97. selectedLayers = new GUIContent(SortingLayer.IDToName(m_ApplyToSortingLayersList[0]));
  98. else if (m_ApplyToSortingLayersList.Count == m_AllSortingLayers.Length)
  99. selectedLayers = Styles.sortingLayerAll;
  100. else if (m_ApplyToSortingLayersList.Count == 0)
  101. selectedLayers = Styles.sortingLayerNone;
  102. else
  103. selectedLayers = Styles.sortingLayerMixed;
  104. if (EditorGUI.DropdownButton(position, selectedLayers, FocusType.Keyboard, EditorStyles.popup))
  105. {
  106. GenericMenu menu = new GenericMenu();
  107. menu.allowDuplicateNames = true;
  108. LayerSelectionData layerSelectionData = new LayerSelectionData(serializedObject, 0, targets, selectionChangedCallback);
  109. menu.AddItem(Styles.sortingLayerNone, m_ApplyToSortingLayersList.Count == 0, OnNoSortingLayerSelected, layerSelectionData);
  110. menu.AddItem(Styles.sortingLayerAll, m_ApplyToSortingLayersList.Count == m_AllSortingLayers.Length, OnAllSortingLayersSelected, layerSelectionData);
  111. menu.AddSeparator("");
  112. for (int i = 0; i < m_AllSortingLayers.Length; ++i)
  113. {
  114. var sortingLayer = m_AllSortingLayers[i];
  115. layerSelectionData = new LayerSelectionData(serializedObject, sortingLayer.id, targets, selectionChangedCallback);
  116. menu.AddItem(m_AllSortingLayerNames[i], m_ApplyToSortingLayersList.Contains(sortingLayer.id), OnSortingLayerSelected, layerSelectionData);
  117. }
  118. menu.DropDown(position);
  119. }
  120. EditorGUI.EndProperty();
  121. }
  122. }
  123. }