// // Private methods // //------------------------------------------------------------------------------ private TemplateElement AddTemplateElement(String name) { String finalName = UniquifyElementName(name); TemplateElement element = new TemplateElement(finalName, "{0}", String.Empty, String.Empty); m_targetTemplate.TemplateElements.Add(element); return(element); }
//------------------------------------------------------------------------------ private static void GenerateFileFromTemplateElement(TemplateElement element, Dictionary <String, String> tokenReplacements) { // Set template specific replacements here tokenReplacements[@"%TEMPLATE_NAME%"] = element.InstanceName; String generatedContents = element.Contents; foreach (KeyValuePair <String, String> kvp in tokenReplacements) { generatedContents = Regex.Replace(generatedContents, kvp.Key, kvp.Value); } File.WriteAllText(element.InstanceAssetPath, generatedContents); AssetDatabase.ImportAsset(element.InstanceAssetPath, ImportAssetOptions.ForceUpdate); }
//------------------------------------------------------------------------------ private void ImportDraggedTextAssetIntoCurrentElement() { for (int index = 0; index < DragAndDrop.objectReferences.Length; ++index) { TextAsset asset = DragAndDrop.objectReferences[index] as TextAsset; String path = DragAndDrop.paths[index]; if (asset == null) { continue; } TemplateElement newElement = m_targetTemplate.TemplateElements[m_selectedElement]; newElement.Extension = System.IO.Path.GetExtension(path); newElement.Contents = asset.text; } }
//------------------------------------------------------------------------------ private void ImportDraggedTextAssetAsNewElement() { for (int index = 0; index < DragAndDrop.objectReferences.Length; ++index) { TextAsset asset = DragAndDrop.objectReferences[index] as TextAsset; String path = DragAndDrop.paths[index]; if (asset == null) { continue; } TemplateElement newElement = AddTemplateElement(asset.name); newElement.Extension = System.IO.Path.GetExtension(path); newElement.TemplateTokenFormat = String.Concat("{0}", newElement.ElementName); newElement.Contents = asset.text; SelectLastElement(); } }
//------------------------------------------------------------------------------ private void TemplateDetailsGUI() { if (m_targetTemplate.TemplateElements.Count <= 0) { return; } EditorGUILayout.LabelField("Element Details", new GUIStyle("OL Title")); Rect elementDetailsRect = EditorGUILayout.BeginVertical(m_elementSelectionBgStyle); TemplateElement element = m_targetTemplate.TemplateElements[m_selectedElement]; String elementNameTooltip = "Token for use in the %ELEMENT_NAME()% substition when referring to other elements in a template."; GUIContent elementNameContent = new GUIContent("Element Name", elementNameTooltip); element.ElementName = FilterElementName(EditorGUILayout.TextField(elementNameContent, element.ElementName)); String templateTokenFormatTooltip = "C# style Format string used to generate the template token and file name for the output of this element. {0} will be replaced by the user supplied token when the template is instantiated."; GUIContent templateTokenFormatContent = new GUIContent("Template Token Format", templateTokenFormatTooltip); element.TemplateTokenFormat = FilterFileNameFormat(EditorGUILayout.TextField(templateTokenFormatContent, element.TemplateTokenFormat)); String fileExtensionTooltip = "Optional file extension used when generating the file name for the output of this element."; GUIContent fileExtensionContent = new GUIContent("File Extension", fileExtensionTooltip); element.Extension = FilterExtension(EditorGUILayout.TextField(fileExtensionContent, element.Extension)); EditorGUILayout.Space(); try { String exampleFileName = String.Format(element.TemplateTokenFormat, "Example"); String exampleHelpText = String.Format("Example generated file name: \"{0}{1}\"", exampleFileName, element.Extension); EditorGUILayout.HelpBox(exampleHelpText, MessageType.Info); } catch (Exception) { EditorGUILayout.HelpBox("Invalid file name format. Please use {0} to insert the user specified name into the file name.", MessageType.Error); } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Contents", EditorStyles.largeLabel); GUILayout.FlexibleSpace(); String importTooltip = "Import a file to populate this template element."; GUIContent importContent = new GUIContent("Import...", importTooltip); bool importFile = GUILayout.Button(importContent, EditorStyles.miniButton); if (importFile) { String path = EditorUtility.OpenFilePanel("Import code file", "", ""); if (!String.IsNullOrEmpty(path)) { using (System.IO.StreamReader fileReader = System.IO.File.OpenText(path)) { element.Contents = fileReader.ReadToEnd(); } GUI.FocusControl(String.Empty); } } EditorGUILayout.EndHorizontal(); m_scrollState = EditorGUILayout.BeginScrollView(m_scrollState, GUILayout.ExpandHeight(true)); String uiNormalizedContents = EditorUtil.NormalizeLineEndings(element.Contents, EditorUtil.EolType.Unix); if (uiNormalizedContents.Length <= EditorUtil.MaxTextAreaStringLength) { // Our template is at an editable length String rawContents = EditorGUILayout.TextArea(uiNormalizedContents, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true)); // If editing has made the template too long then we trim it and force the // TextArea to update by removing focus if (rawContents.Length > EditorUtil.MaxTextAreaStringLength) { rawContents = rawContents.Substring(0, EditorUtil.MaxTextAreaStringLength); GUI.FocusControl(String.Empty); } if (GUI.changed) { element.Contents = EditorUtil.NormalizeLineEndings(rawContents, TemplatePrefs.Eol); } } else { // Template is not at an editable length EditorGUILayout.HelpBox("Template is too long for editing. Please import a shorter template.", MessageType.Error); } EditorGUILayout.EndScrollView(); EditorGUILayout.EndVertical(); // Cache the rect for this UI area for later use in drag & drop operations if (Event.current.type == EventType.Repaint) { m_elementDetailsRect = elementDetailsRect; } }