Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

NodeClassCache.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Profiling;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. [GenerationAPI]
  8. [InitializeOnLoad]
  9. internal static class NodeClassCache
  10. {
  11. private class PostProcessor : AssetPostprocessor
  12. {
  13. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  14. {
  15. if (m_KnownSubGraphLookupTable != null)
  16. {
  17. foreach (string str in deletedAssets)
  18. {
  19. var guid = AssetDatabase.AssetPathToGUID(str);
  20. if (m_KnownSubGraphLookupTable.ContainsKey(guid))
  21. {
  22. m_KnownSubGraphLookupTable.Remove(guid);
  23. }
  24. }
  25. foreach (string str in movedFromAssetPaths)
  26. {
  27. var guid = AssetDatabase.AssetPathToGUID(str);
  28. if (m_KnownSubGraphLookupTable.ContainsKey(guid))
  29. {
  30. m_KnownSubGraphLookupTable.Remove(guid);
  31. }
  32. }
  33. }
  34. foreach (string str in importedAssets)
  35. {
  36. if (str.EndsWith(ShaderSubGraphImporter.Extension))
  37. {
  38. UpdateSubGraphEntry(str);
  39. }
  40. }
  41. foreach (string str in movedAssets)
  42. {
  43. if (str.EndsWith(ShaderSubGraphImporter.Extension))
  44. {
  45. UpdateSubGraphEntry(str);
  46. }
  47. }
  48. }
  49. }
  50. private static Dictionary<Type, List<ContextFilterableAttribute>> m_KnownTypeLookupTable;
  51. private static Dictionary<Type, List<ContextFilterableAttribute>> KnownTypeLookupTable
  52. {
  53. get
  54. {
  55. EnsureKnownTypeLookupTable();
  56. return m_KnownTypeLookupTable;
  57. }
  58. }
  59. public static IEnumerable<Type> knownNodeTypes
  60. {
  61. get => KnownTypeLookupTable.Keys;
  62. }
  63. private static Dictionary<string, SubGraphAsset> m_KnownSubGraphLookupTable;
  64. private static Dictionary<string, SubGraphAsset> KnownSubGraphLookupTable
  65. {
  66. get
  67. {
  68. EnsureKnownSubGraphLookupTable();
  69. return m_KnownSubGraphLookupTable;
  70. }
  71. }
  72. public static IEnumerable<SubGraphAsset> knownSubGraphAssets
  73. {
  74. get => KnownSubGraphLookupTable.Values;
  75. }
  76. public static void UpdateSubGraphEntry(string path)
  77. {
  78. string guid = AssetDatabase.AssetPathToGUID(path);
  79. if (guid.Length == 0)
  80. {
  81. return;
  82. }
  83. var asset = AssetDatabase.LoadAssetAtPath<SubGraphAsset>(path);
  84. bool valid = asset != null && asset.isValid;
  85. if (KnownSubGraphLookupTable.TryGetValue(guid, out SubGraphAsset known))
  86. {
  87. if (!valid)
  88. {
  89. KnownSubGraphLookupTable.Remove(guid);
  90. }
  91. else if (asset != known)
  92. {
  93. KnownSubGraphLookupTable[guid] = asset;
  94. }
  95. }
  96. else if (valid)
  97. {
  98. KnownSubGraphLookupTable.Add(guid, asset);
  99. }
  100. }
  101. public static IEnumerable<ContextFilterableAttribute> GetFilterableAttributesOnNodeType(Type nodeType)
  102. {
  103. if (nodeType == null)
  104. {
  105. throw new ArgumentNullException("Cannot get attributes on a null Type");
  106. }
  107. if (KnownTypeLookupTable.TryGetValue(nodeType, out List<ContextFilterableAttribute> filterableAttributes))
  108. {
  109. return filterableAttributes;
  110. }
  111. else
  112. {
  113. throw new ArgumentException($"The passed in Type {nodeType.FullName} was not found in the loaded assemblies as a child class of AbstractMaterialNode");
  114. }
  115. }
  116. public static T GetAttributeOnNodeType<T>(Type nodeType) where T : ContextFilterableAttribute
  117. {
  118. var filterableAttributes = GetFilterableAttributesOnNodeType(nodeType);
  119. foreach (var attr in filterableAttributes)
  120. {
  121. if (attr is T searchTypeAttr)
  122. {
  123. return searchTypeAttr;
  124. }
  125. }
  126. return null;
  127. }
  128. private static void EnsureKnownTypeLookupTable()
  129. {
  130. if (m_KnownTypeLookupTable == null)
  131. {
  132. Profiler.BeginSample("EnsureKnownTypeLookupTable");
  133. m_KnownTypeLookupTable = new Dictionary<Type, List<ContextFilterableAttribute>>();
  134. foreach (Type nodeType in TypeCache.GetTypesDerivedFrom<AbstractMaterialNode>())
  135. {
  136. if (!nodeType.IsAbstract)
  137. {
  138. List<ContextFilterableAttribute> filterableAttributes = new List<ContextFilterableAttribute>();
  139. foreach (Attribute attribute in Attribute.GetCustomAttributes(nodeType))
  140. {
  141. Type attributeType = attribute.GetType();
  142. if (!attributeType.IsAbstract && attribute is ContextFilterableAttribute contextFilterableAttribute)
  143. {
  144. filterableAttributes.Add(contextFilterableAttribute);
  145. }
  146. }
  147. m_KnownTypeLookupTable.Add(nodeType, filterableAttributes);
  148. }
  149. }
  150. Profiler.EndSample();
  151. }
  152. }
  153. private static void EnsureKnownSubGraphLookupTable()
  154. {
  155. if (m_KnownSubGraphLookupTable == null)
  156. {
  157. Profiler.BeginSample("EnsureKnownSubGraphLookupTable");
  158. m_KnownSubGraphLookupTable = new Dictionary<string, SubGraphAsset>();
  159. foreach (var guid in AssetDatabase.FindAssets(string.Format("t:{0}", typeof(SubGraphAsset))))
  160. {
  161. var asset = AssetDatabase.LoadAssetAtPath<SubGraphAsset>(AssetDatabase.GUIDToAssetPath(guid));
  162. if (asset != null && asset.isValid)
  163. {
  164. m_KnownSubGraphLookupTable.Add(guid, asset);
  165. }
  166. }
  167. Profiler.EndSample();
  168. }
  169. }
  170. private static void DebugPrintKnownNodes()
  171. {
  172. foreach (var entry in KnownTypeLookupTable)
  173. {
  174. var nodeType = entry.Key;
  175. var filterableAttributes = entry.Value;
  176. String attrs = "";
  177. foreach (var filterable in filterableAttributes)
  178. {
  179. attrs += filterable.ToString() + ", ";
  180. }
  181. Debug.Log(nodeType.ToString() + $": [{attrs}]");
  182. }
  183. }
  184. }
  185. }