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 /
avatar image
5
Question by achynes · May 21, 2016 at 03:08 PM · editorfilecustom editorname

Is it bad practice to put a custom Editor class in the same file as the Monobehaviour it affects?

So the documentation says to put your Editor scripts into an Editor folder to stop them compiling at runtime.

However, if I place the MonoBehaviour and Editor classes into a single file with the name of the MonoBehaviour for the file name, it seems to work anyway (and not when in an Editor folder). This reduces clutter for small scripts that you want to give custom inspectors, but is it bad practice?

 using UnityEngine;
 using UnityEditor;
 
 public class Example : MonoBehaviour {
 // Stuff here
 }

 [CustomEditor(typeof(Example))]
 public class ExampleEditor : Editor {
 // Stuff here
 }
Comment
Add comment · Show 5
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 FortisVenaliter · May 21, 2016 at 03:08 PM 0
Share

That shouldn't work unless the whole file is in the Editor folder, in which case it won't be included in your built game...

avatar image achynes FortisVenaliter · May 21, 2016 at 03:26 PM 0
Share

The file isn't in an Editor folder, but its Editor part does work.

avatar image flashframe · May 21, 2016 at 03:36 PM 0
Share

If you don't want to break out your editor class or editor code into a separate script, you can also use preprocessor directives to control what gets compiled into your game. In your case, wrapping the custom editor class in

 #if UNITY_EDITOR
 //editor Code goes here
 #endif

Would prevent your editor code from compiling into the build of your game.

avatar image achynes flashframe · May 21, 2016 at 03:56 PM 0
Share

Ah yes, never thought of that. :) Thanks!

avatar image Suzuka91 · Apr 04, 2017 at 11:11 AM 0
Share

I was thinking the same as achynes. I find more confortable to add the custom editor on the same file.

Anybody know if it has some disadvantages?

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by PedroFabri · Sep 09, 2020 at 01:33 PM

(A bit late, but was searching the same thing and would like to help someone here)

In my opinion, as long as it's not too complex, it's not a bad practice. But if your editor is a really big file, things can get messy.

JUST REMEMBER: If you put your Editor script inside the same file, remember to check if you're in the editor first. Otherwise your game will not build, because de UnityEditor namespace is not added in builds.

Just do something like this:

 // Import your regular stuff
 using UnityEngine;
 using UnityEngine.SceneManagement;
 // using blábláblá
 
 // Only import UnityEditor if we're in the editor.
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class MyClass : MonoBehaviour
 {
     // Cool stuff here 
 }
 
 // Again, class will be declared only if we're in the editor.
 #if UNITY_EDITOR
 [CustomEditor(typeof(MyClass))]
 public class MyClassEditor : Editor
 {
    // Cool editor stuff here
 }
 #endif

Sad to answer a 4 y.o. question, but hope it helps someone!

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

8 People are following this question.

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

Related Questions

File Name And Class Name Dont Match 0 Answers

Custom Editor loosing data on play 1 Answer

Why UnityEngine.UI.Text color is not changed right away in the Editor? 1 Answer

Slideable Float Field in Custom Editor 3 Answers

Can a class's Editor call its component's Editor? Likewise, can a derived class's Editor call its base class's Editor? 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