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.

699 lines
21 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using System.Text.RegularExpressions;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. public class TemplateInputData
  12. {
  13. public string PortName;
  14. public WirePortDataType DataType;
  15. public MasterNodePortCategory PortCategory;
  16. public int PortUniqueId;
  17. public int OrderId;
  18. public int TagGlobalStartIdx;
  19. public int TagLocalStartIdx;
  20. public string TagId;
  21. public string DefaultValue;
  22. public string LinkId;
  23. public TemplateInputData( int tagLocalStartIdx, int tagGlobalStartIdx, string tagId, string portName, string defaultValue, WirePortDataType dataType, MasterNodePortCategory portCategory, int portUniqueId, int orderId, string linkId )
  24. {
  25. DefaultValue = defaultValue;
  26. PortName = portName;
  27. DataType = dataType;
  28. PortCategory = portCategory;
  29. PortUniqueId = portUniqueId;
  30. OrderId = orderId;
  31. TagId = tagId;
  32. TagGlobalStartIdx = tagGlobalStartIdx;
  33. TagLocalStartIdx = tagLocalStartIdx;
  34. LinkId = linkId;
  35. }
  36. public TemplateInputData( TemplateInputData other )
  37. {
  38. DefaultValue = other.DefaultValue;
  39. PortName = other.PortName;
  40. DataType = other.DataType;
  41. PortCategory = other.PortCategory;
  42. PortUniqueId = other.PortUniqueId;
  43. OrderId = other.OrderId;
  44. TagId = other.TagId;
  45. TagGlobalStartIdx = other.TagGlobalStartIdx;
  46. LinkId = other.LinkId;
  47. }
  48. }
  49. [Serializable]
  50. public class TemplatePropertyContainer
  51. {
  52. [SerializeField]
  53. private List<TemplateProperty> m_propertyList = new List<TemplateProperty>();
  54. private Dictionary<string, TemplateProperty> m_propertyDict = new Dictionary<string, TemplateProperty>();
  55. public void AddId( TemplateProperty templateProperty )
  56. {
  57. BuildInfo();
  58. m_propertyList.Add( templateProperty );
  59. m_propertyDict.Add( templateProperty.Id, templateProperty );
  60. }
  61. public void AddId( string body, string ID, bool searchIndentation = true )
  62. {
  63. AddId( body, ID, searchIndentation, string.Empty );
  64. }
  65. public void AddId( string body, string ID, bool searchIndentation, string customIndentation )
  66. {
  67. BuildInfo();
  68. int propertyIndex = body.IndexOf( ID );
  69. if( propertyIndex > -1 )
  70. {
  71. if( searchIndentation )
  72. {
  73. int identationIndex = -1;
  74. for( int i = propertyIndex; i >= 0; i-- )
  75. {
  76. if( body[ i ] == TemplatesManager.TemplateNewLine )
  77. {
  78. identationIndex = i + 1;
  79. break;
  80. }
  81. if( i == 0 )
  82. {
  83. identationIndex = 0;
  84. }
  85. }
  86. if( identationIndex > -1 )
  87. {
  88. int length = propertyIndex - identationIndex;
  89. string indentation = ( length > 0 ) ? body.Substring( identationIndex, length ) : string.Empty;
  90. TemplateProperty templateProperty = new TemplateProperty( ID, indentation, false );
  91. m_propertyList.Add( templateProperty );
  92. m_propertyDict.Add( templateProperty.Id, templateProperty );
  93. }
  94. else
  95. {
  96. TemplateProperty templateProperty = new TemplateProperty( ID, string.Empty, false );
  97. m_propertyList.Add( templateProperty );
  98. m_propertyDict.Add( templateProperty.Id, templateProperty );
  99. }
  100. }
  101. else
  102. {
  103. TemplateProperty templateProperty = new TemplateProperty( ID, customIndentation, true );
  104. m_propertyList.Add( templateProperty );
  105. m_propertyDict.Add( templateProperty.Id, templateProperty );
  106. }
  107. }
  108. }
  109. public void AddId( string body, string ID, int propertyIndex, bool searchIndentation )
  110. {
  111. AddId( body, ID, propertyIndex, searchIndentation, string.Empty );
  112. }
  113. public void AddId( string body, string ID, int propertyIndex, bool searchIndentation, string customIndentation )
  114. {
  115. if( body == null || string.IsNullOrEmpty( body ) )
  116. return;
  117. BuildInfo();
  118. if( searchIndentation && propertyIndex > -1 && propertyIndex < body.Length )
  119. {
  120. int indentationIndex = -1;
  121. for( int i = propertyIndex; i > 0; i-- )
  122. {
  123. if( body[ i ] == TemplatesManager.TemplateNewLine )
  124. {
  125. indentationIndex = i + 1;
  126. break;
  127. }
  128. }
  129. if( indentationIndex > -1 )
  130. {
  131. int length = propertyIndex - indentationIndex;
  132. string indentation = ( length > 0 ) ? body.Substring( indentationIndex, length ) : string.Empty;
  133. TemplateProperty templateProperty = new TemplateProperty( ID, indentation, false );
  134. m_propertyList.Add( templateProperty );
  135. m_propertyDict.Add( templateProperty.Id, templateProperty );
  136. }
  137. }
  138. else
  139. {
  140. TemplateProperty templateProperty = new TemplateProperty( ID, customIndentation, true );
  141. m_propertyList.Add( templateProperty );
  142. m_propertyDict.Add( templateProperty.Id, templateProperty );
  143. }
  144. }
  145. public void BuildInfo()
  146. {
  147. if( m_propertyDict == null )
  148. {
  149. m_propertyDict = new Dictionary<string, TemplateProperty>();
  150. }
  151. if( m_propertyList.Count != m_propertyDict.Count )
  152. {
  153. m_propertyDict.Clear();
  154. for( int i = 0; i < m_propertyList.Count; i++ )
  155. {
  156. m_propertyDict.Add( m_propertyList[ i ].Id, m_propertyList[ i ] );
  157. }
  158. }
  159. }
  160. public void ResetTemplateUsageData()
  161. {
  162. BuildInfo();
  163. for( int i = 0; i < m_propertyList.Count; i++ )
  164. {
  165. m_propertyList[ i ].Used = false;
  166. }
  167. }
  168. public void Reset()
  169. {
  170. m_propertyList.Clear();
  171. m_propertyDict.Clear();
  172. }
  173. public void Destroy()
  174. {
  175. m_propertyList.Clear();
  176. m_propertyList = null;
  177. m_propertyDict.Clear();
  178. m_propertyDict = null;
  179. }
  180. public Dictionary<string, TemplateProperty> PropertyDict
  181. {
  182. get
  183. {
  184. BuildInfo();
  185. return m_propertyDict;
  186. }
  187. }
  188. public List<TemplateProperty> PropertyList { get { return m_propertyList; } }
  189. }
  190. [Serializable]
  191. public class TemplateProperty
  192. {
  193. public bool UseIndentationAtStart = false;
  194. public string Indentation;
  195. public bool UseCustomIndentation;
  196. public string Id;
  197. public bool AutoLineFeed;
  198. public bool Used;
  199. public TemplateProperty( string id, string indentation, bool useCustomIndentation )
  200. {
  201. Id = id;
  202. Indentation = indentation;
  203. UseCustomIndentation = useCustomIndentation;
  204. AutoLineFeed = !string.IsNullOrEmpty( indentation );
  205. Used = false;
  206. }
  207. }
  208. [Serializable]
  209. public class TemplateFunctionData
  210. {
  211. public int MainBodyLocalIdx;
  212. public string MainBodyName;
  213. public string Id;
  214. public int Position;
  215. public string InVarType;
  216. public string InVarName;
  217. public string OutVarType;
  218. public string OutVarName;
  219. public MasterNodePortCategory Category;
  220. public TemplateFunctionData( int mainBodyLocalIdx, string mainBodyName, string id, int position, string inVarInfo, string outVarInfo, MasterNodePortCategory category )
  221. {
  222. MainBodyLocalIdx = mainBodyLocalIdx;
  223. MainBodyName = mainBodyName;
  224. Id = id;
  225. Position = position;
  226. {
  227. string[] inVarInfoArr = inVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  228. if( inVarInfoArr.Length > 1 )
  229. {
  230. InVarType = inVarInfoArr[ 1 ];
  231. InVarName = inVarInfoArr[ 0 ];
  232. }
  233. }
  234. {
  235. string[] outVarInfoArr = outVarInfo.Split( IOUtils.VALUE_SEPARATOR );
  236. if( outVarInfoArr.Length > 1 )
  237. {
  238. OutVarType = outVarInfoArr[ 1 ];
  239. OutVarName = outVarInfoArr[ 0 ];
  240. }
  241. }
  242. Category = category;
  243. }
  244. }
  245. [Serializable]
  246. public class TemplateTagData
  247. {
  248. public int StartIdx = -1;
  249. public string Id;
  250. public bool SearchIndentation;
  251. public string CustomIndentation;
  252. public TemplateTagData( int startIdx, string id, bool searchIndentation )
  253. {
  254. StartIdx = startIdx;
  255. Id = id;
  256. SearchIndentation = searchIndentation;
  257. CustomIndentation = string.Empty;
  258. }
  259. public TemplateTagData( string id, bool searchIndentation )
  260. {
  261. Id = id;
  262. SearchIndentation = searchIndentation;
  263. CustomIndentation = string.Empty;
  264. }
  265. public TemplateTagData( string id, bool searchIndentation, string customIndentation )
  266. {
  267. Id = id;
  268. SearchIndentation = searchIndentation;
  269. CustomIndentation = customIndentation;
  270. }
  271. public bool IsValid { get { return StartIdx >= 0; } }
  272. }
  273. public enum TemplatePortIds
  274. {
  275. Name = 0,
  276. DataType,
  277. UniqueId,
  278. OrderId,
  279. Link
  280. }
  281. public enum TemplateCommonTagId
  282. {
  283. Property = 0,
  284. Global = 1,
  285. Function = 2,
  286. Tag = 3,
  287. Pragmas = 4,
  288. Pass = 5,
  289. Params_Vert = 6,
  290. Params_Frag = 7
  291. //CullMode = 8,
  292. //BlendMode = 9,
  293. //BlendOp = 10,
  294. //ColorMask = 11,
  295. //StencilOp = 12
  296. }
  297. [Serializable]
  298. public class TemplatesManager : ScriptableObject
  299. {
  300. public static int MPShaderVersion = 14503;
  301. public static readonly string TemplateShaderNameBeginTag = "/*ase_name*/";
  302. public static readonly string TemplateStencilTag = "/*ase_stencil*/\n";
  303. public static readonly string TemplateAllModulesTag = "/*ase_all_modules*/\n";
  304. public static readonly string TemplateMPSubShaderTag = "\\bSubShader\\b\\s*{";
  305. //public static readonly string TemplateMPPassTag = "^\\s*Pass\b\\s*{";//"\\bPass\\b\\s*{";
  306. public static readonly string TemplateMPPassTag = "\\bPass\\b\\s*{";
  307. public static readonly string TemplateLocalVarTag = "/*ase_local_var*/";
  308. public static readonly string TemplateDependenciesListTag = "/*ase_dependencies_list*/";
  309. public static readonly string TemplatePragmaTag = "/*ase_pragma*/";
  310. public static readonly string TemplatePassTag = "/*ase_pass*/";
  311. public static readonly string TemplateEndPassTag = "/*ase_end_pass*/";
  312. public static readonly string TemplatePassTagPattern = @"\s\/\*ase_pass\*\/";
  313. public static readonly string TemplatePropertyTag = "/*ase_props*/\n";
  314. public static readonly string TemplateGlobalsTag = "/*ase_globals*/\n";
  315. public static readonly string TemplateInterpolatorBeginTag = "/*ase_interp(";
  316. public static readonly string TemplateVertexDataTag = "/*ase_vdata:";
  317. //public static readonly string TemplateExcludeFromGraphTag = "/*ase_hide_pass*/";
  318. public static readonly string TemplateMainPassTag = "/*ase_main_pass*/";
  319. public static readonly string TemplateFunctionsTag = "/*ase_funcs*/\n";
  320. //public static readonly string TemplateTagsTag = "/*ase_tags*/";
  321. //public static readonly string TemplateCullModeTag = "/*ase_cull_mode*/";
  322. //public static readonly string TemplateBlendModeTag = "/*ase_blend_mode*/";
  323. //public static readonly string TemplateBlendOpTag = "/*ase_blend_op*/";
  324. //public static readonly string TemplateColorMaskTag = "/*ase_color_mask*/";
  325. //public static readonly string TemplateStencilOpTag = "/*ase_stencil*/";
  326. public static readonly string TemplateCodeSnippetAttribBegin = "#CODE_SNIPPET_ATTRIBS_BEGIN#";
  327. public static readonly string TemplateCodeSnippetAttribEnd = "#CODE_SNIPPET_ATTRIBS_END#\n";
  328. public static readonly string TemplateCodeSnippetEnd = "#CODE_SNIPPET_END#\n";
  329. public static readonly char TemplateNewLine = '\n';
  330. // INPUTS AREA
  331. public static readonly string TemplateInputsVertBeginTag = "/*ase_vert_out:";
  332. public static readonly string TemplateInputsFragBeginTag = "/*ase_frag_out:";
  333. public static readonly string TemplateInputsVertParamsTag = "/*ase_vert_input*/";
  334. public static readonly string TemplateInputsFragParamsTag = "/*ase_frag_input*/";
  335. // CODE AREA
  336. public static readonly string TemplateVertexCodeBeginArea = "/*ase_vert_code:";
  337. public static readonly string TemplateFragmentCodeBeginArea = "/*ase_frag_code:";
  338. public static readonly string TemplateEndOfLine = "*/\n";
  339. public static readonly string TemplateEndSectionTag = "*/";
  340. public static readonly string TemplateFullEndTag = "/*end*/";
  341. public static readonly string NameFormatter = "\"{0}\"";
  342. public static readonly TemplateTagData[] CommonTags = { new TemplateTagData( TemplatePropertyTag,true),
  343. new TemplateTagData( TemplateGlobalsTag,true),
  344. new TemplateTagData( TemplateFunctionsTag,true),
  345. //new TemplateTagData( TemplateTagsTag,false," "),
  346. new TemplateTagData( TemplatePragmaTag,true),
  347. new TemplateTagData( TemplatePassTag,true),
  348. new TemplateTagData( TemplateInputsVertParamsTag,false),
  349. new TemplateTagData( TemplateInputsFragParamsTag,false)
  350. //new TemplateTagData( TemplateCullModeTag,false),
  351. //new TemplateTagData( TemplateBlendModeTag,false),
  352. //new TemplateTagData( TemplateBlendOpTag,false),
  353. //new TemplateTagData( TemplateColorMaskTag,false),
  354. //new TemplateTagData( TemplateStencilOpTag,true),
  355. };
  356. public static Dictionary<string, string> OfficialTemplates = new Dictionary<string, string>()
  357. {
  358. { "0770190933193b94aaa3065e307002fa","Unlit"},
  359. { "6ce779933eb99f049b78d6163735e06f","Custom RT Init"},
  360. { "32120270d1b3a8746af2aca8bc749736","Custom RT Update"},
  361. { "1976390536c6c564abb90fe41f6ee334","Lightweight PBR"},
  362. { "e2514bdcf5e5399499a9eb24d175b9db","Lightweight Unlit"},
  363. { "bb308bce79762c34e823049efce65141","HD PBR"},
  364. { "dfe2f27ac20b08c469b2f95c236be0c3","HD Unlit"},
  365. { "c71b220b631b6344493ea3cf87110c93","Legacy/Post Process" },
  366. { "6e114a916ca3e4b4bb51972669d463bf","Legacy/Default Unlit" },
  367. { "5056123faa0c79b47ab6ad7e8bf059a4","Legacy/Default UI" },
  368. { "899e609c083c74c4ca567477c39edef0","Legacy/Unlit Lightmap" },
  369. { "0f8ba0101102bb14ebf021ddadce9b49","Legacy/Default Sprites" },
  370. { "0b6a9f8b4f707c74ca64c0be8e590de0","Legacy/Particles Alpha Blended" },
  371. { "e1de45c0d41f68c41b2cc20c8b9c05ef","Legacy/Multi Pass Unlit" },
  372. { "f53051a8190f7044fa936bd7fbe116c1","Impostors/Bake" },
  373. { "30a8e337ed84177439ca24b6a5c97cd1","Impostors/Runtime Specular" },
  374. { "964e57f8f828f8e44b6bc62ddfbe7b51","Impostors/Runtime Metallic" }
  375. };
  376. public static readonly string TemplateMenuItemsFileGUID = "da0b931bd234a1e43b65f684d4b59bfb";
  377. private Dictionary<string, TemplateDataParent> m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  378. [SerializeField]
  379. private List<TemplateDataParent> m_sortedTemplates = new List<TemplateDataParent>();
  380. [SerializeField]
  381. public string[] AvailableTemplateNames;
  382. [SerializeField]
  383. public bool Initialized = false;
  384. public static string CurrTemplateGUIDLoaded = string.Empty;
  385. public static bool IsTestTemplate { get { return CurrTemplateGUIDLoaded.Equals( "a95a019bbc760714bb8228af04c291d1" ); } }
  386. public static bool ShowDebugMessages = false;
  387. public void RefreshAvailableTemplates()
  388. {
  389. if( m_availableTemplates.Count != m_sortedTemplates.Count )
  390. {
  391. m_availableTemplates.Clear();
  392. int count = m_sortedTemplates.Count;
  393. for( int i = 0; i < count; i++ )
  394. {
  395. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  396. }
  397. }
  398. }
  399. public void Init()
  400. {
  401. if( !Initialized )
  402. {
  403. if( ShowDebugMessages )
  404. Debug.Log( "Initialize" );
  405. string templateMenuItems = IOUtils.LoadTextFileFromDisk( AssetDatabase.GUIDToAssetPath( TemplateMenuItemsFileGUID ) );
  406. bool refreshTemplateMenuItems = false;
  407. foreach( KeyValuePair<string, string> kvp in OfficialTemplates )
  408. {
  409. if( !string.IsNullOrEmpty( AssetDatabase.GUIDToAssetPath( kvp.Key ) ) )
  410. {
  411. TemplateMultiPass template = ScriptableObject.CreateInstance<TemplateMultiPass>();
  412. template.Init( kvp.Value, kvp.Key, false );
  413. AddTemplate( template );
  414. if( !refreshTemplateMenuItems && templateMenuItems.IndexOf( kvp.Value ) < 0 )
  415. refreshTemplateMenuItems = true;
  416. }
  417. }
  418. // Search for other possible templates on the project
  419. string[] allShaders = AssetDatabase.FindAssets( "t:shader" );
  420. for( int i = 0; i < allShaders.Length; i++ )
  421. {
  422. if( !m_availableTemplates.ContainsKey( allShaders[ i ] ) )
  423. {
  424. CheckAndLoadTemplate( allShaders[ i ] );
  425. }
  426. }
  427. // TODO: Sort list alphabeticaly
  428. AvailableTemplateNames = new string[ m_sortedTemplates.Count + 1 ];
  429. AvailableTemplateNames[ 0 ] = "Custom";
  430. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  431. {
  432. m_sortedTemplates[ i ].OrderId = i;
  433. AvailableTemplateNames[ i + 1 ] = m_sortedTemplates[ i ].Name;
  434. }
  435. if( refreshTemplateMenuItems )
  436. CreateTemplateMenuItems();
  437. Initialized = true;
  438. }
  439. }
  440. public void CreateTemplateMenuItems()
  441. {
  442. if( m_sortedTemplates == null || m_sortedTemplates.Count == 0 )
  443. return;
  444. System.Text.StringBuilder fileContents = new System.Text.StringBuilder();
  445. fileContents.Append( "// Amplify Shader Editor - Visual Shader Editing Tool\n" );
  446. fileContents.Append( "// Copyright (c) Amplify Creations, Lda <info@amplify.pt>\n" );
  447. fileContents.Append( "using UnityEditor;\n" );
  448. fileContents.Append( "\n" );
  449. fileContents.Append( "namespace AmplifyShaderEditor\n" );
  450. fileContents.Append( "{\n" );
  451. fileContents.Append( "\tpublic class TemplateMenuItems\n" );
  452. fileContents.Append( "\t{\n" );
  453. int fixedPriority = 85;
  454. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  455. {
  456. fileContents.AppendFormat( "\t\t[MenuItem( \"Assets/Create/Amplify Shader/{0}\", false, {1} )]\n", m_sortedTemplates[ i ].Name, fixedPriority );
  457. fileContents.AppendFormat( "\t\tpublic static void ApplyTemplate{0}()\n", i );
  458. fileContents.Append( "\t\t{\n" );
  459. fileContents.AppendFormat( "\t\t\tAmplifyShaderEditorWindow.CreateNewTemplateShader( \"{0}\" );\n", m_sortedTemplates[ i ].GUID );
  460. fileContents.Append( "\t\t}\n" );
  461. }
  462. fileContents.Append( "\t}\n" );
  463. fileContents.Append( "}\n" );
  464. string filePath = AssetDatabase.GUIDToAssetPath( TemplateMenuItemsFileGUID );
  465. IOUtils.SaveTextfileToDisk( fileContents.ToString(), filePath, false );
  466. AssetDatabase.ImportAsset( filePath );
  467. }
  468. public int GetIdForTemplate( TemplateData templateData )
  469. {
  470. if( templateData == null )
  471. return -1;
  472. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  473. {
  474. if( m_sortedTemplates[ i ].GUID.Equals( templateData.GUID ) )
  475. return m_sortedTemplates[ i ].OrderId;
  476. }
  477. return -1;
  478. }
  479. public void AddTemplate( TemplateDataParent templateData )
  480. {
  481. if( templateData == null || !templateData.IsValid )
  482. return;
  483. RefreshAvailableTemplates();
  484. if( !m_availableTemplates.ContainsKey( templateData.GUID ) )
  485. {
  486. m_sortedTemplates.Add( templateData );
  487. m_availableTemplates.Add( templateData.GUID, templateData );
  488. }
  489. }
  490. public void RemoveTemplate( string guid )
  491. {
  492. TemplateDataParent templateData = GetTemplate( guid );
  493. if( templateData != null )
  494. {
  495. RemoveTemplate( templateData );
  496. }
  497. }
  498. public void RemoveTemplate( TemplateDataParent templateData )
  499. {
  500. RefreshAvailableTemplates();
  501. if( m_availableTemplates != null )
  502. m_availableTemplates.Remove( templateData.GUID );
  503. m_sortedTemplates.Remove( templateData );
  504. templateData.Destroy();
  505. }
  506. public void Destroy()
  507. {
  508. if( TemplatesManager.ShowDebugMessages )
  509. Debug.Log( "Destroy Manager" );
  510. if( m_availableTemplates != null )
  511. {
  512. foreach( KeyValuePair<string, TemplateDataParent> kvp in m_availableTemplates )
  513. {
  514. kvp.Value.Destroy();
  515. }
  516. m_availableTemplates.Clear();
  517. m_availableTemplates = null;
  518. }
  519. int count = m_sortedTemplates.Count;
  520. for( int i = 0; i < count; i++ )
  521. {
  522. ScriptableObject.DestroyImmediate( m_sortedTemplates[ i ] );
  523. }
  524. m_sortedTemplates.Clear();
  525. m_sortedTemplates = null;
  526. AvailableTemplateNames = null;
  527. Initialized = false;
  528. }
  529. public TemplateDataParent GetTemplate( int id )
  530. {
  531. if( id < m_sortedTemplates.Count )
  532. return m_sortedTemplates[ id ];
  533. return null;
  534. }
  535. public TemplateDataParent GetTemplate( string guid )
  536. {
  537. RefreshAvailableTemplates();
  538. if( m_availableTemplates == null && m_sortedTemplates != null )
  539. {
  540. m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  541. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  542. {
  543. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  544. }
  545. }
  546. if( m_availableTemplates.ContainsKey( guid ) )
  547. return m_availableTemplates[ guid ];
  548. return null;
  549. }
  550. public TemplateDataParent GetTemplateByName( string name )
  551. {
  552. RefreshAvailableTemplates();
  553. if( m_availableTemplates == null && m_sortedTemplates != null )
  554. {
  555. m_availableTemplates = new Dictionary<string, TemplateDataParent>();
  556. for( int i = 0; i < m_sortedTemplates.Count; i++ )
  557. {
  558. m_availableTemplates.Add( m_sortedTemplates[ i ].GUID, m_sortedTemplates[ i ] );
  559. }
  560. }
  561. foreach( KeyValuePair<string, TemplateDataParent> kvp in m_availableTemplates )
  562. {
  563. if( kvp.Value.DefaultShaderName.Equals( name ) )
  564. {
  565. return kvp.Value;
  566. }
  567. }
  568. return null;
  569. }
  570. public TemplateDataParent CheckAndLoadTemplate( string guid )
  571. {
  572. TemplateDataParent templateData = GetTemplate( guid );
  573. if( templateData == null )
  574. {
  575. string datapath = AssetDatabase.GUIDToAssetPath( guid );
  576. string body = IOUtils.LoadTextFileFromDisk( datapath );
  577. if( body.IndexOf( TemplatesManager.TemplateShaderNameBeginTag ) > -1 )
  578. {
  579. templateData = ScriptableObject.CreateInstance<TemplateMultiPass>();
  580. templateData.Init( string.Empty, guid, true );
  581. if( templateData.IsValid )
  582. {
  583. AddTemplate( templateData );
  584. return templateData;
  585. }
  586. }
  587. }
  588. return null;
  589. }
  590. private void OnEnable()
  591. {
  592. if( !Initialized )
  593. {
  594. Init();
  595. }
  596. else
  597. {
  598. RefreshAvailableTemplates();
  599. }
  600. hideFlags = HideFlags.HideAndDontSave;
  601. if( ShowDebugMessages )
  602. Debug.Log( "On Enable Manager: " + this.GetInstanceID() );
  603. }
  604. public int TemplateCount { get { return m_sortedTemplates.Count; } }
  605. }
  606. }