Exemplo n.º 1
0
        public static void SetupGUI(TexFont font)
        {
            EditorGUI.BeginDisabledGroup(onRendering);
            GUILayout.BeginHorizontal();
            GUI.changed = false;

            GUILayout.Label("Atlas Resolution", GUILayout.Width(EditorGUIUtility.labelWidth));
            _bufferWidth  = EditorGUILayout.IntPopup(_bufferWidth, FontResolutionLabels, FontAtlasResolutions);  //, GUILayout.Width(80));
            _bufferHeight = EditorGUILayout.IntPopup(_bufferHeight, FontResolutionLabels, FontAtlasResolutions); //, GUILayout.Width(80));

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Padding", GUILayout.Width(EditorGUIUtility.labelWidth));
            _padding = EditorGUILayout.IntSlider(_padding, 1, 10);
            GUILayout.EndHorizontal();

            _render = (RenderModes)EditorGUILayout.EnumPopup(_render);
            if (GUILayout.Button("Render"))
            {
                CreateSDFAsset(font);
            }
            EditorGUI.BeginDisabledGroup(onRendering || !font.SDF_Asset);
            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Delete SDF Asset"))
            {
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(font.SDF_Asset));
                font.type = TexFontType.Font;
                font.Populate();
            }
            GUI.backgroundColor = Color.white;
            EditorGUI.EndDisabledGroup();
            {
                EditorGUILayout.Space();
                if (GUILayout.Button("Render All Fonts"))
                {
                    onRenderingBatch = (EditorUtility.DisplayDialogComplex("Confirm Action", "Are you sure? This will take few moments.\nAnd what will you do with existing SDF Asset?",
                                                                           "Overwrite", "Cancel", "Skip (Faster)"));
                    onRenderingBatch = onRenderingBatch == 1 ? 0 : (onRenderingBatch == 0 ? 1: 2);
                    if (onRenderingBatch > 0)
                    {
                        DoBatchRendering();
                    }
                }
                EditorGUI.BeginDisabledGroup(onRendering || !font.SDF_Asset);
                GUI.backgroundColor = new Color(0.5f, 0, 0.5f);
                if (GUILayout.Button("Delete All SDF Asset"))
                {
                    if (EditorUtility.DisplayDialog("Confirm Deletion", "Are you sure you want to delete ALL SDF Font Asset?", "Yes", "No"))
                    {
                        var fonts = TEXPreference.main.fontData;
                        for (int i = 0; i < fonts.Length; i++)
                        {
                            EditorUtility.DisplayProgressBar("Please wait", "Reimporting Fonts...", i / (float)fonts.Length);
                            var f = fonts[i];
                            if (f.type != TexFontType.Font_SDF)
                            {
                                continue;
                            }
                            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(f.SDF_Asset));
                            f.type = TexFontType.Font;
                            f.Populate(false);
                        }
                        TEXPreference.main.RebuildMaterial();
                        EditorUtility.ClearProgressBar();
                    }
                }
                GUI.backgroundColor = Color.white;
                EditorGUI.EndDisabledGroup();
            }

            EditorGUI.EndDisabledGroup();
            if (onRendering)
            {
                var prog = TMPro_FontPlugin.Check_RenderProgress();
                EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), prog, prog.ToString("P"));
                if (onRenderingBatch > 0)
                {
                    if (onRenderingBatch == 3)
                    {
                        EditorGUILayout.HelpBox("Will be stopped after current rendering is done", MessageType.Info);
                    }
                    else
                    {
                        GUI.backgroundColor = Color.yellow;
                        if (GUILayout.Button("Cancel"))
                        {
                            onRenderingBatch = 3;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void OnFinished(TexFont font)
        {
            var sdfPath = TEXPreference.main.MainFolderPath + "/Fonts/TMPro/" + font.id + ".asset";

            TMP_FontAsset asset;

            if (!(asset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(sdfPath)))
            {
                // It doesn't exist (or invalid), so create new
                asset = font.SDF_Asset = ScriptableObject.CreateInstance <TMP_FontAsset>();
                if (!AssetDatabase.IsValidFolder(TEXPreference.main.MainFolderPath + "/Fonts/TMPro"))
                {
                    AssetDatabase.CreateFolder(TEXPreference.main.MainFolderPath + "/Fonts", "TMPro");
                }
                AssetDatabase.CreateAsset(asset, sdfPath);
            }
            asset.fontAssetType = _render >= RenderModes.DistanceField16 ? TMP_FontAsset.FontAssetTypes.SDF : TMP_FontAsset.FontAssetTypes.Bitmap;
            FaceInfo face = GetFaceInfo(_faceInfo, 1);

            asset.AddFaceInfo(face);


            _atlasInfo = new Texture2D(_bufferWidth, _bufferHeight, TextureFormat.Alpha8, false, true);
            var _buffer32 = Array.ConvertAll(_buffers, x => new Color32(x, x, x, x));

            _atlasInfo.SetPixels32(_buffer32);
            _atlasInfo.Apply(false, true);


            // Add GlyphInfo[] to Font Asset
            TMP_Glyph[] glyphs = GetGlyphInfo(_glyphsInfo, 1);
            asset.AddGlyphInfo(glyphs);

            // Get and Add Kerning Pairs to Font Asset

            string       fontFilePath = AssetDatabase.GetAssetPath(font.Font_Asset);
            KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize);

            asset.AddKerningInfo(kerningTable);


            // Add Line Breaking Rules
            //LineBreakingTable lineBreakingTable = new LineBreakingTable();
            //


            // Add Font Atlas as Sub-Asset
            asset.atlas     = _atlasInfo;
            _atlasInfo.name = font.id + " Atlas";
#if !(UNITY_5_3 || UNITY_5_4)
            _atlasInfo.hideFlags = HideFlags.HideInHierarchy;
#endif
            AssetDatabase.AddObjectToAsset(_atlasInfo, asset);

            // Create new Material and Add it as Sub-Asset
            Shader   default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection;
            Material tmp_material   = new Material(default_Shader);

            tmp_material.name = _atlasInfo + " Material";
            tmp_material.SetTexture(ShaderUtilities.ID_MainTex, _atlasInfo);
            tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, _atlasInfo.width);
            tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, _atlasInfo.height);


            tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, asset.normalStyle);
            tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, asset.boldStyle);

            int spread = _render >= RenderModes.DistanceField16 ? _padding + 1 : _padding;
            tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF.

            asset.material = tmp_material;
#if !(UNITY_5_3 || UNITY_5_4)
            tmp_material.hideFlags = HideFlags.HideInHierarchy;
#endif
            AssetDatabase.AddObjectToAsset(tmp_material, asset);

            font.type      = TexFontType.Font_SDF;
            font.SDF_Asset = asset;
            font.Populate();
            AssetDatabase.SaveAssets();
        }