Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CastingSourceDropDown.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.Universal;
  4. using System.Collections.Generic;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. internal class CastingSourceDropDown
  8. {
  9. public struct ShadowShapeProviderData
  10. {
  11. public Component component;
  12. public ShadowShape2DProvider provider;
  13. public ShadowShapeProviderData(Component inComponent, ShadowShape2DProvider inProvider)
  14. {
  15. component = inComponent;
  16. provider = inProvider;
  17. }
  18. }
  19. class SelectionData
  20. {
  21. public SerializedObject shadowCaster;
  22. public ShadowShape2DProvider newShadowShapeProvider;
  23. public Component newShadowShapeComponent;
  24. public int newCastingSource;
  25. public SelectionData(int inNewCastingSource, ShadowShape2DProvider inNewShapeProvider, Component inNewShadowShapeComponent, SerializedObject inShadowCaster)
  26. {
  27. shadowCaster = inShadowCaster;
  28. newShadowShapeProvider = inNewShapeProvider;
  29. newShadowShapeComponent = inNewShadowShapeComponent;
  30. newCastingSource = inNewCastingSource;
  31. }
  32. }
  33. struct ProviderComparer : IComparer<ShadowShapeProviderData>
  34. {
  35. public int Compare(ShadowShapeProviderData a, ShadowShapeProviderData b)
  36. {
  37. return b.provider.Priority() - a.provider.Priority();
  38. }
  39. }
  40. static public List<ShadowShapeProviderData> GetShadowShapeProviders(GameObject go)
  41. {
  42. // Create some providers to check against.
  43. var providerTypes = TypeCache.GetTypesDerivedFrom<ShadowShape2DProvider>();
  44. var providers = new List<ShadowShape2DProvider>(providerTypes.Count);
  45. foreach (Type providerType in providerTypes)
  46. {
  47. if (providerType.IsAbstract)
  48. continue;
  49. providers.Add(Activator.CreateInstance(providerType) as ShadowShape2DProvider);
  50. }
  51. // Fetch the components to check.
  52. var components = go.GetComponents<Component>();
  53. var results = new List<ShadowShapeProviderData>();
  54. foreach (var component in components)
  55. {
  56. // check each component to see if it is a valid provider
  57. foreach (var provider in providers)
  58. {
  59. if (provider.IsShapeSource(component))
  60. {
  61. results.Add(new ShadowShapeProviderData(component, Activator.CreateInstance(provider.GetType()) as ShadowShape2DProvider));
  62. }
  63. }
  64. }
  65. return results;
  66. }
  67. void OnMenuOptionSelected(object layerSelectionDataObject)
  68. {
  69. SelectionData selectionData = (SelectionData)layerSelectionDataObject;
  70. SerializedProperty shapeProvider = selectionData.shadowCaster.FindProperty("m_ShadowShape2DProvider");
  71. SerializedProperty shapeComponent = selectionData.shadowCaster.FindProperty("m_ShadowShape2DComponent");
  72. SerializedProperty castingSource = selectionData.shadowCaster.FindProperty("m_ShadowCastingSource");
  73. selectionData.shadowCaster.Update();
  74. castingSource.intValue = selectionData.newCastingSource;
  75. shapeProvider.managedReferenceValue = selectionData.newShadowShapeProvider;
  76. shapeComponent.objectReferenceValue = selectionData.newShadowShapeComponent;
  77. selectionData.shadowCaster.ApplyModifiedProperties();
  78. }
  79. string GetCompactTypeName(Component component)
  80. {
  81. string type = component.GetType().ToString();
  82. int lastIndex = type.LastIndexOf('.');
  83. string compactTypeName = lastIndex < 0 ? type : type.Substring(lastIndex + 1);
  84. return ObjectNames.NicifyVariableName(compactTypeName);
  85. }
  86. public void OnCastingSource(SerializedObject serializedObject, UnityEngine.Object[] targets, GUIContent labelContent)
  87. {
  88. Rect totalPosition = EditorGUILayout.GetControlRect();
  89. Rect position = EditorGUI.PrefixLabel(totalPosition, labelContent);
  90. if (targets.Length <= 1)
  91. {
  92. ShadowCaster2D shadowCaster = targets[0] as ShadowCaster2D;
  93. // Check for the current value
  94. GUIContent selected = new GUIContent("None");
  95. if (shadowCaster.shadowCastingSource == ShadowCaster2D.ShadowCastingSources.ShapeEditor)
  96. selected = new GUIContent("ShapeEditor");
  97. else if (shadowCaster.shadowCastingSource == ShadowCaster2D.ShadowCastingSources.ShapeProvider && shadowCaster.shadowShape2DProvider != null)
  98. {
  99. if (shadowCaster.shadowShape2DComponent != null)
  100. selected = new GUIContent(shadowCaster.shadowShape2DProvider.ProviderName(GetCompactTypeName(shadowCaster.shadowShape2DComponent)));
  101. else
  102. selected = new GUIContent("None");
  103. }
  104. // Draw the drop down menu
  105. if (EditorGUI.DropdownButton(position, selected, FocusType.Keyboard, EditorStyles.popup))
  106. {
  107. GenericMenu menu = new GenericMenu();
  108. menu.allowDuplicateNames = true;
  109. ProviderComparer providerComparer = new ProviderComparer();
  110. List<ShadowShapeProviderData> castingSources = GetShadowShapeProviders(shadowCaster.gameObject);
  111. castingSources.Sort(providerComparer);
  112. for (int i = 0; i < castingSources.Count; i++)
  113. {
  114. string menuName = castingSources[i].provider.ProviderName(GetCompactTypeName(castingSources[i].component));
  115. menu.AddItem(new GUIContent(menuName), false, OnMenuOptionSelected, new SelectionData((int)ShadowCaster2D.ShadowCastingSources.ShapeProvider, castingSources[i].provider, castingSources[i].component, serializedObject));
  116. }
  117. menu.AddItem(new GUIContent("Shape Editor"), false, OnMenuOptionSelected, new SelectionData((int)ShadowCaster2D.ShadowCastingSources.ShapeEditor, null, null, serializedObject));
  118. menu.AddItem(new GUIContent("None"), false, OnMenuOptionSelected, new SelectionData((int)ShadowCaster2D.ShadowCastingSources.None, null, null, serializedObject));
  119. menu.DropDown(position);
  120. }
  121. }
  122. else
  123. {
  124. EditorGUI.showMixedValue = true;
  125. EditorGUI.BeginDisabledGroup(true);
  126. EditorGUI.DropdownButton(position, new GUIContent(""), FocusType.Keyboard, EditorStyles.popup);
  127. EditorGUI.EndDisabledGroup();
  128. }
  129. }
  130. }
  131. }