Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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,480
    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");
     
    tarasfromlviv, Zelek and liortal like this.
  4. rohanlatif1

    rohanlatif1

    Joined:
    Sep 26, 2020
    Posts:
    1
unityunity