FilmGrainEditor.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using UnityEngine.Rendering.Universal;
  3. namespace UnityEditor.Rendering.Universal
  4. {
  5. [VolumeComponentEditor(typeof(FilmGrain))]
  6. sealed class FilmGrainEditor : VolumeComponentEditor
  7. {
  8. SerializedDataParameter m_Type;
  9. SerializedDataParameter m_Intensity;
  10. SerializedDataParameter m_Response;
  11. SerializedDataParameter m_Texture;
  12. public override void OnEnable()
  13. {
  14. var o = new PropertyFetcher<FilmGrain>(serializedObject);
  15. m_Type = Unpack(o.Find(x => x.type));
  16. m_Intensity = Unpack(o.Find(x => x.intensity));
  17. m_Response = Unpack(o.Find(x => x.response));
  18. m_Texture = Unpack(o.Find(x => x.texture));
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. if (UniversalRenderPipeline.asset?.postProcessingFeatureSet == PostProcessingFeatureSet.PostProcessingV2)
  23. {
  24. EditorGUILayout.HelpBox(UniversalRenderPipelineAssetEditor.Styles.postProcessingGlobalWarning, MessageType.Warning);
  25. return;
  26. }
  27. PropertyField(m_Type);
  28. if (m_Type.value.intValue == (int)FilmGrainLookup.Custom)
  29. {
  30. PropertyField(m_Texture);
  31. var texture = (target as FilmGrain).texture.value;
  32. if (texture != null)
  33. {
  34. var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
  35. // Fails when using an internal texture as you can't change import settings on
  36. // builtin resources, thus the check for null
  37. if (importer != null)
  38. {
  39. bool valid = importer.mipmapEnabled == false
  40. && importer.alphaSource == TextureImporterAlphaSource.FromGrayScale
  41. && importer.filterMode == FilterMode.Point
  42. && importer.textureCompression == TextureImporterCompression.Uncompressed
  43. && importer.textureType == TextureImporterType.SingleChannel;
  44. if (!valid)
  45. CoreEditorUtils.DrawFixMeBox("Invalid texture import settings.", () => SetTextureImportSettings(importer));
  46. }
  47. }
  48. }
  49. PropertyField(m_Intensity);
  50. PropertyField(m_Response);
  51. }
  52. static void SetTextureImportSettings(TextureImporter importer)
  53. {
  54. importer.textureType = TextureImporterType.SingleChannel;
  55. importer.alphaSource = TextureImporterAlphaSource.FromGrayScale;
  56. importer.mipmapEnabled = false;
  57. importer.filterMode = FilterMode.Point;
  58. importer.textureCompression = TextureImporterCompression.Uncompressed;
  59. importer.SaveAndReimport();
  60. AssetDatabase.Refresh();
  61. }
  62. }
  63. }