Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Request preprocessor directive for unsafe code

Discussion in '2018.1 Beta' started by amarcolina, Mar 13, 2018.

  1. amarcolina

    amarcolina

    Joined:
    Jun 19, 2014
    Posts:
    65
    In the current beta, unsafe code is a nice friendly toggle that can be checked or unchecked in the player settings. This is definitely an improvement, and is very convenient compared to some of the previously required workarounds. I'd request an additional feature on top of this new one, that when unsafe code is enabled, that Unity define a preprocessor directive such as UNSAFE_CODE_ENABLED.

    Benefits:
    - An asset that needs unsafe code could replace all of their unsafe routines with stubs that throw exceptions notifying the user that they need to turn unsafe code on. They could also use the #error directive to provide a single compile time message notifying the user of the need to enable unsafe code, instead of the many dozens of errors they might get otherwise.
    - An asset that wants unsafe code, but doesn't need it, could use the directive to automatically switch between the optimized unsafe variant and the less optimized but safe variant. Could allow an asset to work out of the box without the user needing to know about unsafe code (but still allow unsafe code to be enabled for greater performance). Could also allow an asset to automatically work for platforms that don't support unsafe code, while still using unsafe code for platforms that do.