Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Only include attribute when IL2CPP is being used

Discussion in 'Scripting' started by fdart, Aug 31, 2023.

  1. fdart

    fdart

    Joined:
    Jan 4, 2019
    Posts:
    17
    On Windows, it seems like ENABLE_MONO and ENABLE_IL2CPP are both defined for PC, irrespective of the currently selected scripting backend. Is there a way to check which scripting backend is used at runtime/preprocessor time?
    We'd like to only define the attribute below when appropriate.
    Code (CSharp):
    1. #if ENABLE_IL2CPP
    2. [AOT.MonoPInvokeCallback(typeof(...))]
    3. #endif
    We used to simply guard with IOS and ANDROID platform checks, but want to add IL2CPP support for PC and MAC as well.
    Code (CSharp):
    1. #if UNITY_ANDROID || UNITY_IOS
    2.             [AOT.MonoPInvokeCallback(typeof(...))]
    3. #endif
     
    Last edited: Aug 31, 2023
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    The defines are the way to go in this case, that's what they're here for.

    If both of them are defined then that's a bug. Try updating to the latest applicable Unity version and report a bug if the problem persists.

    Just to make sure: You tested in a Windows build that both
    ENABLE_MONO
    and
    ENABLE_IL2CPP
    are set? And you don't accidentally have them defined in player settings? Note that the editor always runs on Mono,
    ENABLE_IL2CPP
    is only set in builds.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    You have used something like that in your code, right?
    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4. Debug.Log("Testing backend ...");
    5. #if ENABLE_IL2CPP
    6.     Debug.Log("Using IL2CPP backend");
    7. #endif
    8. #if ENABLE_MONO
    9.     Debug.Log("Using Mono backend");
    10. #endif
    11. #if !ENABLE_MONO && !ENABLE_IL2CPP
    12.     Debug.Log("You should never see this!!!!");
    13. #endif
    14.     Debug.Log("...done");
    15. }
    16.  
    put that on an object in your project and check the console / log file of your project. Like Adrian said, when you test inside the editor, you should see that Mono is used. When you create a windows standalone build, it should use IL2CPP (assuming you actually choosen that scripting backend).

    So given this testing code, you claim that you see 4 debug logs that look like this?
    Code (CSharp):
    1. "Testing backend ..."
    2. "Using IL2CPP backend"
    3. "Using Mono backend"
    4. "...done"
    5.  
    I would also doubt that. Though if you do, it would indeed be a bug.
     
  4. fdart

    fdart

    Joined:
    Jan 4, 2019
    Posts:
    17
    Thanks for your replies and confirming my suspicion. I will have to test this further when I get more time. This likely won't be for a while but I will follow up.