Preprocessor directive for package

Hi,

I am trying to use conditional compilation with #if <SOMETHING> for a package installed via the package manager, specifically, the built-in package com.unity.xr.oculus. That is, I want my own code only to be compiled if another, not mine package is installed.

Is this possible? I’ve tried to understand whether I can use .asmdef or .asmref files but I frankly got lost in those settings, and I’m unsure if this works like I want to.

Can someone help me out here?

Thanks!

The “Version Defines” section on assembly definitions is what you looking for. It allows you to create defines for your own assembly definition based on wether or packages are installed.

1 Like

Hi Adrian,

thanks for your response! Could you elaborate on the use of assemblies? Say I have a brand-new Unity project and I’d like to write a script with an #if <SOMETHING> for the presence of com.unity.xr.oculus - what should I do? Do I create an “Assembly Definition” for the entire project and add a version define in there? Or do I create an “Assembly Definition” for the Oculus Package? If so, where?

Thanks!

Philip

I’d recommend reading the documentation page I linked above. There’s a lot involved with assembly definitions and Unity’s compilation pipeline, so it’s good to have an overview.

Basically, you create an assembly definition anywhere in your project and that asmdef will group together all scripts in the same and all subfolders. You could create one directly in the Assets folder or create one just in the folder of the script you want to create a define for.

Note that the compilation/references rules change when creating an assembly definition, so you maybe will get errors after creating one and will have to set up the references correctly. E.g. scripts without an asmdef can reference scripts from asmdefs but not the other way around.

After you’ve got the asmdef working you can set up the defines in the asmdef’s inspector under “Version Defines”.

1 Like

Thanks!

So, what I did was this:

  • Added an Assembly Definition File (.asmdef) for the (entire) project. Things did not compile afzer that, so I manually added references to every assembly I was using.
  • Afterwards, to add a preprocessor directives, I added a Version Define. This allows using a preprocessor directive #if <NAME> to conditionally compile on the presence of the package.

Now the editor compiles, and I can use the (Editor) script which changes the settings without any issue.

However, I can’t build anymore. It seems the asmdef is not picked up on build. Actually, the code which requires the conditional compilation is an editor script in an Editor folder. I tried placing the asmdef in that folder only (there is only one script in there), but I still get compile errors when building (which is weird since I thought Editor folders are ignored on build?).

?

Philip

1 Like

Did you get round to figuring this out? This part doesn’t seem to be Package Manager related and I’d rather get this thread moved somewhere to get you further help if so :slight_smile: