A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3using UnityEditor;
4
5
6namespace TMPro.EditorUtilities
7{
8 /// <summary>
9 /// Asset post processor used to handle text assets changes.
10 /// This includes tracking of changes to textures used by sprite assets as well as font assets potentially getting updated outside of the Unity editor.
11 /// </summary>
12 internal class TMPro_TexturePostProcessor : AssetPostprocessor
13 {
14 private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
15 {
16 // Only run post processor after the Editor has been fully loaded.
17 if (Time.frameCount == 0)
18 return;
19
20 bool textureImported = false;
21
22 foreach (var asset in importedAssets)
23 {
24 // Return if imported asset path is outside of the project.
25 if (asset.StartsWith("Assets/", StringComparison.OrdinalIgnoreCase) == false)
26 continue;
27
28 Type assetType = AssetDatabase.GetMainAssetTypeAtPath(asset);
29
30 if (assetType == typeof(TMP_FontAsset))
31 {
32 TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath(asset, typeof(TMP_FontAsset)) as TMP_FontAsset;
33
34 // Only refresh font asset definition if font asset was previously initialized.
35 if (fontAsset != null && fontAsset.m_CharacterLookupDictionary != null)
36 TMP_EditorResourceManager.RegisterFontAssetForDefinitionRefresh(fontAsset);
37
38 continue;
39 }
40
41 if (assetType == typeof(Texture2D))
42 textureImported = true;
43 }
44
45 // If textures were imported, issue callback to any potential text objects that might require updating.
46 if (textureImported)
47 TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, null);
48 }
49 }
50
51 internal class TMP_FontAssetPostProcessor : UnityEditor.AssetModificationProcessor
52 {
53 static AssetDeleteResult OnWillDeleteAsset(string path, RemoveAssetOptions opt)
54 {
55 if (AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(TMP_FontAsset))
56 TMP_ResourceManager.RebuildFontAssetCache();
57
58 return AssetDeleteResult.DidNotDelete;
59 }
60 }
61}