Skip to content

Commit

Permalink
[content automatically redacted] touching PlatformDependent folder
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-vazquez-unity3d authored and Evergreen committed Feb 3, 2023
1 parent 3973104 commit def6175
Show file tree
Hide file tree
Showing 24 changed files with 480 additions and 345 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using UnityEngine;
using UnityEngine.Rendering;

namespace UnityEditor.Rendering
{
/// <summary>
/// <see cref="ProjectWindowCallback.EndNameEditAction"/> for <see cref="RenderPipelineGlobalSettings"/>
/// </summary>
public class RenderPipelineGlobalSettingsEndNameEditAction : ProjectWindowCallback.EndNameEditAction
{
private Type renderPipelineType { get; set; }
private Type renderPipelineGlobalSettingsType { get; set; }
private RenderPipelineGlobalSettings source { get; set; }

/// <inheritdoc/>
public override void Action(int instanceId, string pathName, string resourceFile)
{
RenderPipelineGlobalSettings assetCreated = RenderPipelineGlobalSettingsUtils.Create(renderPipelineGlobalSettingsType, pathName, source) as RenderPipelineGlobalSettings;

if (renderPipelineType != null)
{
if (assetCreated != null)
GraphicsSettings.RegisterRenderPipelineSettings(renderPipelineType, assetCreated);
else
GraphicsSettings.UnregisterRenderPipelineSettings(renderPipelineType);
}

ProjectWindowUtil.ShowCreatedAsset(assetCreated);
}

private static string s_DefaultAssetName = "{0}.asset";

internal static RenderPipelineGlobalSettingsEndNameEditAction CreateEndNameEditAction(
Type renderPipelineType, Type renderPipelineGlobalSettingsType, bool updateAssetOnGraphicsSettings, RenderPipelineGlobalSettings source = null)
{
var action = ScriptableObject.CreateInstance<RenderPipelineGlobalSettingsEndNameEditAction>();
action.renderPipelineGlobalSettingsType = renderPipelineGlobalSettingsType;

if (updateAssetOnGraphicsSettings)
action.renderPipelineType = renderPipelineType;

if (source != null)
action.source = source;

return action;
}

internal static void StartEndNameEditAction(RenderPipelineGlobalSettingsEndNameEditAction action, string pathName)
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
action.GetInstanceID(),
action,
pathName,
CoreEditorStyles.globalSettingsIcon,
null);
}

/// <summary>
/// Creates a new <see cref="RenderPipelineGlobalSettings"/> of the given type for the given pipeline
/// </summary>
/// <typeparam name="TRenderPipeline"></typeparam>
/// <typeparam name="TGlobalSettings"></typeparam>
/// <param name="usePathFromCurrentSettings"></param>
/// <param name="updateAssetOnGraphicsSettings"></param>
public static void CreateNew<TRenderPipeline, TGlobalSettings>(bool usePathFromCurrentSettings = false, bool updateAssetOnGraphicsSettings = false)
where TRenderPipeline : RenderPipeline
where TGlobalSettings : RenderPipelineGlobalSettings
{
string pathName = string.Format(s_DefaultAssetName, typeof(TGlobalSettings).Name);

if (usePathFromCurrentSettings)
{
var currentSettings = GraphicsSettings.GetSettingsForRenderPipeline(typeof(TRenderPipeline));
if (currentSettings != null)
pathName = AssetDatabase.GenerateUniqueAssetPath(AssetDatabase.GetAssetPath(currentSettings));
}

StartEndNameEditAction(CreateEndNameEditAction(typeof(TRenderPipeline), typeof(TGlobalSettings), updateAssetOnGraphicsSettings), pathName);
}

internal static void CloneFrom<TRenderPipeline, TGlobalSettings>(RenderPipelineGlobalSettings source, bool updateAssetOnGraphicsSettings = false)
where TRenderPipeline : RenderPipeline
where TGlobalSettings : RenderPipelineGlobalSettings
{
var pathName = AssetDatabase.GenerateUniqueAssetPath(AssetDatabase.GetAssetPath(source));
StartEndNameEditAction(CreateEndNameEditAction(typeof(TRenderPipeline), typeof(TGlobalSettings), updateAssetOnGraphicsSettings, source), pathName);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ public override void OnDeactivate()
/// </summary>
/// <param name="useProjectSettingsFolder">If the asset should be created on the project settings folder</param>
/// <param name="activateAsset">if the asset should be shown on the inspector</param>
protected abstract void Create(bool useProjectSettingsFolder, bool activateAsset);
protected virtual void Create(bool useProjectSettingsFolder, bool activateAsset)
{
RenderPipelineGlobalSettingsEndNameEditAction.CreateNew<TRenderPipeline, TGlobalSettings>(useProjectSettingsFolder, activateAsset);
}

/// <summary>
/// Clones the <see cref="RenderPipelineGlobalSettings"/> asset
/// </summary>
/// <param name="src">The <see cref="RenderPipelineGlobalSettings"/> to clone.</param>
/// <param name="source">The <see cref="RenderPipelineGlobalSettings"/> to clone.</param>
/// <param name="activateAsset">if the asset should be shown on the inspector.</param>
protected abstract void Clone(RenderPipelineGlobalSettings src, bool activateAsset);
protected virtual void Clone(RenderPipelineGlobalSettings source, bool activateAsset)
{
RenderPipelineGlobalSettingsEndNameEditAction.CloneFrom<TRenderPipeline, TGlobalSettings>(source, activateAsset);
}

/// <summary>
/// Method called to render the IMGUI of the settings provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace UnityEngine.Rendering
{
/// <summary>
/// A <see cref="ScriptableObject"/> to associate with a <see cref="RenderPipeline"/> and store project-wide settings for that pipeline.
/// You can register a single <see cref="RenderPipelineGlobalSettings"/> instance to the <see cref="GraphicsSettings"/> by using <see cref="Rendering.GraphicsSettings.RegisterRenderPipelineSettings"/>. You can use this to save `RenderPipeline` settings that appear in `GraphicsSettings`.
/// </summary>
/// <typeparam name="TGlobalRenderPipelineSettings"><see cref="RenderPipelineGlobalSettings"/></typeparam>
/// <typeparam name="TRenderPipeline"><see cref="RenderPipeline"/></typeparam>
public abstract class RenderPipelineGlobalSettings<TGlobalRenderPipelineSettings, TRenderPipeline> : RenderPipelineGlobalSettings
where TRenderPipeline : RenderPipeline
where TGlobalRenderPipelineSettings : RenderPipelineGlobalSettings
{
/// <summary>
/// Active Global Settings asset. If the value is `null` then no `TGlobalRenderPipelineSettings` is registered to the Graphics Settings with the `TRenderPipeline`.
/// </summary>
#if UNITY_EDITOR
public static TGlobalRenderPipelineSettings instance =>
GraphicsSettings.GetSettingsForRenderPipeline<TRenderPipeline>() as TGlobalRenderPipelineSettings;
#else
public static TGlobalRenderPipelineSettings instance => s_Instance.Value;
private static Lazy<TGlobalRenderPipelineSettings> s_Instance = new (() => GraphicsSettings.GetSettingsForRenderPipeline<TRenderPipeline>() as TGlobalRenderPipelineSettings);
#endif
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#if UNITY_EDITOR
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;

namespace UnityEngine.Rendering
{
/// <summary>
/// Set of utilities for <see cref="RenderPipelineGlobalSettings"/>
/// </summary>
public class RenderPipelineGlobalSettingsUtils
{
/// <summary>
/// Creates a <see cref="RenderPipelineGlobalSettings"/> asset
/// </summary>
/// <param name="path">The path where Unity generates the asset.</param>
/// <param name="dataSource">Another `RenderPipelineGlobalSettings` that Unity uses as a data source.</param>
/// <typeparam name="TGlobalSetting"><see cref="RenderPipelineGlobalSettings"/> </typeparam>
/// <returns>Returns the asset created.</returns>
public static TGlobalSetting Create<TGlobalSetting>(string path, TGlobalSetting dataSource = null)
where TGlobalSetting : RenderPipelineGlobalSettings
{
return Create(typeof(TGlobalSetting), path, dataSource) as TGlobalSetting;
}

/// <summary>
/// Creates a <see cref="RenderPipelineGlobalSettings"/> asset
/// </summary>
/// <param name="renderPipelineGlobalSettingsType"></param>
/// <param name="path">The path where Unity generates the asset.</param>
/// <param name="dataSource">Another `RenderPipelineGlobalSettings` that Unity uses as a data source.</param>
/// <returns>Returns the asset created.</returns>
public static RenderPipelineGlobalSettings Create(Type renderPipelineGlobalSettingsType, string path, RenderPipelineGlobalSettings dataSource = null)
{
if (!typeof(RenderPipelineGlobalSettings).IsAssignableFrom(renderPipelineGlobalSettingsType))
throw new ArgumentException(
$"{nameof(renderPipelineGlobalSettingsType)} must be a valid {typeof(RenderPipelineGlobalSettings)}");

// Sanitize the path
if (string.IsNullOrEmpty(path))
path = $"Assets/{renderPipelineGlobalSettingsType.Name}.asset";

if (!path.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
path = $"Assets/{path}";

CoreUtils.EnsureFolderTreeInAssetFilePath(path);
path = AssetDatabase.GenerateUniqueAssetPath(path);

var assetCreated = ScriptableObject.CreateInstance(renderPipelineGlobalSettingsType) as RenderPipelineGlobalSettings;
if (assetCreated)
{
AssetDatabase.CreateAsset(assetCreated, path);

// copy data from provided source
if (dataSource != null)
EditorUtility.CopySerializedManagedFieldsOnly(dataSource, assetCreated);

assetCreated.Initialize(dataSource);

AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

return assetCreated;
}

/// <summary>
/// Checks that a <see cref="RenderPipelineGlobalSettings"/> asset exists.
/// If the asset isn't valid, Unity tries the following in order:
/// 1. Loads the asset at the default path.
/// 2. Finds any asset in the project with the same type.
/// 3. If `canCreateNewAsset` is true, creates a new asset in the default path.
/// If Unity finds or creates a valid asset, Unity updates the <see cref="GraphicsSettings"/> with it. Otherwise Unity will unregister the settings for the given pipeline.
/// </summary>
/// <param name="instance">The current instance of the asset.</param>
/// <param name="defaultPath">The default path.</param>
/// <param name="canCreateNewAsset">If set to `true`, Unity creates a new asset if it can't find an existing asset in the project.</param>
/// <typeparam name="TGlobalSetting">The type of global settings asset to check.</typeparam>
/// <typeparam name="TRenderPipeline">The type of `RenderPipeline` that this asset belongs to.</typeparam>
/// <returns>The asset that Unity found or created, or `null` if Unity can't find or create a valid asset.</returns>
public static bool TryEnsure<TGlobalSetting, TRenderPipeline>(ref TGlobalSetting instance, string defaultPath = "", bool canCreateNewAsset = true)
where TGlobalSetting : RenderPipelineGlobalSettings<TGlobalSetting, TRenderPipeline>
where TRenderPipeline : RenderPipeline
{
if (!TryEnsure<TGlobalSetting, TRenderPipeline>(ref instance, defaultPath, canCreateNewAsset, out var error))
{
Debug.LogError(error.Message);
return false;
}

return true;
}

// This method is exposed to Unit Tests
internal static bool TryEnsure<TGlobalSetting, TRenderPipeline>(ref TGlobalSetting instance, string defaultPath, bool canCreateNewAsset, out Exception error)
where TGlobalSetting : RenderPipelineGlobalSettings<TGlobalSetting, TRenderPipeline>
where TRenderPipeline : RenderPipeline
{
var globalSettingsName = typeof(TGlobalSetting).GetCustomAttribute<DisplayInfoAttribute>()?.name ?? typeof(TGlobalSetting).Name;

if (instance == null || instance.Equals(null))
{
if (!string.IsNullOrEmpty(defaultPath))
{
// Look at default path, if the asset exist, is the one that we need
instance = AssetDatabase.LoadAssetAtPath<TGlobalSetting>(defaultPath);
}

if (instance == null)
{
// There was not saved into the default path, fetch the asset database to see if there is one defined in the project
instance = CoreUtils.LoadAllAssets<TGlobalSetting>().FirstOrDefault();
if (instance == null)
{
// Try to create one if possible
if (canCreateNewAsset)
{
instance = Create<TGlobalSetting>(defaultPath);
if (instance != null)
{
Debug.LogWarning($"{globalSettingsName} has been created for you. If you want to modify it, go to Project Settings > Graphics > {globalSettingsName}");
}
}
}
}
}

if (instance == null || instance.Equals(null))
{
error = new Exception($"Unable to find or create a {globalSettingsName}. The configured Render Pipeline may not work correctly. Go to Project Settings > Graphics > {globalSettingsName} for additional help.");
GraphicsSettings.UnregisterRenderPipelineSettings<TRenderPipeline>();
return false;
}

error = null;
GraphicsSettings.RegisterRenderPipelineSettings<TRenderPipeline>(instance);
return true;
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit def6175

Please sign in to comment.