Advertisement
Guest User

ShaderMaster EditorWindow

a guest
Dec 21st, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.11 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6.  
  7. public class ShaderMasterWindow : EditorWindow
  8. {
  9.     [MenuItem("Tools/Shader Master")]
  10.     public static void Open()
  11.     {
  12.         GetWindow<ShaderMasterWindow>();
  13.     }
  14.  
  15.     void OnGUI()
  16.     {
  17.         DrawForceCompilation();
  18.         DrawCgincOccurence();
  19.         DrawShaderOccurence();
  20.     }
  21.  
  22.     #region Force Compilation
  23.  
  24.     Object compilationFolder;
  25.  
  26.     void DrawForceCompilation()
  27.     {
  28.         compilationFolder = FolderField(compilationFolder, "Folder");
  29.         if (GUILayout.Button("Recompil all shaders in this folder"))
  30.             ForceShaderCompilation();
  31.     }
  32.  
  33.     void ForceShaderCompilation()
  34.     {
  35.         string folderPath = AssetDatabase.GetAssetPath(compilationFolder);
  36.         if (string.IsNullOrEmpty(folderPath)) folderPath = "Assets";
  37.  
  38.         string dialogMessage = "Are you sure? This functionality writes all .shader files in the [ " +folderPath+ " ] folder and can take several minutes.";
  39.  
  40.         if (EditorUtility.DisplayDialog("Force Shaders Compilation", dialogMessage, "Of Course !", "Hum... nope."))
  41.         {
  42.             if (!AssetDatabase.IsValidFolder(folderPath))
  43.             {
  44.                 Debug.LogError(folderPath + " is not a valid folder.");
  45.                 return;
  46.             }
  47.  
  48.             string[] guids = AssetDatabase.FindAssets("t:Shader", new string[1]{folderPath});
  49.             for (int i=0; i<guids.Length; i++)
  50.             {
  51.                 string shaderPath = AssetDatabase.GUIDToAssetPath(guids[i]);
  52.                 Shader shader = AssetDatabase.LoadAssetAtPath<Shader>(shaderPath);
  53.  
  54.                 string marker = "//LastForcedCompilation";
  55.                 string date = System.DateTime.Now.Day.ToString() +"/"+ System.DateTime.Now.Month.ToString() +"/"+ System.DateTime.Now.Year.ToString();
  56.                 string time = System.DateTime.Now.Hour.ToString() +":"+ System.DateTime.Now.Minute.ToString() +":"+ System.DateTime.Now.Second.ToString();
  57.                 string append = marker +"_"+ date +"_"+ time;
  58.  
  59.                 System.IO.FileStream str = System.IO.File.OpenRead(Application.dataPath.Replace("Assets","") + shaderPath);
  60.                 Debug.Log(str.Name, shader);
  61.                 str.Close();
  62.  
  63.                 string[] lines = System.IO.File.ReadAllLines(Application.dataPath.Replace("Assets","") + shaderPath);
  64.  
  65.                 if (lines[0].Contains(marker))
  66.                 {
  67.                     lines[0] = append;
  68.                 }
  69.                 else
  70.                 {
  71.                     List<string> l = lines.ToList();
  72.                     l.Insert(0, append);
  73.                     lines = l.ToArray();
  74.                 }
  75.  
  76.                 System.IO.File.WriteAllLines(Application.dataPath.Replace("Assets","") + shaderPath, lines);
  77.                 AssetDatabase.ImportAsset(shaderPath, ImportAssetOptions.ForceUpdate);
  78.             }
  79.         }
  80.     }
  81.  
  82.     #endregion
  83.  
  84.     #region CGINC Occurence
  85.  
  86.     TextAsset cginc;
  87.     List<string> cgincDeps = new List<string>();
  88.     Vector2 scrollShaders;
  89.  
  90.     void DrawCgincOccurence()
  91.     {
  92.         DrawHeader("Find all shaders/cginc using this cginc");
  93.         DrawHorizontalLine();
  94.  
  95.         TextAsset cgincPrev = cginc;
  96.         cginc = EditorGUILayout.ObjectField("CGinc", cginc, typeof(TextAsset), false) as TextAsset;
  97.         if (cginc != cgincPrev)
  98.         {
  99.             List<string> all = new List<string>();
  100.  
  101.             string[] allTextAsset = AssetDatabase.FindAssets("t:TextAsset");
  102.             for (int i = 0; i < allTextAsset.Length; i++)
  103.             {
  104.                 string path = AssetDatabase.GUIDToAssetPath(allTextAsset[i]);
  105.                 if (path.Contains(".cginc")) all.Add(allTextAsset[i]);
  106.             }
  107.             string[] allShaders = AssetDatabase.FindAssets("t:Shader");
  108.             for (int i = 0; i < allShaders.Length; i++) all.Add(allShaders[i]);
  109.  
  110.             cgincDeps.Clear();
  111.             for (int i = 0; i < all.Count; i++)
  112.             {
  113.                 all[i] = AssetDatabase.GUIDToAssetPath(all[i]);
  114.                 if (!string.IsNullOrEmpty(all[i]))
  115.                 {
  116.                     string text = System.IO.File.ReadAllText(Application.dataPath.Replace("Assets","") + all[i]);
  117.                     text = text.ToLower();
  118.                     if (text.Contains(cginc.name.ToLower() + ".cginc")) cgincDeps.Add(all[i]);
  119.                 }
  120.             }
  121.         }
  122.  
  123.         scrollShaders = GUILayout.BeginScrollView(scrollShaders);
  124.         {
  125.             for (int i = 0; i < cgincDeps.Count; i++)
  126.             {
  127.                 GUILayout.BeginHorizontal();
  128.                 {
  129.                     Object asset = AssetDatabase.LoadAssetAtPath<Object>(cgincDeps[i]);
  130.                     Texture2D assetPreview = AssetPreview.GetMiniThumbnail(asset);
  131.                     string assetName = Path.GetFileName(cgincDeps[i]);
  132.                     GUIContent content = new GUIContent(assetPreview);
  133.  
  134.                     EditorGUILayout.BeginHorizontal();
  135.                     GUILayout.Label(content, GUILayout.Height(20), GUILayout.Width(20));
  136.                     GUILayout.Label(assetName, GUILayout.Height(20));
  137.  
  138.                     GUILayout.FlexibleSpace();
  139.                     if (GUILayout.Button("Show"))
  140.                     {
  141.                         EditorGUIUtility.PingObject(asset);
  142.                     }
  143.                     EditorGUILayout.EndHorizontal();
  144.                        
  145.                 }
  146.                 GUILayout.EndHorizontal();
  147.             }
  148.         }
  149.         GUILayout.EndScrollView();
  150.     }
  151.  
  152.     #endregion
  153.  
  154.     #region SHADER Occurence
  155.  
  156.     Shader shader;
  157.     List<string> materials = new List<string>();
  158.     Vector2 scrollMaterials;
  159.  
  160.     void DrawShaderOccurence()
  161.     {
  162.         DrawHeader("Find all materials using this shader");
  163.         DrawHorizontalLine();
  164.  
  165.         Shader prev = shader;
  166.         shader = EditorGUILayout.ObjectField("Shader", shader, typeof(Shader), false) as Shader;
  167.         if (shader != prev)
  168.         {
  169.             string shaderPath = AssetDatabase.GetAssetPath(shader);
  170.             string[] allMaterials = AssetDatabase.FindAssets("t:Material");
  171.             materials.Clear();
  172.             for (int i = 0; i < allMaterials.Length; i++)
  173.             {
  174.                 allMaterials[i] = AssetDatabase.GUIDToAssetPath(allMaterials[i]);
  175.                 string[] dep = AssetDatabase.GetDependencies(allMaterials[i]);
  176.                 if (ArrayUtility.Contains(dep, shaderPath))
  177.                     materials.Add(allMaterials[i]);
  178.             }
  179.         }
  180.  
  181.         scrollMaterials = GUILayout.BeginScrollView(scrollMaterials);
  182.         {
  183.             for (int i = 0; i < materials.Count; i++)
  184.             {
  185.                 GUILayout.BeginHorizontal();
  186.                 {
  187.                     Object asset = AssetDatabase.LoadAssetAtPath<Object>(materials[i]);
  188.                     Texture2D assetPreview = AssetPreview.GetMiniThumbnail(asset);
  189.                     string assetName = Path.GetFileName(materials[i]);
  190.                     GUIContent content = new GUIContent(assetPreview);
  191.  
  192.                     EditorGUILayout.BeginHorizontal();
  193.                     GUILayout.Label(content, GUILayout.Height(20), GUILayout.Width(20));
  194.                     GUILayout.Label(assetName, GUILayout.Height(20));
  195.  
  196.                     GUILayout.FlexibleSpace();
  197.                     if (GUILayout.Button("Show"))
  198.                     {
  199.                         EditorGUIUtility.PingObject(asset);
  200.                     }
  201.                     EditorGUILayout.EndHorizontal();
  202.  
  203.                 }
  204.                 GUILayout.EndHorizontal();
  205.             }
  206.         }
  207.         GUILayout.EndScrollView();
  208.     }
  209.  
  210.     #endregion
  211.  
  212.     #region GUI Helpers
  213.  
  214.     Object FolderField(Object folder, string name)
  215.     {
  216.         GUILayout.BeginHorizontal();
  217.         GUILayout.Label(name);
  218.  
  219.         folder = EditorGUILayout.ObjectField(folder, typeof(Object), false) as Object;
  220.         if (folder != null)
  221.         {
  222.             if (!IsValidFolder(folder))
  223.                 folder = null;
  224.         }
  225.         GUILayout.EndHorizontal();
  226.  
  227.         return folder;
  228.     }
  229.  
  230.     bool IsValidFolder(Object folder)
  231.     {
  232.         if (folder == null) // no folder
  233.             return false;
  234.  
  235.         string folderPath = AssetDatabase.GetAssetPath(folder);
  236.         if (!AssetDatabase.IsValidFolder(folderPath)) // not a folder
  237.             return false;
  238.  
  239.         return true;
  240.     }
  241.  
  242.     void DrawHeader(string title)
  243.     {
  244.         EditorGUILayout.BeginVertical("Box");
  245.         EditorGUILayout.BeginVertical("Box");
  246.         EditorGUILayout.BeginHorizontal();
  247.         GUILayout.FlexibleSpace();
  248.         GUILayout.Label (title, EditorStyles.largeLabel);
  249.         GUILayout.FlexibleSpace();
  250.         EditorGUILayout.EndHorizontal();
  251.         EditorGUILayout.EndVertical();
  252.         EditorGUILayout.EndVertical();
  253.     }
  254.  
  255.     void DrawHorizontalLine()
  256.     {
  257.         GUIStyle s = new GUIStyle(GUI.skin.horizontalSlider);
  258.         s.fixedHeight = 10;
  259.         EditorGUILayout.LabelField("", s, GUILayout.Height(8));
  260.     }
  261.  
  262.     #endregion
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement