Exemplo n.º 1
0
        private void CreateFileFromTemplate()
        {
            templateSettings.enableSyntaxHighlighting = false;

            //Create the file
            string sourceCode = TemplateBuilder.BuildCodeFromTemplate(template, templateSettings);
            string filePath   = templateCreateFilePath;

            File.WriteAllText(filePath, sourceCode);

            //Open the file
            AssetDatabase.Refresh();
            Object createdFile = AssetDatabase.LoadAssetAtPath <Object>(filePath);

            Selection.activeObject = createdFile;
            AssetDatabase.OpenAsset(createdFile);

            //Close this window
            Close();
        }
        /// Draws the UI of the template builder window
        private void OnGUI()
        {
            RebuildTemplateListIfNecessary();
            ApplyMinimumsToWindow();

            EditorGUIUtility.labelWidth = 100.0f;
            sharedWindowPosition        = position;

            //Update creation path
            if (!string.IsNullOrEmpty(templateCreateFolderPath))
            {
                lastKnownCreationPath = templateCreateFolderPath;
            }

            //Update the preview
            if (codePreview == null && template != null)
            {
                codePreview = TemplateBuilder.BuildCodeFromTemplate(template, templateSettings);
                codePreview = codePreview.Replace("\t", "    ");                 // Unity doesn't render tabs very well in the editor
            }

            if (codePreviewStyle == null)
            {
                codePreviewStyle           = new GUIStyle("box");
                codePreviewStyle.wordWrap  = false;
                codePreviewStyle.alignment = TextAnchor.UpperLeft;
                codePreviewStyle.padding   = new RectOffset(6, 6, 6, 6);
                codePreviewStyle.richText  = true;

                codePreviewStyle.normal.textColor          =
                    codePreviewStyle.active.textColor      =
                        codePreviewStyle.focused.textColor =
                            Color.white;

                codePreviewStyle.normal.background          =
                    codePreviewStyle.active.background      =
                        codePreviewStyle.focused.background =
                            AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/Fright/Editor/Skins/CodePreviewBackground.png");

                Font firaFont = AssetDatabase.LoadAssetAtPath <Font>("Assets/Fright/Editor/Skins/FiraCode-Regular.ttf");

                if (firaFont)
                {
                    codePreviewStyle.font     = firaFont;
                    codePreviewStyle.fontSize = 12;
                }
            }

            //Drawing
            EditorGUI.BeginChangeCheck();
            {
                EditorGUILayout.BeginHorizontal();
                {
                    //Template Settings
                    EditorGUILayout.BeginVertical(GUILayout.Width(SETTINGS_PANEL_WIDTH), GUILayout.ExpandHeight(true));
                    {
                        GUI.enabled = !EditorApplication.isCompiling;
                        {
                            DrawTemplateSettings();
                        }
                        GUI.enabled = true;
                    }
                    EditorGUILayout.EndVertical();

                    //Template Preview
                    EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                    {
                        DrawTemplatePreview();
                    }
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndHorizontal();

                DrawToolbar();
            }
            if (EditorGUI.EndChangeCheck())
            {
                codePreview = null;

                //Apply any peristent template settings
                if (template != null && templateSettings != null)
                {
                    templateSettings.SavePersistentSettings(template);
                }
            }
        }