using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.Rendering
{
///
/// Render Pipeline Settings provider
///
///
///
public abstract class RenderPipelineGlobalSettingsProvider : SettingsProvider
where TRenderPipeline : RenderPipeline
where TGlobalSettings : RenderPipelineGlobalSettings
{
static class Styles
{
public static readonly string warningGlobalSettingsMissing = "Select a valid {0} asset.";
public static readonly string warningSRPNotActive = "Current Render Pipeline is {0}. Check the settings: Graphics > Scriptable Render Pipeline Settings, Quality > Render Pipeline Asset.";
public static readonly string settingNullRPSettings = "Are you sure you want to unregister the Render Pipeline Settings? There might be issues with rendering.";
public static readonly GUIContent newAssetButtonLabel = EditorGUIUtility.TrTextContent("New", "Create a Global Settings asset in the Assets folder.");
public static readonly GUIContent cloneAssetButtonLabel = EditorGUIUtility.TrTextContent("Clone", "Clone a Global Settings asset in the Assets folder.");
public static readonly GUILayoutOption[] buttonOptions = new GUILayoutOption[] { GUILayout.Width(45), GUILayout.Height(18) };
}
Editor m_Editor;
RenderPipelineGlobalSettings renderPipelineSettings => GraphicsSettings.GetSettingsForRenderPipeline();
///
/// Constructor
///
/// The path of the settings
public RenderPipelineGlobalSettingsProvider(string v)
: base(v, SettingsScope.Project)
{
}
///
/// Method called when the title bar is being rendered
///
public override void OnTitleBarGUI()
{
if (GUILayout.Button(CoreEditorStyles.iconHelp, CoreEditorStyles.iconHelpStyle))
Help.BrowseURL(Help.GetHelpURLForObject(renderPipelineSettings));
}
void DestroyEditor()
{
if (m_Editor == null)
return;
UnityEngine.Object.DestroyImmediate(m_Editor);
m_Editor = null;
}
///
/// This method is being called when the provider is activated
///
/// The context with the search
/// The with the root
public override void OnActivate(string searchContext, VisualElement rootElement)
{
DestroyEditor();
base.OnActivate(searchContext, rootElement);
}
///
/// This method is being called when the provider is deactivated
///
public override void OnDeactivate()
{
DestroyEditor();
base.OnDeactivate();
}
///
/// Ensures that the asset is correct
///
protected abstract void Ensure();
///
/// Creates a new asset
///
/// If the asset should be created on the project settings folder
/// if the asset should be shown on the inspector
protected abstract void Create(bool useProjectSettingsFolder, bool activateAsset);
///
/// Clones the asset
///
/// The to clone.
/// if the asset should be shown on the inspector.
protected abstract void Clone(RenderPipelineGlobalSettings src, bool activateAsset);
///
/// Method called to render the IMGUI of the settings provider
///
/// The search content
public override void OnGUI(string searchContext)
{
using (new SettingsProviderGUIScope())
{
if (renderPipelineSettings == null)
{
CoreEditorUtils.DrawFixMeBox(string.Format(Styles.warningGlobalSettingsMissing, ObjectNames.NicifyVariableName(typeof(TGlobalSettings).Name)), () => Ensure());
DestroyEditor();
}
else
{
DrawAssetSelection();
if (RenderPipelineManager.currentPipeline != null && !(RenderPipelineManager.currentPipeline is TRenderPipeline))
{
EditorGUILayout.HelpBox(string.Format(Styles.warningSRPNotActive, ObjectNames.NicifyVariableName(RenderPipelineManager.currentPipeline.GetType().Name)), MessageType.Warning);
}
if (m_Editor != null && (m_Editor.target == null || m_Editor.target != renderPipelineSettings))
DestroyEditor();
if (m_Editor == null)
m_Editor = Editor.CreateEditor(renderPipelineSettings);
m_Editor?.OnInspectorGUI();
}
}
base.OnGUI(searchContext);
}
void DrawAssetSelection()
{
var oldRenderPipelineSettings = renderPipelineSettings;
using (new EditorGUILayout.HorizontalScope())
{
var newSettings = (TGlobalSettings)EditorGUILayout.ObjectField(renderPipelineSettings, typeof(TGlobalSettings), false);
if (renderPipelineSettings != newSettings)
{
if (newSettings != null)
GraphicsSettings.RegisterRenderPipelineSettings(newSettings);
else
{
if (EditorUtility.DisplayDialog($"Invalid {ObjectNames.NicifyVariableName(typeof(TGlobalSettings).Name)}", Styles.settingNullRPSettings, "Yes", "No"))
GraphicsSettings.UnregisterRenderPipelineSettings();
}
if (renderPipelineSettings != null && !renderPipelineSettings.Equals(null))
EditorUtility.SetDirty(renderPipelineSettings);
}
if (GUILayout.Button(Styles.newAssetButtonLabel, Styles.buttonOptions))
{
Create(useProjectSettingsFolder: false, activateAsset: true);
}
bool guiEnabled = GUI.enabled;
GUI.enabled = guiEnabled && (renderPipelineSettings != null);
if (GUILayout.Button(Styles.cloneAssetButtonLabel, Styles.buttonOptions))
{
Clone(renderPipelineSettings, activateAsset: true);
}
GUI.enabled = guiEnabled;
}
if (oldRenderPipelineSettings != renderPipelineSettings)
{
DestroyEditor();
}
}
}
}