Created
August 21, 2024 18:50
-
-
Save yasirkula/edf4ba1bb7c5c18800e791e01bc9bcd7 to your computer and use it in GitHub Desktop.
Prevent dynamic TMP_FontAssets from constantly showing up in your source control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To
Simply create a folder called Editor inside your Project window and add this script inside it. Then, select a font asset and at the top of the Inspector, click Lock Font Assets. While this option is active, the font asset's inspector will be hidden (to avoid accidental unsaved changes) and the font asset won't be saved to the disk. Note that this setting applies to all font assets in the project.