Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Created August 21, 2024 18:50
Show Gist options
  • Select an option

  • Save yasirkula/edf4ba1bb7c5c18800e791e01bc9bcd7 to your computer and use it in GitHub Desktop.

Select an option

Save yasirkula/edf4ba1bb7c5c18800e791e01bc9bcd7 to your computer and use it in GitHub Desktop.

Revisions

  1. yasirkula created this gist Aug 21, 2024.
    51 changes: 51 additions & 0 deletions FontAssetSaveDisabler.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    using System;
    using TMPro;
    using TMPro.EditorUtilities;
    using UnityEditor;
    using UnityEngine;

    [CustomEditor(typeof(TMP_FontAsset))]
    public class FontAssetSaveDisabler : TMP_FontAssetEditor
    {
    private static bool FontAssetsLocked
    {
    get => EditorPrefs.GetBool("TMPFontsLocked", false);
    set => EditorPrefs.SetBool("TMPFontsLocked", value);
    }

    public override void OnInspectorGUI()
    {
    if (GUILayout.Button(FontAssetsLocked ? "Unlock Font Assets" : "Lock Font Assets"))
    {
    FontAssetsLocked = !FontAssetsLocked;
    GUIUtility.ExitGUI();
    }

    if (!FontAssetsLocked)
    {
    EditorGUILayout.Space();
    base.OnInspectorGUI();
    }
    }

    // Credit: https://discussions.unity.com/t/tmpro-dynamic-font-asset-constantly-changes-in-source-control/868941/29
    private class SaveHandler : AssetModificationProcessor
    {
    private static string[] OnWillSaveAssets(string[] paths)
    {
    if (FontAssetsLocked)
    {
    int index = 0;
    foreach (string path in paths)
    {
    if (!typeof(TMP_FontAsset).IsAssignableFrom(AssetDatabase.GetMainAssetTypeAtPath(path)))
    paths[index++] = path;
    }

    Array.Resize(ref paths, index);
    }

    return paths;
    }
    }
    }