Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Edit Import Settings for a plugin from script in Editor

Discussion in 'Editor & General Support' started by Victor_Kallai, Oct 28, 2015.

  1. Victor_Kallai

    Victor_Kallai

    Joined:
    Mar 5, 2014
    Posts:
    123
    I want to know if there is a simple way to edit the import settings for a native file/plugin (.mm or .jar) from a script in Unity Editor. I have a lot of native plugins, and I'm tired of selecting them manually each time I update them.
    Also, Is it possible to set from a script the Compile flags property for iOS native plugins?
    I know all of this may be possible just by editing the .meta file of the plugin, but I have no idea how to parse it without breaking anything in it.
    If you have any idea, let me know.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You can use editor scripting to modify the import settings for native plugins.
    Every asset gets assigned an AssetImporter - a thing that is responsible for its importing. For plugins, the PluginImporter takes care of this.
    In order to get an instance of an importer for a given asset, you can use the AssetImporter.GetAtPath method.

    This returns an AssetImporter instance, so you should cast that (for native plugins) into a PluginImporter object and then use its methods / properties for what you want to achieve.

    I think that plugin importers have a generic mechanism of storing data in key/value pairs, so with some hackery and reverse engineering you can figure out how the compiler flags property is saved and modify that.

    By peeking into the iOSPluginImporterExtension class i could see that it defines a property called CompileFlags

    Maybe if you query for that you could get what you're looking for.
     
    Victor_Kallai likes this.
  3. Victor_Kallai

    Victor_Kallai

    Joined:
    Mar 5, 2014
    Posts:
    123
    Thanks liortal for the info, everything seems clear now. I wish Unity documentation was a bit more clear.
    Here is the sample code for everyone else:

    Code (CSharp):
    1. PluginImporter facebookPlugin = AssetImporter.GetAtPath("Assets/Facebook/Editor/iOS/FbUnityInterface.mm") as PluginImporter;
    2.             facebookPlugin.SetCompatibleWithEditor(false);
    3.             facebookPlugin.SetCompatibleWithAnyPlatform(false);
    4.             facebookPlugin.SetCompatibleWithPlatform(BuildTarget.iOS, true);
    5.             facebookPlugin.SetPlatformData(BuildTarget.iOS, "CompileFlags", "-fno-objc-arc");
     
  4. rohanlatif1

    rohanlatif1

    Joined:
    Sep 26, 2020
    Posts:
    1
  5. mpaolis_supsi

    mpaolis_supsi

    Joined:
    Sep 26, 2022
    Posts:
    1
    Hello,
    thank you for the code, I am trying to do a similar thing by using
    SetExcludeEditorFromAnyPlatform to exclude a DLL from editor based on some editor settings automatically.
    Visually it works because I can see the checkmark that appears and disappears under Exclude Platforms -> Editor
    but the change does not affect the code because I cannot see the changes until I click "Apply" in the Import Settings of the DLL.
    I tried to call:
    • SaveAndReimport of PluginImporter
    • AssetDatabase.ImportAsset with the path of DLL
    • CompilationPipeline.RequestScriptCompilation

    but none of these worked even when all together.
    Is there a correct way to apply the changes of the plugin?

    Thank you in advance
    mpaolis