Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by oddeven · Jul 25, 2013 at 01:22 AM · editorpreprocessorbuildsettings

Set preprocessor symbols from Editor script

I'm working on a very simple build utility and I want to programmatically add/remove preprocessor symbols before builds.

For example, if I have Build Android-Release button, I want to programmatically add a RELEASE preprocessor to the build settings before I start the build. Or if I wanted to make a device-specific build for OUYA, I could add an appropriate symbol instead of managing that manually in the Editor.

I have found EditorUserBuildSettings.activeScriptCompilationDefines but is read-only. Any suggestions?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
17
Best Answer

Answer by Brian@Artific · Jul 25, 2013 at 04:41 AM

Set your preprocessor definitions in: Player Settings -> Per-platform Settings -> Other Settings -> Configuration -> Scripting Define Symbols.

EDIT: If you have multiple definitions, separate with a semicolon.

EDIT: From an editor script, use PlayerSettings.SetScriptingDefineSymbolsForGroup to set these same values programmatically, which is what I would have said if I had exercised the slightest bit of reading comprehension the first time. (apologies)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oddeven · Jul 25, 2013 at 05:21 AM 0
Share

I'm looking for a way to do that in an Editor script ;)

avatar image Benproductions1 · Jul 25, 2013 at 05:38 AM 0
Share

@$$anonymous$$@Floppy That doesn't answer the question. Please at least read the title before answering :)

avatar image Brian@Artific · Jul 25, 2013 at 02:09 PM 0
Share

Ugh ... quite right - no more insomnia-fueled answers for me. :(

See my edit above for an answer to your actual question rather than random semi-related facts.

avatar image oddeven · Jul 25, 2013 at 07:31 PM 0
Share

Works perfectly, thanks!

avatar image
7

Answer by hasanbayat · Oct 21, 2017 at 12:02 PM

You can use AddDefineSymbols to add define symbols automatically.

Features

  • Multiple Define Symbols

  • Safety

  • Runs when Compile ends

  • Removes Duplicates

Installation

  1. Download the Script or Copy/Paste it from the Below

  2. Open Script

  3. Go to Symbols property and add your own symbols

  4. Go back to Unity and wait for compile ends

  5. All done, now check Player Settings, The symbols added

Here is the code for copy/pasting:

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEditor;
  
 /// <summary>
 /// Adds the given define symbols to PlayerSettings define symbols.
 /// Just add your own define symbols to the Symbols property at the below.
 /// </summary>
 [InitializeOnLoad]
 public class AddDefineSymbols : Editor
 {
  
     /// <summary>
     /// Symbols that will be added to the editor
     /// </summary>
     public static readonly string [] Symbols = new string[] {
         "MYCOMPANY",
         "MYCOMPANY_MYPACKAGE"
     };
  
     /// <summary>
     /// Add define symbols as soon as Unity gets done compiling.
     /// </summary>
     static AddDefineSymbols ()
     {
         string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup ( EditorUserBuildSettings.selectedBuildTargetGroup );
         List<string> allDefines = definesString.Split ( ';' ).ToList ();
         allDefines.AddRange ( Symbols.Except ( allDefines ) );
         PlayerSettings.SetScriptingDefineSymbolsForGroup (
             EditorUserBuildSettings.selectedBuildTargetGroup,
             string.Join ( ";", allDefines.ToArray () ) );
     }
  
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image evilangel89 · Dec 31, 2017 at 05:03 AM 0
Share

Great answer! Deserves more credit and I would flag it as the more correct answer as it's safer and complete.

avatar image xari2k · Jul 04, 2018 at 05:46 AM 1
Share

Great solution. I added your code as a private class in my custom editor script.

         /// <summary>
         /// Adds the given define symbols to PlayerSettings define symbols.
         /// Just add your own define symbols to the Symbols property at the below.
         /// </summary>
         private static class AddDefineSymbols
         {
             /// <summary>
             /// Symbols that will be added to the editor
             /// </summary>
             private static List<string> Symbols = new List<string>();
 
             /// <summary>
             /// Add define symbols as soon as Unity gets done compiling.
             /// </summary>
             public static void Add(string define)
             {
                 Symbols.Add(define);
                 string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
                 List<string> allDefines = definesString.Split(';').ToList();
                 allDefines.AddRange(Symbols.Except(allDefines));
                 PlayerSettings.SetScriptingDefineSymbolsForGroup(
                     EditorUserBuildSettings.selectedBuildTargetGroup,
                     string.Join(";", allDefines.ToArray()));
             }
 
             /// <summary>
             /// Clear all symbols.
             /// </summary>
             public static void Clear()
             {
                 Symbols.Clear();
             }
         }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

plugins mac bundle architectures 0 Answers

BuildPlayer says successful but there is no .app to run 0 Answers

Custom icons for files on Project Window 0 Answers

Instance of [Editor] couldn't be created because there is no script with that name. 1 Answer

How to load a scene from disk in Standalone player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges