Search Unity

How to programmatically detect if package X is installed? Compilation failing.

Discussion in 'Package Manager' started by jerotas, May 13, 2019.

  1. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Let's say Physics3D or Physics2D. I assume there's code to detect if those are installed. I believe that it would need to be a "#if" like the platform compilation directives in order to be usable.

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    What I'm trying to do is make sure that Physics code in my plugin (Master Audio) doesn't try to compile if physics package is disabled. Is it possible?

    Thanks in advance!
     
  2. mathieur

    mathieur

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    31
    Certainly! You can use the Version Defines section of your package's assembly definitions to control this.

    You would simply set a define of PHY_EXISTS (or whatever keyword you want) for the resource com.unity.modules.physics (or whatever other package).

    Then you can use this define to either bracket your code within a #if PHY_EXISTS, or alternatively, you can add define constraints to the assembly definitions in your package, and set the constraint to PHY_EXISTS. This would then only compile this entire assembly if the physics package exists.
     
    ChrisAshtear and ModLunar like this.
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Thanks for posting. When you are talking about "assembly definitions" are you talking about .asmdef files? We don't currently have one for the plugin and I'm reluctant to add one.

    I did some testing with 2 of our plugins in the same project before, by adding .asmdef files to both of them to test if the compilation was faster if only making changes to one plugin's code, but found that compilation time actually *increased* with the .asmdef's so I abandoned the idea since that wasn't the point of those files in the first place. The point was to decrease compilation times - for me.

    If you're not talking about .asmdef files, then where is this controlled? Could you show an example either way or a reference of what you're talking about?

    And yes, I'm wanting the project to compile, but omit certain features if physics is not installed.

    Thanks!
     
  4. mathieur

    mathieur

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    31
    Yes, I do mean the asmdef files when referring to assembly definitions. I believe one of the goal would be to localize compilation to only modified code, but it's possible the system isn't perfect yet. In any case, it shouldn't cause any major difference to compilation times as far as I'm aware, and would allow you to achieve the conditional compilation that you seek for this feature...
     
  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Ok thanks for the clarification. I will give it a shot.
     
  6. moonproxymode

    moonproxymode

    Joined:
    Feb 20, 2017
    Posts:
    10
    Hey @mathieur I am also trying to do this yet I want to check if the developer is using SteamVR or OVR (Occulus integration). These packages, although imported into the project, do not show up in that dropdown. I then ran some code to print out the names of the active packages in the project and they don't show up there either. I've noticed in the project directory that they are under the 'Assets' folder, not the 'Packages' one, but I cannot change that fact, as the import does this automatically. Is that the issue?

    What is the solution?
    Thanks!
     
    altair2020 likes this.
  7. mathieur

    mathieur

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    31
    @moonproxymode I think you can edit the asmdef directly to add a package by string if it is not included in your project (see screenshot below of the asmdef text file content)

    But for your case it won't help if the packages are under `Assets` folder. In this case they are not seen as actual packages so you will have to put them under the `Packages` folder for this to work (that is if they are actual package with a package.json at their root).

    upload_2021-4-1_15-24-15.png
     
    roccob_unity and thelebaron like this.