Search Unity

Preprocessor directive for package

Discussion in 'Package Manager' started by plmx, Jan 23, 2021.

  1. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    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!
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    plmx likes this.
  3. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    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
     
    Last edited: Jan 23, 2021
  4. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    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".
     
    plmx likes this.
  5. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    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
     
    Last edited: Jan 23, 2021
    Favo-Yang likes this.
  6. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    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 :)