{"id":14960696,"url":"https://github.com/alkimeegames/codeoptimizationsymbols","last_synced_at":"2025-10-24T19:30:23.896Z","repository":{"id":240679291,"uuid":"355196685","full_name":"AlkimeeGames/CodeOptimizationSymbols","owner":"AlkimeeGames","description":"Automatically sets preprocessor directives based on the Code Optimization mode set in the Unity Editor","archived":false,"fork":false,"pushed_at":"2021-04-12T17:22:58.000Z","size":30,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2024-09-23T11:03:40.383Z","etag":null,"topics":["code-generation","compiler","openupm","symbols","unity","unity3d","unity3d-editor","unity3d-plugin"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlkimeeGames.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.paypal.com/paypalme/Alkimee/","https://www.buymeacoffee.com/Alkimee"]}},"created_at":"2021-04-06T13:22:33.000Z","updated_at":"2024-05-25T15:10:09.000Z","dependencies_parsed_at":"2024-05-20T15:56:01.307Z","dependency_job_id":null,"html_url":"https://github.com/AlkimeeGames/CodeOptimizationSymbols","commit_stats":null,"previous_names":["alkimeegames/codeoptimizationsymbols"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlkimeeGames%2FCodeOptimizationSymbols","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlkimeeGames%2FCodeOptimizationSymbols/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlkimeeGames%2FCodeOptimizationSymbols/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlkimeeGames%2FCodeOptimizationSymbols/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlkimeeGames","download_url":"https://codeload.github.com/AlkimeeGames/CodeOptimizationSymbols/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219867815,"owners_count":16554365,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","compiler","openupm","symbols","unity","unity3d","unity3d-editor","unity3d-plugin"],"created_at":"2024-09-24T13:22:46.783Z","updated_at":"2025-10-24T19:30:16.583Z","avatar_url":"https://github.com/AlkimeeGames.png","language":"C#","funding_links":["https://www.paypal.com/paypalme/Alkimee/","https://www.buymeacoffee.com/Alkimee"],"categories":[],"sub_categories":[],"readme":"# Code Optimization Symbols\n\n[![OpenUPM](https://img.shields.io/npm/v/com.alkimeegames.codeoptimizationsymbols?label=OpenUPM\u0026registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.alkimeegames.codeoptimizationsymbols/)\n[![GitHub Release Date](https://img.shields.io/github/release-date/AlkimeeGames/CodeOptimizationSymbols)](https://github.com/AlkimeeGames/CodeOptimizationSymbols/releases)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg)](http://makeapullrequest.com)\n[![Unity 2018.4 or Layer](https://img.shields.io/badge/unity-2020.1%20or%20later-green.svg)](https://unity3d.com/unity/qa/lts-releases)\n[![MIT License](https://img.shields.io/github/license/AlkimeeGames/CodeOptimizationSymbols)](https://github.com/AlkimeeGames/CodeO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TMtMzAtOS01LTMtOS05LTEzLTE2YTgxIDgxIDAgMDEtNi0zMiA5NCA5NCAwIDAxOC0zNSA5MCA5MCAwIDAxNi0xMmwxLTJjNS05IDEzLTEzIDIzLTE2IDE2LTUgMzItMyA1MCA5IDEzIDggMjMgMjAgMzAgMzYgNyAxNSA3IDI5IDAgNDJ6bS00My03M2MtMTctOC0zMy02LTQ2IDUtMTAgOC0xNiAyMC0xOSAzN2E1NCA1NCAwIDAwNSAzNGM3IDE1IDIwIDIzIDM3IDIyIDIyLTEgMzgtOSA0OC0yNGE0MSA0MSAwIDAwOC0yNCA0MyA0MyAwIDAwLTEtMTJjLTYtMTgtMTYtMzEtMzItMzh6bS0yMyA5MWgtMWMtNyAwLTE0LTItMjEtN2EyNyAyNyAwIDAxLTEwLTEzIDU3IDU3IDAgMDEtNC0yMCA2MyA2MyAwIDAxNi0yNWM1LTEyIDEyLTE5IDI0LTIxIDktMyAxOC0yIDI3IDIgMTQgNiAyMyAxOCAyNyAzM3MtMiAzMS0xNiA0MGMtMTEgOC0yMSAxMS0zMiAxMXptMS0zNHYxNGgtOFY2OGg4djI4bDEwLTEwaDExbC0xNCAxNSAxNyAxOEg5NnoiLz48L3N2Zz4K)](https://github.com/AlkimeeGames/CodeOptimizationSymbols/blob/develop/CHANGELOG.md)\n[![GitHub Org's Stars](https://img.shields.io/github/stars/alkimeegames?style=social)](https://github.com/AlkimeeGames)\n\n\u003e **Automatically** sets preprocessor directives based on the Code Optimization mode set in the Unity Editor.\n\n## What are Code Optimization Symbols?\n\nThe Code Optimization Symbols tool automatically creates [Compiler Symbols](https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) based on the Unity\nEditor's [Code Optimization](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) mode. These compiler symbols can then be used in your code to change behaviour based on the\ncurrently active Code Optimization mode.\n\n## Why use the Code Optimization Symbols?\n\nHaving compiler symbols for both the Debug and Release Code Optimization modes allows you to write code that is only available when either of these modes are available.\n\nFor example, you may have a class which writes logs to the Unity Console when actions occur. You could use the pre-existing 'UNITY_EDITOR' symbol which would result in logs being\nsent to the console whenever you're running the game in the Editor. However, if you don't need these logs all the time and only need them whilst debugging, surrounding the code\nwith the\n'UNITY_EDITOR_DEBUG' symbol will make those logs *only* appear when the Editor is in Debug Code Optimization mode.\n\nNaturally, the symbols set by this tool can be used for more than just logging. You could also instantiate entirely difference classes based on the Code Optimization mode for\nperformance reasons.\n\n## Whats wrong with #if UNITY_EDITOR et al?\n\nThe [Platform Dependant Compilation](https://docs.unity3d.com/Manual/PlatformDependentCompilation.html) symbols already provided by the Unity Editor are excellent for targeting\nspecific platforms or code you only want to run in the Editor. However, it provides no means for determining which Code Optimization mode the Unity Editor is currently in. This\nmeans you currently cannot have code run in the Editor when only in the Debug Code Optimization mode.\n\n## Setup\n\nThe Code Optimization Symbols tool works out of the box, automatically and in the background with no dependencies other than Unity itself. It's pure CSharp and the public API has\ncomplete XML documentation.\n\n## How does this work?\n\nThe Code Optimization Symbols tool adds two new compiler symbols determined by which Code Optimization mode the Editor is in. Because of this, they can never both be set at the\nsame time.\n\n- Debug Mode introduces the \"UNITY_EDITOR_DEBUG\" symbol.\n- Release Mode introduces the \"UNITY_EDITOR_RELEASE\" symbol.\n\n## Automatic Behaviour\n\n- When building your project the symbols will be automatically removed and re-added once the build is complete.\n- Switching platforms via the [Build Settings](https://docs.unity3d.com/Manual/BuildSettings.html) window or via script will automatically remove the symbols from the previous\n  platform and add them to the now active platform.\n- Exiting the Editor will remove either of the symbols.\n\n## Example Usages\n\n### As a [preprocessor directive](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives)\n\n```c#\npublic sealed class Bullet : MonoBehaviour\n{\n    private void OnCollisionEnter([Collision other)\n    {\n        if (other.gameObject.CompareTag(\"Player\")) {\n\n#if UNITY_EDITOR_DEBUG\n            // You will only see this log in the Editor AND if the Editor is in Debug Code Optimization mode.\n            Debug.Log(\"Destroying \" + other.name);\n#endif\n\n            Destroy(other.gameObject);\n        }\n    }\n }\n```\n\n### Using the [Conditional Attribute](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=netstandard-2.0)\n\n```c#\nusing System.Diagnostics;\nusing static AlkimeeGames.CodeOptimizationSymbols.Symbol;\n\npublic sealed class PlayerState\n{\n    private void PerformingAction()\n    {\n        Log(\"I performed an action.\");\n    }\n\n    [Conditional(CodeOptimizationDebug)]\n    private void Log(string @string)\n    {\n        Debug.Log(@string)\n    }\n}\n```\n\nUsing the Conditional Attribute with [Code Stripping](https://docs.unity3d.com/2020.1/Documentation/Manual/ManagedCodeStripping.html) disabled or set to Low will result in the call\nsite to the method being removed from the build. With Code Stripping set to Medium or High, the call site *and* the method definition will be removed from the build (except in the\ncase of classes deriving from MonoBehaviour or ScriptableObject where Unity tends to be more pessimistic in stripping code from those derived classes).\n\n## API and Extensibility\n\nThere are two public events available to subscribe to from your own Editor scripts.\n\n- SymbolSetter.SettingSymbols\n    - Raised *before* setting the symbols.\n- SymbolSetter.SetSymbols\n    - Raised *after* setting the symbols.\n\nBoth pass an ISet\u003cstring\u003e to the subscribed method which contains *all* the symbols being applied.\n\nYou can also access the names of the symbols directly in your code.\n\n- Symbol.CodeOptimizationDebug\n- Symbol.CodeOptimizationRelease\n\n## Installation\n\n### Install via OpenUPM\n\nThe package is available on the [openupm registry](https://openupm.com/packages/com.alkimeegames.codeoptimizationsymbols/). It's recommended to install it\nvia [openupm-cli](https://github.com/openupm/openupm-cli).\n\n```\nopenupm add com.alkimeegames.codeoptimizationsymbols\n```\n\n### Install Via Package Manager\n\nVia the Package Manager, you can add the package as a Git dependency. Follow the instructions for\n[Installing froma Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html) and further information around\n[Git dependencies](https://docs.unity3d.com/Manual/upm-git.html) in Unity. We advise specifically locking to the\n[main](https://github.com/AlkimeeGames/CodeOptimizationSymbols/tree/main) branch.\n\n### Install Via Manifest.json\n\nOpen *Packages/manifest.json* with your favorite text editor. Add the following line to the dependencies block.\n\n    {\n        \"dependencies\": {\n            \"com.alkimeegames.codeoptimizationsymbols\": \"https://github.com/AlkimeeGames/CodeOptimizationSymbols.git#main\"\n        }\n    }\n\n#### Git Updates\n\nThe Unity Package Manager records the current commit to a lock entry of the *manifest.json*. To update to the latest version, change the hash value manually or remove the lock\nentry to re-resolve the package.\n\n    \"lock\": {\n      \"com.alkimeegames.codeoptimizationsymbols\": {\n        \"revision\": \"main\",\n        \"hash\": \"...\"\n      }\n    }","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falkimeegames%2Fcodeoptimizationsymbols","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falkimeegames%2Fcodeoptimizationsymbols","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falkimeegames%2Fcodeoptimizationsymbols/lists"}