public static void ToPath(string shader, string path) { string distribute = Content.Get(DISTRIBUTION_PATH + MATEDIT_DISTRIBUTE); distribute = Directory.GetParent(distribute).FullName; distribute = Path.Combine(distribute, MATEDIT_DISTRIBUTE); string shaderContent = FileOperator.ReadStringFromFile(shader); Match shaderEditor = Regex.Match(shaderContent, "CustomEditor.*?" + '"' + ".*?" + '"'); shaderEditor = Regex.Match(shaderEditor.Value, '"' + ".*?" + '"'); string shaderEditorName = shaderEditor.Value.Replace('"' + "", "").Replace(" ", ""); string shaderEditorPath = GetMonoScriptOfType(shaderEditorName); if (shaderEditorPath == "") { return; } shaderEditorPath = Path.Combine(MatEdit.ProjectPath(), shaderEditorPath); CopyDirectory(distribute, path); string newShaderPath = Path.Combine(path, Path.GetFileName(shader)); shaderContent = Regex.Replace(shaderContent, "CustomEditor.*?" + '"' + ".*?" + '"', "CustomEditor " + '"' + "MED_" + shaderEditorName + '"'); FileOperator.WriteStringToFile(shaderContent, newShaderPath); string scriptContent = FileOperator.ReadStringFromFile(shaderEditorPath); scriptContent = scriptContent.Replace(shaderEditorName, "MED_" + shaderEditorName); scriptContent = scriptContent.Replace("MB.MatEdit", "MB.MatEditDistribute"); FileOperator.WriteStringToFile(scriptContent, Path.Combine(Path.Combine(path, "Editor"), "MED_" + Path.GetFileName(shaderEditorPath))); AssetDatabase.Refresh(); }
/// <summary> /// Generates a new ShaderGUI for a specified Shader /// </summary> /// <param name="shader">The Shader which should get a new ShaderGUI</param> private static void CreateShaderEditor(Shader shader) { // Generate the absolute Path for the Shader string path = Path.GetFullPath(AssetDatabase.GetAssetPath(shader)); path = Path.Combine(MatEdit.ProjectPath(), path); // Get the filename of the shader -> shader.name only returns the internal shader name string shaderName = Path.GetFileNameWithoutExtension(path); // Get the folder which locates the Shader string rootPath = Directory.GetParent(path).FullName; // Generate the path used for the Editor script string editorRootPath = Path.Combine(rootPath, "Editor"); string editorPath = Path.Combine(editorRootPath, shaderName + "_GUI.cs"); // Abandon task if a ShaderGUI is already located if (File.Exists(editorPath)) { return; } // Abandon if a ShaderGUI is already paired but the user wants to override it string shaderContent = FileOperator.ReadStringFromFile(path); if (Regex.IsMatch(shaderContent, "CustomEditor.*?" + '"' + ".*?" + '"')) { bool overwrite = EditorUtility.DisplayDialog("ShaderGUI Creation", "The shader:\n" + shaderName + "\n" + "allready contains a definition for a ShaderGUI.\n" + "Do you want to overwrite it?", "Yes", "No"); if (!overwrite) { return; } shaderContent = Regex.Replace(shaderContent, "CustomEditor.*?" + '"' + ".*?" + '"', "CustomEditor " + '"' + shaderName + "_GUI" + '"'); } else { // Make the last } the last for good int lastIndex = shaderContent.LastIndexOf("}"); shaderContent = shaderContent.Substring(0, lastIndex + 1); shaderContent = Regex.Replace(shaderContent, "}$", " CustomEditor " + '"' + shaderName + "_GUI" + '"' + "\n" + "}"); } // Write new Shader content to file FileOperator.WriteStringToFile(shaderContent, path); // Folder // If the Editor folder is not created yet - create one if (!Directory.Exists(editorRootPath)) { Directory.CreateDirectory(editorRootPath); } // Template // Get the Template for a SimpleShaderGUI string template = Templates.Get(Templates.SIMPLE_SHADER_GUI); // Replace the placeholder with the actual class name template = template.Replace(Templates.SHADER_GUI_CLASS_NAME, Path.GetFileNameWithoutExtension(editorPath)); // Write the template to the script file FileOperator.WriteStringToFile(template, editorPath); // Reimport // Generate the Asset folder located path and reimport the ShaderGUI script string assetPath = MatEdit.AbsoluteToAssetPath(editorPath) + "/" + Path.GetFileName(editorPath); AssetDatabase.ImportAsset(assetPath); }