AmplifyShaderFunctionEditor.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [CustomEditor( typeof( AmplifyShaderFunction ) )]
  10. public class AmplifyShaderFunctionEditor : Editor
  11. {
  12. class FunctionDependency
  13. {
  14. public string AssetName;
  15. public string AssetPath;
  16. public FunctionDependency(string name, string path)
  17. {
  18. AssetName = name;
  19. AssetPath = path;
  20. }
  21. }
  22. AmplifyShaderFunction m_target;
  23. List<FunctionDependency> m_dependencies = new List<FunctionDependency>();
  24. void OnEnable()
  25. {
  26. m_target = ( target as AmplifyShaderFunction );
  27. }
  28. public override void OnInspectorGUI()
  29. {
  30. //base.OnInspectorGUI();
  31. //base.serializedObject.Update();
  32. if( GUILayout.Button( "Open in Shader Editor" ) )
  33. {
  34. ASEPackageManagerHelper.SetupLateShaderFunction( m_target );
  35. }
  36. //EditorGUILayout.Separator();
  37. //m_target.FunctionInfo = EditorGUILayout.TextArea( m_target.FunctionInfo );
  38. if( m_target.Description.Length > 0 )
  39. {
  40. EditorGUILayout.HelpBox( m_target.Description, MessageType.Info );
  41. }
  42. EditorGUILayout.Space();
  43. if( GUILayout.Button( "Search Direct Dependencies" ) )
  44. {
  45. m_dependencies.Clear();
  46. string guid = AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_target ) );
  47. string[] allSFs = AssetDatabase.FindAssets( "t:AmplifyShaderFunction", null );
  48. foreach( string guid1 in allSFs )
  49. {
  50. string sfPath = AssetDatabase.GUIDToAssetPath( guid1 );
  51. bool found = SearchForGUID( guid, sfPath );
  52. if( found )
  53. {
  54. //string n = Regex.Replace( sfPath, @"(\.\w+|[\w\d\/]+\/)", "" );
  55. string n = Regex.Replace( sfPath, @"[\w\d\/]+\/", "" );
  56. m_dependencies.Add(new FunctionDependency( n, sfPath ) );
  57. }
  58. }
  59. string[] allSHs = AssetDatabase.FindAssets( "t:Shader", null );
  60. foreach( string guid1 in allSHs )
  61. {
  62. string shPath = AssetDatabase.GUIDToAssetPath( guid1 );
  63. bool found = SearchForGUID( guid, shPath );
  64. if( found )
  65. {
  66. string n = Regex.Replace( shPath, @"[\w\d\/]+\/", "" );
  67. m_dependencies.Add( new FunctionDependency( n, shPath ) );
  68. }
  69. }
  70. }
  71. EditorGUILayout.Space();
  72. for( int i = 0; i < m_dependencies.Count; i++ )
  73. {
  74. EditorGUILayout.BeginHorizontal();
  75. if( GUILayout.Button( m_dependencies[ i ].AssetName, "minibuttonleft" ) )
  76. {
  77. SelectAtPath( m_dependencies[ i ].AssetPath );
  78. }
  79. if( GUILayout.Button( "edit", "minibuttonright", GUILayout.Width(100) ) )
  80. {
  81. if( m_dependencies[ i ].AssetName.EndsWith( ".asset" ) )
  82. {
  83. var obj = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( m_dependencies[ i ].AssetPath );
  84. AmplifyShaderEditorWindow.LoadShaderFunctionToASE( obj, false );
  85. }
  86. else
  87. {
  88. var obj = AssetDatabase.LoadAssetAtPath<Shader>( m_dependencies[ i ].AssetPath );
  89. AmplifyShaderEditorWindow.ConvertShaderToASE( obj );
  90. }
  91. }
  92. EditorGUILayout.EndHorizontal();
  93. }
  94. if( m_dependencies.Count > 0 )
  95. {
  96. List<string> assetPaths = new List<string>();
  97. for( int i = 0; i < m_dependencies.Count; i++ )
  98. {
  99. assetPaths.Add( m_dependencies[ i ].AssetPath );
  100. }
  101. if( GUILayout.Button( "Open and Save All" ) )
  102. {
  103. bool doit = EditorUtility.DisplayDialog( "Open and Save All", "This will try to open all shader function and shaders that use this shader function and save them in quick succession, this may irreversibly break your files if something goes wrong. Are you sure you want to try?", "Yes, I'll take the risk", "No, I'll do it myself" );
  104. if( doit )
  105. AmplifyShaderEditorWindow.LoadAndSaveList( assetPaths.ToArray() );
  106. }
  107. }
  108. }
  109. public void SelectAtPath( string path )
  110. {
  111. var obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>( path );
  112. EditorGUIUtility.PingObject( obj );
  113. }
  114. public static bool SearchForGUID( string guid, string pathName )
  115. {
  116. bool result = false;
  117. int count = 0;
  118. if( !string.IsNullOrEmpty( pathName ) && File.Exists( pathName ) )
  119. {
  120. StreamReader fileReader = null;
  121. try
  122. {
  123. fileReader = new StreamReader( pathName );
  124. string line;
  125. int index = -1;
  126. while( ( line = fileReader.ReadLine() ) != null )
  127. {
  128. index = line.IndexOf( guid );
  129. count++;
  130. if( index > -1 )
  131. {
  132. result = true;
  133. break;
  134. }
  135. }
  136. }
  137. catch( Exception e )
  138. {
  139. Debug.LogException( e );
  140. }
  141. finally
  142. {
  143. if( fileReader != null )
  144. fileReader.Close();
  145. }
  146. }
  147. return result;
  148. }
  149. }
  150. }