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,029
    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:
    29
    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.
     
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,029
    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:
    29
    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,029
    Ok thanks for the clarification. I will give it a shot.