ShaderGraphAssetPostProcessor.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEditor.ShaderGraph.Drawing;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. class ShaderGraphAssetPostProcessor : AssetPostprocessor
  10. {
  11. static void RegisterShaders(string[] paths)
  12. {
  13. foreach (var path in paths)
  14. {
  15. if (!path.EndsWith(ShaderGraphImporter.Extension, StringComparison.InvariantCultureIgnoreCase))
  16. continue;
  17. var mainObj = AssetDatabase.LoadMainAssetAtPath(path);
  18. if (mainObj is Shader)
  19. ShaderUtil.RegisterShader((Shader)mainObj);
  20. var objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
  21. foreach (var obj in objs)
  22. {
  23. if (obj is Shader)
  24. ShaderUtil.RegisterShader((Shader)obj);
  25. }
  26. }
  27. }
  28. static void UpdateAfterAssetChange(string[] newNames)
  29. {
  30. // This will change the title of the window.
  31. MaterialGraphEditWindow[] windows = Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>();
  32. foreach (var matGraphEditWindow in windows)
  33. {
  34. for (int i = 0; i < newNames.Length; ++i)
  35. {
  36. if (matGraphEditWindow.selectedGuid == AssetDatabase.AssetPathToGUID(newNames[i]))
  37. matGraphEditWindow.assetName = Path.GetFileNameWithoutExtension(newNames[i]).Split('/').Last();
  38. }
  39. }
  40. }
  41. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  42. {
  43. RegisterShaders(importedAssets);
  44. bool anyShaders = movedAssets.Any(val => val.EndsWith(ShaderGraphImporter.Extension, StringComparison.InvariantCultureIgnoreCase));
  45. anyShaders |= movedAssets.Any(val => val.EndsWith(ShaderSubGraphImporter.Extension, StringComparison.InvariantCultureIgnoreCase));
  46. if (anyShaders)
  47. UpdateAfterAssetChange(movedAssets);
  48. var changedFiles = movedAssets.Union(importedAssets)
  49. .Where(x => x.EndsWith(ShaderSubGraphImporter.Extension, StringComparison.InvariantCultureIgnoreCase)
  50. || CustomFunctionNode.s_ValidExtensions.Contains(Path.GetExtension(x)))
  51. .Select(AssetDatabase.AssetPathToGUID)
  52. .Distinct()
  53. .ToList();
  54. if (changedFiles.Count > 0)
  55. {
  56. var windows = Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>();
  57. foreach (var window in windows)
  58. {
  59. window.ReloadSubGraphsOnNextUpdate(changedFiles);
  60. }
  61. }
  62. }
  63. }
  64. }