Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- public class ShaderMasterWindow : EditorWindow
- {
- [MenuItem("Tools/Shader Master")]
- public static void Open()
- {
- GetWindow<ShaderMasterWindow>();
- }
- void OnGUI()
- {
- DrawForceCompilation();
- DrawCgincOccurence();
- DrawShaderOccurence();
- }
- #region Force Compilation
- Object compilationFolder;
- void DrawForceCompilation()
- {
- compilationFolder = FolderField(compilationFolder, "Folder");
- if (GUILayout.Button("Recompil all shaders in this folder"))
- ForceShaderCompilation();
- }
- void ForceShaderCompilation()
- {
- string folderPath = AssetDatabase.GetAssetPath(compilationFolder);
- if (string.IsNullOrEmpty(folderPath)) folderPath = "Assets";
- string dialogMessage = "Are you sure? This functionality writes all .shader files in the [ " +folderPath+ " ] folder and can take several minutes.";
- if (EditorUtility.DisplayDialog("Force Shaders Compilation", dialogMessage, "Of Course !", "Hum... nope."))
- {
- if (!AssetDatabase.IsValidFolder(folderPath))
- {
- Debug.LogError(folderPath + " is not a valid folder.");
- return;
- }
- string[] guids = AssetDatabase.FindAssets("t:Shader", new string[1]{folderPath});
- for (int i=0; i<guids.Length; i++)
- {
- string shaderPath = AssetDatabase.GUIDToAssetPath(guids[i]);
- Shader shader = AssetDatabase.LoadAssetAtPath<Shader>(shaderPath);
- string marker = "//LastForcedCompilation";
- string date = System.DateTime.Now.Day.ToString() +"/"+ System.DateTime.Now.Month.ToString() +"/"+ System.DateTime.Now.Year.ToString();
- string time = System.DateTime.Now.Hour.ToString() +":"+ System.DateTime.Now.Minute.ToString() +":"+ System.DateTime.Now.Second.ToString();
- string append = marker +"_"+ date +"_"+ time;
- System.IO.FileStream str = System.IO.File.OpenRead(Application.dataPath.Replace("Assets","") + shaderPath);
- Debug.Log(str.Name, shader);
- str.Close();
- string[] lines = System.IO.File.ReadAllLines(Application.dataPath.Replace("Assets","") + shaderPath);
- if (lines[0].Contains(marker))
- {
- lines[0] = append;
- }
- else
- {
- List<string> l = lines.ToList();
- l.Insert(0, append);
- lines = l.ToArray();
- }
- System.IO.File.WriteAllLines(Application.dataPath.Replace("Assets","") + shaderPath, lines);
- AssetDatabase.ImportAsset(shaderPath, ImportAssetOptions.ForceUpdate);
- }
- }
- }
- #endregion
- #region CGINC Occurence
- TextAsset cginc;
- List<string> cgincDeps = new List<string>();
- Vector2 scrollShaders;
- void DrawCgincOccurence()
- {
- DrawHeader("Find all shaders/cginc using this cginc");
- DrawHorizontalLine();
- TextAsset cgincPrev = cginc;
- cginc = EditorGUILayout.ObjectField("CGinc", cginc, typeof(TextAsset), false) as TextAsset;
- if (cginc != cgincPrev)
- {
- List<string> all = new List<string>();
- string[] allTextAsset = AssetDatabase.FindAssets("t:TextAsset");
- for (int i = 0; i < allTextAsset.Length; i++)
- {
- string path = AssetDatabase.GUIDToAssetPath(allTextAsset[i]);
- if (path.Contains(".cginc")) all.Add(allTextAsset[i]);
- }
- string[] allShaders = AssetDatabase.FindAssets("t:Shader");
- for (int i = 0; i < allShaders.Length; i++) all.Add(allShaders[i]);
- cgincDeps.Clear();
- for (int i = 0; i < all.Count; i++)
- {
- all[i] = AssetDatabase.GUIDToAssetPath(all[i]);
- if (!string.IsNullOrEmpty(all[i]))
- {
- string text = System.IO.File.ReadAllText(Application.dataPath.Replace("Assets","") + all[i]);
- text = text.ToLower();
- if (text.Contains(cginc.name.ToLower() + ".cginc")) cgincDeps.Add(all[i]);
- }
- }
- }
- scrollShaders = GUILayout.BeginScrollView(scrollShaders);
- {
- for (int i = 0; i < cgincDeps.Count; i++)
- {
- GUILayout.BeginHorizontal();
- {
- Object asset = AssetDatabase.LoadAssetAtPath<Object>(cgincDeps[i]);
- Texture2D assetPreview = AssetPreview.GetMiniThumbnail(asset);
- string assetName = Path.GetFileName(cgincDeps[i]);
- GUIContent content = new GUIContent(assetPreview);
- EditorGUILayout.BeginHorizontal();
- GUILayout.Label(content, GUILayout.Height(20), GUILayout.Width(20));
- GUILayout.Label(assetName, GUILayout.Height(20));
- GUILayout.FlexibleSpace();
- if (GUILayout.Button("Show"))
- {
- EditorGUIUtility.PingObject(asset);
- }
- EditorGUILayout.EndHorizontal();
- }
- GUILayout.EndHorizontal();
- }
- }
- GUILayout.EndScrollView();
- }
- #endregion
- #region SHADER Occurence
- Shader shader;
- List<string> materials = new List<string>();
- Vector2 scrollMaterials;
- void DrawShaderOccurence()
- {
- DrawHeader("Find all materials using this shader");
- DrawHorizontalLine();
- Shader prev = shader;
- shader = EditorGUILayout.ObjectField("Shader", shader, typeof(Shader), false) as Shader;
- if (shader != prev)
- {
- string shaderPath = AssetDatabase.GetAssetPath(shader);
- string[] allMaterials = AssetDatabase.FindAssets("t:Material");
- materials.Clear();
- for (int i = 0; i < allMaterials.Length; i++)
- {
- allMaterials[i] = AssetDatabase.GUIDToAssetPath(allMaterials[i]);
- string[] dep = AssetDatabase.GetDependencies(allMaterials[i]);
- if (ArrayUtility.Contains(dep, shaderPath))
- materials.Add(allMaterials[i]);
- }
- }
- scrollMaterials = GUILayout.BeginScrollView(scrollMaterials);
- {
- for (int i = 0; i < materials.Count; i++)
- {
- GUILayout.BeginHorizontal();
- {
- Object asset = AssetDatabase.LoadAssetAtPath<Object>(materials[i]);
- Texture2D assetPreview = AssetPreview.GetMiniThumbnail(asset);
- string assetName = Path.GetFileName(materials[i]);
- GUIContent content = new GUIContent(assetPreview);
- EditorGUILayout.BeginHorizontal();
- GUILayout.Label(content, GUILayout.Height(20), GUILayout.Width(20));
- GUILayout.Label(assetName, GUILayout.Height(20));
- GUILayout.FlexibleSpace();
- if (GUILayout.Button("Show"))
- {
- EditorGUIUtility.PingObject(asset);
- }
- EditorGUILayout.EndHorizontal();
- }
- GUILayout.EndHorizontal();
- }
- }
- GUILayout.EndScrollView();
- }
- #endregion
- #region GUI Helpers
- Object FolderField(Object folder, string name)
- {
- GUILayout.BeginHorizontal();
- GUILayout.Label(name);
- folder = EditorGUILayout.ObjectField(folder, typeof(Object), false) as Object;
- if (folder != null)
- {
- if (!IsValidFolder(folder))
- folder = null;
- }
- GUILayout.EndHorizontal();
- return folder;
- }
- bool IsValidFolder(Object folder)
- {
- if (folder == null) // no folder
- return false;
- string folderPath = AssetDatabase.GetAssetPath(folder);
- if (!AssetDatabase.IsValidFolder(folderPath)) // not a folder
- return false;
- return true;
- }
- void DrawHeader(string title)
- {
- EditorGUILayout.BeginVertical("Box");
- EditorGUILayout.BeginVertical("Box");
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- GUILayout.Label (title, EditorStyles.largeLabel);
- GUILayout.FlexibleSpace();
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.EndVertical();
- EditorGUILayout.EndVertical();
- }
- void DrawHorizontalLine()
- {
- GUIStyle s = new GUIStyle(GUI.skin.horizontalSlider);
- s.fixedHeight = 10;
- EditorGUILayout.LabelField("", s, GUILayout.Height(8));
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement