Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KamikazeBacon · Oct 20, 2015 at 02:34 AM · inspectoroptionsconditional

Preprocessor conditional code for cleaner inspector options

I started learning Unity this past weekend, and I'm having similar problems with preprocessor conditionals. I want to clear up clutter in the inspector by only providing declarations and access only to relevant variables. That way, I can increase the re-usability of my scripts while avoiding huge if statements. The following code returns a "Invalid preprocessor directive" and a "Single-line comment or end-of-line expected" on the lines following the #if and #elif directives. using UnityEngine; using System.Collections;

  public class GunController : MonoBehaviour
  {
      public enum GunType { None, Projectile, Laser, Plasma }; // Behavior labels
      public GunType type = GunType.Laser; // Selectable via drop-down menu
  
      #if (type == GunType.Laser)
      float power = 0; // Not relevant for a mass driver
  
      #elif (type == GunType.Projectile)
      float velocity = 0; // Not relevant for a laser
  
      #else
      // Other conditions follow
  
      #endif
  }

Is it not possible to use directives within a class? Am I asking for too much? My alternative is just to make separate classes for each type of weapon. Also, is this even the right approach? This would only work if the scripts are compiled on a per-use basis, which I don't understand if they are or not. Do you have alternatives?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by laurentlavigne · Dec 19, 2015 at 10:38 AM

I thought that U5 introduced an attribute [ShowIf(bolean)] that does what you're looking for but I cannot find it. Until it magically re-appears, the official solution is this: http://docs.unity3d.com/Manual/editor-PropertyDrawers.html

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

33 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

Related Questions

Why does rotating via viewport give different values than inspector rotations? 2 Answers

How do I get a List or array in a custom inspector? 1 Answer

Multiply script of same type in single game object, how to choose in inspector 0 Answers

How to drag a specific component reference from one object's Inspector view to another one? 1 Answer

Inspector Euler angles deviates from Euler angles in code 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