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/Volume Profile", priority = 201)]
static void CreateVolumeProfile()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
0,
ScriptableObject.CreateInstance(),
"New Volume Profile.asset",
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)
{
var profile = ScriptableObject.CreateInstance();
profile.name = Path.GetFileName(path);
AssetDatabase.CreateAsset(profile, path);
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)
{
string path;
if (string.IsNullOrEmpty(scene.path))
{
path = "Assets/";
}
else
{
var scenePath = Path.GetDirectoryName(scene.path);
var extPath = scene.name;
var profilePath = scenePath + Path.DirectorySeparatorChar + extPath;
if (!AssetDatabase.IsValidFolder(profilePath))
{
var directories = profilePath.Split(Path.DirectorySeparatorChar);
string rootPath = "";
foreach (var directory in directories)
{
var newPath = rootPath + directory;
if (!AssetDatabase.IsValidFolder(newPath))
AssetDatabase.CreateFolder(rootPath.TrimEnd(Path.DirectorySeparatorChar), directory);
rootPath = newPath + Path.DirectorySeparatorChar;
}
}
path = profilePath + Path.DirectorySeparatorChar;
}
path += targetName + " Profile.asset";
path = AssetDatabase.GenerateUniqueAssetPath(path);
var profile = ScriptableObject.CreateInstance();
AssetDatabase.CreateAsset(profile, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return profile;
}
///
/// 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.
///
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 DoCreatePostProcessProfile : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
var profile = VolumeProfileFactory.CreateVolumeProfileAtPath(pathName);
ProjectWindowUtil.ShowCreatedAsset(profile);
}
}
}