Home > Net >  Unity - Callback when a package is removed but before recompile
Unity - Callback when a package is removed but before recompile

Time:06-29

I'm creating a Unity package I want to give to other people to help with translations. Currently, I only handle the Text component but I would like to handle TMPro too.

My idea is to create a conditional compilation tag to prevent any TMPro line of code from compiling (and causing errors) when it's not imported. That part is the easy one, I managed to do that. I also managed to add the tag when TMPro gets imported or when it's already in the project as I import my custom package. The thing I cannot manage to do is to remove my tag when TMPro gets removed. Since everything is reloading, the compilation crashes because it cannot find TMPro anymore. Thus my question in the title.

I'm also completely open to any other solution than this one, as long as it doesn't involve assemblies.

More infos : I want my package to work in Unity 2019, 2020 and 2021

CodePudding user response:

Use the AssetPostprocessor class. With the method on below, it gives you import, delete and move callbacks.

public class AssetManager : AssetPostprocessor
{
    public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
        string[] movedFromAssetPaths)
    {
        foreach (var deletedAsset in deletedAssets)
        {
            Debug.Log(deletedAsset);
        }
    }
}

The following code must be in the Editor Folder.

  • Related