/// <summary> /// This method installs all neacessary data for MatEdit CG support /// </summary> public static void Install() { // Generate all paths string installPath = Path.Combine(MatEdit.ProjectPath(), MatEdit.ROOT_PATH); string versionFile = Path.Combine(installPath, Content.VERSION_FILE); string cgFile = Path.Combine(MatEdit.ProjectPath(), Content.MATGUI_CG); string cgCurveFile = Path.Combine(installPath, Content.MATGUI_CG_CURVE); string cgGradientFile = Path.Combine(installPath, Content.MATGUI_CG_GRADIENT); // Check if the install location already exists - if not create it if (!Directory.Exists(installPath)) { Directory.CreateDirectory(installPath); } // Install the seperate files InstallFile(versionFile, Content.VERSION_FILE); InstallFile(cgFile, Content.CG_PATH + Content.MATGUI_CG); InstallFile(cgCurveFile, Content.CG_PATH + Content.MATGUI_CG_CURVE); InstallFile(cgGradientFile, Content.CG_PATH + Content.MATGUI_CG_GRADIENT); }
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(); }
//---------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------\\ //------------------------------------< HELPER METHODS >---------------------------------\\ //---------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------\\ #region Helper Methods /// <summary> /// Checks for the installed Version number /// </summary> /// <returns>The Version number as string</returns> public static string GetInstalledVersionNumber() { // Generate all paths string installPath = Path.Combine(MatEdit.ProjectPath(), MatEdit.ROOT_PATH); string versionFile = Path.Combine(installPath, Content.VERSION_FILE); // Check if a version file is installed if (File.Exists(versionFile)) { // If so open the xml and search for the version tag XmlDocument xmlInstalledVersion = new XmlDocument(); xmlInstalledVersion.Load(versionFile); XmlNodeList installedVersionNodes = xmlInstalledVersion.GetElementsByTagName("version"); foreach (XmlNode xn in installedVersionNodes) { // return its value return(xn.InnerText); } } // return -1 if no file is installed return("-1"); }
/// <summary> /// Converts a system absolute path to the Asset folder as root /// </summary> /// <param name="path">The absolute path to convert</param> /// <returns>The path with start in the Asset folder</returns> internal static string AbsoluteToAssetPath(string path) { path = Directory.GetParent(path).FullName; path = path.Replace(MatEdit.ProjectPath(), ""); path = path.Replace(@"\", "/"); path = path.Substring(1, path.Length - 1); return(path); }
/// <summary> /// This method uninstalls all neacessary data for MatEdit CG support /// </summary> public static void Uninstall() { // Generate all paths string installPath = Path.Combine(MatEdit.ProjectPath(), MatEdit.ROOT_PATH); string cgFile = Path.Combine(MatEdit.ProjectPath(), Content.MATGUI_CG); // Check if the install location already exists - if so delete it if (Directory.Exists(installPath)) { Directory.Delete(installPath, true); } // Uninstall remaining files if (File.Exists(cgFile)) { File.Delete(cgFile); } }
private static void DistributeShader() { // Filter Objects for Shaders Object[] selection = Selection.objects; List <Shader> shader = new List <Shader>(); for (int o = 0; o < selection.Length; o++) { if (selection[0].GetType() == typeof(Shader)) { shader.Add((Shader)selection[0]); } } // Create Shader Editor for (int s = 0; s < shader.Count; s++) { string fileName = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(shader[s])); string shaderPath = Path.Combine(MatEdit.ProjectPath(), AssetDatabase.GetAssetPath(shader[s])); string path = EditorUtility.SaveFolderPanel("Distribute " + shader[s].name + " To", Application.dataPath, fileName + "Distribution"); Distribute.ToPath(shaderPath, path); } }
/// <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); }
public static string GetAsset(string content) { return(MatEdit.AbsoluteToAssetPath(Get(content))); }