Esempio n. 1
0
    public override void OnInspectorGUI()
    {
        tk2dFont gen = (tk2dFont)target;

        if (gen.proxyFont)
        {
            GUILayout.Label("This font is managed by a Sprite Collection");
            return;
        }
        gen.Upgrade();

        EditorGUILayout.BeginVertical();

        DrawDefaultInspector();
        tk2dGuiUtility.SpriteCollectionSize(gen.sizeDef);

        // Warning when texture is compressed
        if (gen.texture != null)
        {
            Texture2D tex = (Texture2D)gen.texture;
            if (tex && IsTextureCompressed(tex))
            {
                int buttonPressed;
                if ((buttonPressed = tk2dGuiUtility.InfoBoxWithButtons(
                         "Font texture appears to be compressed. " +
                         "Quality will be lost and the texture may appear blocky in game.\n" +
                         "Do you wish to change the format?",
                         tk2dGuiUtility.WarningLevel.Warning,
                         new string[] { "16bit", "Truecolor" }
                         )) != -1)
                {
                    if (buttonPressed == 0)
                    {
                        ConvertTextureToFormat(tex, TextureImporterFormat.Automatic16bit);
                    }
                    else
                    {
                        ConvertTextureToFormat(tex, TextureImporterFormat.AutomaticTruecolor);
                    }
                }
            }
        }

        // Warning when gradient texture is compressed
        if (gen.gradientTexture != null &&
            (gen.gradientTexture.format != TextureFormat.ARGB32 && gen.gradientTexture.format != TextureFormat.RGB24 && gen.gradientTexture.format != TextureFormat.RGBA32))
        {
            if (tk2dGuiUtility.InfoBoxWithButtons(
                    "The gradient texture should be truecolor for best quality. " +
                    "Current format is " + gen.gradientTexture.format.ToString() + ".",
                    tk2dGuiUtility.WarningLevel.Warning,
                    new string[] { "Fix" }
                    ) != -1)
            {
                ConvertTextureToFormat(gen.gradientTexture, TextureImporterFormat.AutomaticTruecolor);
            }
        }

        if (GUILayout.Button("Commit..."))
        {
            if (gen.bmFont == null || gen.texture == null)
            {
                EditorUtility.DisplayDialog("BMFont", "Need an bmFont and texture bound to work", "Ok");
                return;
            }

            if (gen.material == null)
            {
                gen.material = new Material(GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked));
                string materialPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "material.mat");
                AssetDatabase.CreateAsset(gen.material, materialPath);
            }

            if (gen.data == null)
            {
                string bmFontPath = AssetDatabase.GetAssetPath(gen).Replace(".prefab", "data.prefab");

                GameObject go = new GameObject();
                go.AddComponent <tk2dFontData>();
                tk2dEditorUtility.SetGameObjectActive(go, false);

                Object p = PrefabUtility.CreateEmptyPrefab(bmFontPath);
                PrefabUtility.ReplacePrefab(go, p);

                GameObject.DestroyImmediate(go);
                AssetDatabase.SaveAssets();

                gen.data = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(tk2dFontData)) as tk2dFontData;
            }

            ParseBMFont(AssetDatabase.GetAssetPath(gen.bmFont), gen.data, gen);

            if (gen.manageMaterial)
            {
                Shader s = GetShader(gen.gradientTexture != null, gen.data != null && gen.data.isPacked);
                if (gen.material.shader != s)
                {
                    gen.material.shader = s;
                    EditorUtility.SetDirty(gen.material);
                }
                if (gen.material.mainTexture != gen.texture)
                {
                    gen.material.mainTexture = gen.texture;
                    EditorUtility.SetDirty(gen.material);
                }
                if (gen.gradientTexture != null && gen.gradientTexture != gen.material.GetTexture("_GradientTex"))
                {
                    gen.material.SetTexture("_GradientTex", gen.gradientTexture);
                    EditorUtility.SetDirty(gen.material);
                }
            }

            gen.data.version = tk2dFontData.CURRENT_VERSION;

            gen.data.material         = gen.material;
            gen.data.textureGradients = gen.gradientTexture != null;
            gen.data.gradientCount    = gen.gradientCount;
            gen.data.gradientTexture  = gen.gradientTexture;

            gen.data.invOrthoSize     = 1.0f / gen.sizeDef.OrthoSize;
            gen.data.halfTargetHeight = gen.sizeDef.TargetHeight * 0.5f;

            // Rebuild assets already present in the scene
            tk2dTextMesh[] sprs = Resources.FindObjectsOfTypeAll(typeof(tk2dTextMesh)) as tk2dTextMesh[];
            foreach (tk2dTextMesh spr in sprs)
            {
                spr.ForceBuild();
            }

            EditorUtility.SetDirty(gen);
            EditorUtility.SetDirty(gen.data);

            // update index
            tk2dEditorUtility.GetOrCreateIndex().AddOrUpdateFont(gen);
            tk2dEditorUtility.CommitIndex();
        }

        EditorGUILayout.EndVertical();

        GUILayout.Space(64);
    }
    public override void OnInspectorGUI()
    {
        tk2dFont gen = (tk2dFont)target;

        if (gen.proxyFont)
        {
            GUILayout.Label("This font is managed by a Sprite Collection");
            return;
        }
        gen.Upgrade();

        EditorGUILayout.BeginVertical();

        DrawDefaultInspector();
        tk2dGuiUtility.SpriteCollectionSize(gen.sizeDef);

        // Warning when texture is compressed
        if (gen.texture != null)
        {
            Texture2D tex = (Texture2D)gen.texture;
            if (tex && IsTextureCompressed(tex))
            {
                int buttonPressed;
                if ((buttonPressed = tk2dGuiUtility.InfoBoxWithButtons(
                         "Font texture appears to be compressed. " +
                         "Quality will be lost and the texture may appear blocky in game.\n" +
                         "Do you wish to change the format?",
                         tk2dGuiUtility.WarningLevel.Warning,
                         new string[] { "Truecolor" }
                         )) != -1)
                {
                    if (buttonPressed == 0)
                    {
                        ConvertTextureToUncompressed(tex);
                    }
                }
            }
        }

        // Warning when gradient texture is compressed
        if (gen.gradientTexture != null &&
            (gen.gradientTexture.format != TextureFormat.ARGB32 && gen.gradientTexture.format != TextureFormat.RGB24 && gen.gradientTexture.format != TextureFormat.RGBA32))
        {
            if (tk2dGuiUtility.InfoBoxWithButtons(
                    "The gradient texture should be truecolor for best quality. " +
                    "Current format is " + gen.gradientTexture.format.ToString() + ".",
                    tk2dGuiUtility.WarningLevel.Warning,
                    new string[] { "Fix" }
                    ) != -1)
            {
                ConvertTextureToUncompressed(gen.gradientTexture);
            }
        }


        string message = @"Due to changes in the prefab system in Unity 2018.3, the commit functionality has been moved." +
                         "Exit prefab edit mode, select your font collection and click 2D Toolikt / Commit Font in the main menu";

        tk2dGuiUtility.InfoBox(message, tk2dGuiUtility.WarningLevel.Warning);


        GUI.enabled = false;
        if (GUILayout.Button("Commit..."))
        {
        }
        GUI.enabled = true;

        EditorGUILayout.EndVertical();

        GUILayout.Space(64);
    }