UniversalRenderPipelineAssetEditor.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using UnityEditorInternal;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.Universal;
  4. using Styles = UnityEditor.Rendering.Universal.UniversalRenderPipelineAssetUI.Styles;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. [CustomEditor(typeof(UniversalRenderPipelineAsset)), CanEditMultipleObjects]
  8. public class UniversalRenderPipelineAssetEditor : Editor
  9. {
  10. SerializedProperty m_RendererDataProp;
  11. SerializedProperty m_DefaultRendererProp;
  12. internal ReorderableList rendererList => m_RendererDataList;
  13. ReorderableList m_RendererDataList;
  14. private SerializedUniversalRenderPipelineAsset m_SerializedURPAsset;
  15. public override void OnInspectorGUI()
  16. {
  17. m_SerializedURPAsset.Update();
  18. UniversalRenderPipelineAssetUI.Inspector.Draw(m_SerializedURPAsset, this);
  19. m_SerializedURPAsset.Apply();
  20. }
  21. void OnEnable()
  22. {
  23. m_SerializedURPAsset = new SerializedUniversalRenderPipelineAsset(serializedObject);
  24. CreateRendererReorderableList();
  25. }
  26. void CreateRendererReorderableList()
  27. {
  28. m_RendererDataProp = serializedObject.FindProperty("m_RendererDataList");
  29. m_DefaultRendererProp = serializedObject.FindProperty("m_DefaultRendererIndex");
  30. m_RendererDataList = new ReorderableList(serializedObject, m_RendererDataProp, true, true, true, true)
  31. {
  32. drawElementCallback = OnDrawElement,
  33. drawHeaderCallback = (Rect rect) => EditorGUI.LabelField(rect, Styles.rendererHeaderText),
  34. onCanRemoveCallback = reorderableList => reorderableList.count > 1,
  35. onRemoveCallback = OnRemoveElement,
  36. onReorderCallbackWithDetails = (reorderableList, index, newIndex) => UpdateDefaultRendererValue(index, newIndex) // Need to update the default renderer index
  37. };
  38. }
  39. void OnRemoveElement(ReorderableList reorderableList)
  40. {
  41. bool shouldUpdateIndex = false;
  42. // Checking so that the user is not deleting the default renderer
  43. if (reorderableList.index != m_DefaultRendererProp.intValue)
  44. {
  45. // Need to add the undo to the removal of our assets here, for it to work properly.
  46. Undo.RecordObject(target, $"Deleting renderer at index {reorderableList.index}");
  47. shouldUpdateIndex = true;
  48. m_RendererDataProp.DeleteArrayElementAtIndex(reorderableList.index);
  49. }
  50. else
  51. {
  52. EditorUtility.DisplayDialog(Styles.rendererListDefaultMessage.text, Styles.rendererListDefaultMessage.tooltip, "Close");
  53. }
  54. if (shouldUpdateIndex)
  55. {
  56. UpdateDefaultRendererValue(reorderableList.index);
  57. }
  58. EditorUtility.SetDirty(target);
  59. }
  60. void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)
  61. {
  62. rect.y += 2;
  63. Rect indexRect = new Rect(rect.x, rect.y, 14, EditorGUIUtility.singleLineHeight);
  64. EditorGUI.LabelField(indexRect, index.ToString());
  65. Rect objRect = new Rect(rect.x + indexRect.width, rect.y, rect.width - 134, EditorGUIUtility.singleLineHeight);
  66. EditorGUI.BeginChangeCheck();
  67. EditorGUI.ObjectField(objRect, m_RendererDataProp.GetArrayElementAtIndex(index), GUIContent.none);
  68. if (EditorGUI.EndChangeCheck())
  69. EditorUtility.SetDirty(target);
  70. Rect defaultButton = new Rect(rect.width - 75, rect.y, 86, EditorGUIUtility.singleLineHeight);
  71. var defaultRenderer = m_DefaultRendererProp.intValue;
  72. GUI.enabled = index != defaultRenderer;
  73. if (GUI.Button(defaultButton, !GUI.enabled ? Styles.rendererDefaultText : Styles.rendererSetDefaultText))
  74. {
  75. m_DefaultRendererProp.intValue = index;
  76. EditorUtility.SetDirty(target);
  77. }
  78. GUI.enabled = true;
  79. Rect selectRect = new Rect(rect.x + rect.width - 24, rect.y, 24, EditorGUIUtility.singleLineHeight);
  80. UniversalRenderPipelineAsset asset = target as UniversalRenderPipelineAsset;
  81. if (asset.ValidateRendererData(index))
  82. {
  83. if (GUI.Button(selectRect, Styles.rendererSettingsText))
  84. {
  85. Selection.SetActiveObjectWithContext(m_RendererDataProp.GetArrayElementAtIndex(index).objectReferenceValue,
  86. null);
  87. }
  88. }
  89. else // Missing ScriptableRendererData
  90. {
  91. if (GUI.Button(selectRect, index == defaultRenderer ? Styles.rendererDefaultMissingText : Styles.rendererMissingText))
  92. {
  93. EditorGUIUtility.ShowObjectPicker<ScriptableRendererData>(null, false, null, index);
  94. }
  95. }
  96. // If object selector chose an object, assign it to the correct ScriptableRendererData slot.
  97. if (Event.current.commandName == "ObjectSelectorUpdated" && EditorGUIUtility.GetObjectPickerControlID() == index)
  98. {
  99. m_RendererDataProp.GetArrayElementAtIndex(index).objectReferenceValue = EditorGUIUtility.GetObjectPickerObject();
  100. }
  101. }
  102. void UpdateDefaultRendererValue(int index)
  103. {
  104. // If the index that is being removed is lower than the default renderer value,
  105. // the default prop value needs to be one lower.
  106. if (index < m_DefaultRendererProp.intValue)
  107. {
  108. m_DefaultRendererProp.intValue--;
  109. }
  110. }
  111. void UpdateDefaultRendererValue(int prevIndex, int newIndex)
  112. {
  113. // If we are moving the index that is the same as the default renderer we need to update that
  114. if (prevIndex == m_DefaultRendererProp.intValue)
  115. {
  116. m_DefaultRendererProp.intValue = newIndex;
  117. }
  118. // If newIndex is the same as default
  119. // then we need to know if newIndex is above or below the default index
  120. else if (newIndex == m_DefaultRendererProp.intValue)
  121. {
  122. m_DefaultRendererProp.intValue += prevIndex > newIndex ? 1 : -1;
  123. }
  124. // If the old index is lower than default renderer and
  125. // the new index is higher then we need to move the default renderer index one lower
  126. else if (prevIndex < m_DefaultRendererProp.intValue && newIndex > m_DefaultRendererProp.intValue)
  127. {
  128. m_DefaultRendererProp.intValue--;
  129. }
  130. else if (newIndex < m_DefaultRendererProp.intValue && prevIndex > m_DefaultRendererProp.intValue)
  131. {
  132. m_DefaultRendererProp.intValue++;
  133. }
  134. }
  135. }
  136. }