using System;
using UnityEngine;
using UnityEditor.ProjectWindowCallback;
using System.IO;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
namespace UnityEditor.Rendering
{
///
/// A utility class to create Volume Profiles and components.
///
public static class VolumeProfileFactory
{
[MenuItem("Assets/Create/Rendering/Volume Profile", priority = 201)]
static void CreateVolumeProfile()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
0,
ScriptableObject.CreateInstance(),
"New Volume Profile.asset",
null,
null
);
}
///
/// Asks for editor user input for the asset name, creates a Asset, saves it at the
/// given path and invokes the callback.
///
/// The path to save the asset to.
/// Callback to invoke after the asset has been created.
public static void CreateVolumeProfileWithCallback(string fullPath, Action callback)
{
var assetCreator = ScriptableObject.CreateInstance();
assetCreator.callback = callback;
CoreUtils.EnsureFolderTreeInAssetFilePath(fullPath);
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
assetCreator.GetInstanceID(),
assetCreator,
fullPath,
null,
null);
}
///
/// Creates a Asset and saves it at the given path.
///
/// The path to save the Asset to, relative to the Project folder.
/// The newly created .
public static VolumeProfile CreateVolumeProfileAtPath(string path) => CreateVolumeProfileAtPath(path, null);
///
/// Creates a Asset and saves it at the given path.
///
/// The path to save the Asset to, relative to the Project folder.
/// Another `VolumeProfile` that Unity uses as a data source.
/// The newly created .
public static VolumeProfile CreateVolumeProfileAtPath(string path, VolumeProfile dataSource)
{
var profile = ScriptableObject.CreateInstance();
profile.name = Path.GetFileName(path);
AssetDatabase.CreateAsset(profile, path);
if (dataSource != null)
{
foreach (var sourceComponent in dataSource.components)
{
var profileComponent = profile.Add(sourceComponent.GetType());
for (int i = 0; i < sourceComponent.parameters.Count; i++)
profileComponent.parameters[i].overrideState = sourceComponent.parameters[i].overrideState;
VolumeProfileUtils.CopyValuesToComponent(sourceComponent, profileComponent, true);
AssetDatabase.AddObjectToAsset(profileComponent, profile);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return profile;
}
///
/// Creates a Asset and saves it in a folder next to the Scene.
///
/// The Scene to save the Profile next to.
/// A name to use for the Asset filename.
/// The newly created .
public static VolumeProfile CreateVolumeProfile(Scene scene, string targetName)
{
return CoreEditorUtils.CreateAssetAt(scene, targetName);
}
///
/// Creates a in an existing .
///
/// A type of .
/// The profile to store the new component in.
/// specifies whether to override the parameters in the component or not.
/// Specifies whether to save the Profile Asset or not. This is useful when you need to
/// create several components in a row and only want to save the Profile Asset after adding the last one,
/// because saving Assets to disk can be slow.
/// The newly created component of type .
public static T CreateVolumeComponent(VolumeProfile profile, bool overrides = false, bool saveAsset = true)
where T : VolumeComponent
{
var comp = profile.Add(overrides);
comp.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(comp, profile);
if (saveAsset)
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
return comp;
}
}
class CreateVolumeProfileAction : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
var profile = VolumeProfileFactory.CreateVolumeProfileAtPath(pathName);
ProjectWindowUtil.ShowCreatedAsset(profile);
}
}
class CreateVolumeProfileWithCallbackAction : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
var profile = VolumeProfileFactory.CreateVolumeProfileAtPath(pathName);
ProjectWindowUtil.ShowCreatedAsset(profile);
callback?.Invoke(profile);
}
internal Action callback { get; set; }
}
}