No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMPro_TexturePostProcessor.cs 2.4KB

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