No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewPostProcessTemplateDropdownItems.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using Object = UnityEngine.Object;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. internal static class NewPostProcessTemplateDropdownItems
  8. {
  9. const string k_FeatureTemplatePath =
  10. "Packages/com.unity.render-pipelines.universal/Editor/RendererFeatures/NewPostProcessRendererFeature.cs.txt";
  11. const string k_VolumeTemplatePath =
  12. "Packages/com.unity.render-pipelines.universal/Editor/RendererFeatures/NewPostProcessVolumeComponent.cs.txt";
  13. static string PreprocessScriptTemplate(string content, string featureType = null, string volumeType = null, string displayName = null)
  14. {
  15. if(featureType != null)
  16. content = content.Replace("#FEATURE_TYPE#", featureType);
  17. if(volumeType != null)
  18. content = content.Replace("#VOLUME_TYPE#", volumeType);
  19. if(displayName != null)
  20. content = content.Replace("#DISPLAY_NAME#", displayName);
  21. return content;
  22. }
  23. static Object CreateScriptAssetFromTemplate(string templatePath, string targetPath, string featureType = null, string volumeType = null, string displayName = null)
  24. {
  25. string content = File.ReadAllText(templatePath);
  26. return ProjectWindowUtil.CreateScriptAssetWithContent(targetPath, PreprocessScriptTemplate(content, featureType, volumeType, displayName));
  27. }
  28. internal class CreateCombinedScriptTemplateAssetsAction : ProjectWindowCallback.EndNameEditAction
  29. {
  30. public override void Action(int instanceId, string userPath, string resourceFile)
  31. {
  32. string directoryPath = Path.GetDirectoryName(userPath);
  33. string enteredName = Path.GetFileNameWithoutExtension(userPath);
  34. string cleanedEnteredNamed = enteredName.Replace(" ", "");
  35. string featureTypeName = cleanedEnteredNamed + "RendererFeature";
  36. string volumeTypeName = cleanedEnteredNamed + nameof(VolumeComponent);
  37. try
  38. {
  39. AssetDatabase.StartAssetEditing();
  40. Object o = CreateScriptAssetFromTemplate(k_FeatureTemplatePath,
  41. Path.Combine(directoryPath, featureTypeName + ".cs"), featureTypeName, volumeTypeName, null);
  42. CreateScriptAssetFromTemplate(k_VolumeTemplatePath,
  43. Path.Combine(directoryPath, volumeTypeName + ".cs"), featureTypeName, volumeTypeName,
  44. enteredName);
  45. ProjectWindowUtil.ShowCreatedAsset(o);
  46. }
  47. finally
  48. {
  49. AssetDatabase.StopAssetEditing();
  50. }
  51. }
  52. }
  53. [MenuItem("Assets/Create/Rendering/URP Post-processing Effect (Renderer Feature with Volume)", priority = CoreUtils.Sections.section4 + CoreUtils.Priorities.assetsCreateRenderingMenuPriority)]
  54. static void MenuCreateCustomPostProcessVolumeRendererFeature()
  55. {
  56. Texture2D icon = EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D;
  57. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
  58. ScriptableObject.CreateInstance<CreateCombinedScriptTemplateAssetsAction>(), "NewPostProcessEffect.cs", icon, k_FeatureTemplatePath);
  59. }
  60. }
  61. }