Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by virtueelbouwen · Feb 05, 2018 at 09:54 PM · visualstudiolibrariespreprocessorc# dll

How to access Unity's preprocessor directives from a custom C# class library project?

Hi guys, I've got a Project in Visual Studio that builds it's own dll (standard C# class Library) added to a unity solution (because we can these days). Now I want to use unity's built-in preprocessor directives: these ones Preprocessor Directives. The only issue is they aren't defined in my C# project even if I reference a Unity project.

The goal here is to insert the dll into a unity project and be able to compile parts of the code for specific versions of Unity.

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
0

Answer by Bunny83 · Feb 05, 2018 at 10:44 PM

Pre processor directives as the name suggests are interpreted by the preprocessor of the compiler. Therefore once the code is compiled they do not exist anymore. The pre processor literally removes things that are in a conditional code block from the source files before they are compiled. Since you compiled your DLL already there is no way to change anything in the code. You would need to compile seperate assemblies for the different versions you want to support. You have to set the defines manually in your Visual Studio project to control which version you are compiling.

Comment
Add comment · 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
0

Answer by Glurth · Feb 06, 2018 at 03:43 AM

You can use the Conditional attribute. https://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute(v=vs.110).aspx
e.g.

 [Conditional("CONDITION1")]
 public static void Method1(int x){..condition1 specific stuff..}

If the pragma, CONDITION1, is defined, the function will be called, if not, it will be skipped. (Careful, even it's parameters will NOT be evaluated)

It looks like, this even works in a class library DLL! The attribute's pragma is "applied/evaluated" when compiling the CLIENT executable, NOT your class library dll. https://stackoverflow.com/questions/26634744/conditional-compilation-in-referenced-assemblies

So, you can write a function for each version, applying to each, a conditional attribute with its own unique "Version" pragma. Then, in your public/primary function, call them all: one will actually execute (assuming you have (only) one of the unique pragmas defined in the client project).

Comment
Add comment · 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

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

77 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 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 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 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

how can i know about the libraries of c# in unity 1 Answer

Linux server .so files not loaded 0 Answers

System.Globalization.CCEastAsianLunisolarCalendar.GetMonth (DateTime date) 2 Answers

TypeLoadException with libraries in C# 1 Answer

Visual Studio alternatives for Mac 3 Answers


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