ProfileFactory.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. using UnityEditor.ProjectWindowCallback;
  3. using System.IO;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.Rendering.PostProcessing;
  6. namespace UnityEditor.Rendering.PostProcessing
  7. {
  8. /// <summary>
  9. /// An utility class to help the creation of new post-processing profile assets.
  10. /// </summary>
  11. public sealed class ProfileFactory
  12. {
  13. [MenuItem("Assets/Create/Post-processing Profile", priority = 201)]
  14. static void CreatePostProcessProfile()
  15. {
  16. //var icon = EditorGUIUtility.FindTexture("ScriptableObject Icon");
  17. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<DoCreatePostProcessProfile>(), "New Post-processing Profile.asset", null, null);
  18. }
  19. /// <summary>
  20. /// Creates a post-processing profile asset at the given location.
  21. /// </summary>
  22. /// <param name="path">The path to use relative to the project folder</param>
  23. /// <returns>The newly created profile</returns>
  24. public static PostProcessProfile CreatePostProcessProfileAtPath(string path)
  25. {
  26. var profile = ScriptableObject.CreateInstance<PostProcessProfile>();
  27. profile.name = Path.GetFileName(path);
  28. AssetDatabase.CreateAsset(profile, path);
  29. AssetDatabase.SaveAssets();
  30. AssetDatabase.Refresh();
  31. return profile;
  32. }
  33. /// <summary>
  34. /// Creates a post-processing profile asset and automatically put it in a sub folder next
  35. /// to the given scene.
  36. /// </summary>
  37. /// <param name="scene">A scene</param>
  38. /// <param name="targetName">A name for the new profile</param>
  39. /// <returns>The newly created profile</returns>
  40. public static PostProcessProfile CreatePostProcessProfile(Scene scene, string targetName)
  41. {
  42. var path = string.Empty;
  43. if (string.IsNullOrEmpty(scene.path))
  44. {
  45. path = "Assets/";
  46. }
  47. else
  48. {
  49. var scenePath = Path.GetDirectoryName(scene.path);
  50. var extPath = scene.name + "_Profiles";
  51. var profilePath = scenePath + "/" + extPath;
  52. if (!AssetDatabase.IsValidFolder(profilePath))
  53. AssetDatabase.CreateFolder(scenePath, extPath);
  54. path = profilePath + "/";
  55. }
  56. path += targetName + " Profile.asset";
  57. path = AssetDatabase.GenerateUniqueAssetPath(path);
  58. var profile = ScriptableObject.CreateInstance<PostProcessProfile>();
  59. AssetDatabase.CreateAsset(profile, path);
  60. AssetDatabase.SaveAssets();
  61. AssetDatabase.Refresh();
  62. return profile;
  63. }
  64. }
  65. class DoCreatePostProcessProfile : EndNameEditAction
  66. {
  67. public override void Action(int instanceId, string pathName, string resourceFile)
  68. {
  69. var profile = ProfileFactory.CreatePostProcessProfileAtPath(pathName);
  70. ProjectWindowUtil.ShowCreatedAsset(profile);
  71. }
  72. }
  73. }