using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering.PostProcessing
{
///
/// An asset holding a set of post-processing settings to use with a .
///
///
public sealed class PostProcessProfile : ScriptableObject
{
///
/// A list of all settings stored in this profile.
///
[Tooltip("A list of all settings currently stored in this profile.")]
public List settings = new List();
///
/// Sets to true if the content of the profile has changed. This is only really used
/// in the editor to handle inspector refreshes.
///
[NonSerialized]
public bool isDirty = true;
void OnEnable()
{
// Make sure every setting is valid. If a profile holds a script that doesn't exist
// anymore, nuke it to keep the profile clean. Note that if you delete a script that is
// currently in use in a profile you'll still get a one-time error in the console, it's
// harmless and happens because Unity does a redraw of the editor (and thus the current
// frame) before the recompilation step.
settings.RemoveAll(x => x == null);
}
///
/// Adds settings for an effect to the profile.
///
/// A type of
/// The instance created from the given type
///
public T AddSettings()
where T : PostProcessEffectSettings
{
return (T)AddSettings(typeof(T));
}
///
/// Adds settings for an effect to the profile.
///
/// A type of
/// The instance created from the given type
///
public PostProcessEffectSettings AddSettings(Type type)
{
if (HasSettings(type))
throw new InvalidOperationException("Effect already exists in the stack");
var effect = (PostProcessEffectSettings)CreateInstance(type);
effect.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
effect.name = type.Name;
effect.enabled.value = true;
settings.Add(effect);
isDirty = true;
return effect;
}
///
/// Adds settings for an effect to the profile.
///
/// An instance of
/// The given effect instance
///
public PostProcessEffectSettings AddSettings(PostProcessEffectSettings effect)
{
if (HasSettings(settings.GetType()))
throw new InvalidOperationException("Effect already exists in the stack");
settings.Add(effect);
isDirty = true;
return effect;
}
///
/// Removes settings for an effect from the profile.
///
/// The type to look for and remove from the profile
/// Thrown if the effect doesn't exist in the
/// profile
public void RemoveSettings()
where T : PostProcessEffectSettings
{
RemoveSettings(typeof(T));
}
///
/// Removes settings for an effect from the profile.
///
/// The type to look for and remove from the profile
/// Thrown if the effect doesn't exist in the
/// profile
public void RemoveSettings(Type type)
{
int toRemove = -1;
for (int i = 0; i < settings.Count; i++)
{
if (settings[i].GetType() == type)
{
toRemove = i;
break;
}
}
if (toRemove < 0)
throw new InvalidOperationException("Effect doesn't exist in the profile");
settings.RemoveAt(toRemove);
isDirty = true;
}
///
/// Checks if an effect has been added to the profile.
///
/// The type to look for
/// true if the effect exists in the profile, false otherwise
public bool HasSettings()
where T : PostProcessEffectSettings
{
return HasSettings(typeof(T));
}
///
/// Checks if an effect has been added to the profile.
///
/// The type to look for
/// true if the effect exists in the profile, false otherwise
public bool HasSettings(Type type)
{
foreach (var setting in settings)
{
if (setting.GetType() == type)
return true;
}
return false;
}
///
/// Returns settings for a given effect type.
///
/// The type to look for
/// Settings for the given effect type, null otherwise
public T GetSetting() where T : PostProcessEffectSettings
{
foreach (var setting in settings)
{
if (setting is T)
return setting as T;
}
return null;
}
///
/// Gets settings for a given effect type.
///
/// The type to look for
/// When this method returns, contains the value associated with
/// the specified type, if the type is found; otherwise, this parameter will be null.
/// This parameter is passed uninitialized.
/// true if the effect exists in the profile, false otherwise
public bool TryGetSettings(out T outSetting)
where T : PostProcessEffectSettings
{
var type = typeof(T);
outSetting = null;
foreach (var setting in settings)
{
if (setting.GetType() == type)
{
outSetting = (T)setting;
return true;
}
}
return false;
}
}
}