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
1
Question by WillNode · May 11, 2015 at 09:37 PM · c#editorcomponent

Create a Component Script for Editor Only

Hi guys, I'm working on some editor stuff right now.

so, I'm trying to create a component script that can be attached to any game objects in a scene, but this component are just for managing editor stuff, so when I'm build my game, this script will not included in my game.

how I do that? trying to tag the Game Object to EditorOnly just make the whole script including game object not included in build, also trying to move my component script to Editor folder just make it disappear from add component menu.

Comment
Add comment · Show 2
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 AntonyBaasan · May 11, 2015 at 09:50 PM 1
Share

You could remove the component inside Awake() or Start() methods.

avatar image WillNode · May 11, 2015 at 09:55 PM 0
Share

hmm, seems working, but I've already mark my script as ExecuteInEdit$$anonymous$$ode. Don't it will also removed automatically when I'm starting Unity?

1 Reply

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

Answer by maccabbe · May 11, 2015 at 10:01 PM

You could try using directives. For instance Application.Quit() did not work for Unity Editor so I added code inside a directive that did work in Unity Editor

 Application.Quit();
 #if UNITY_EDITOR
     UnityEditor.EditorApplication.isPlaying=false;
 #endif

https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

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 WillNode · May 11, 2015 at 10:25 PM 0
Share

hmmm, not the best one, but well this seems making sense. combined with Antony comment, my script will look like this :

 using UnityEngine;
 #if UNITY_EDITOR
   using UnityEditor;
   [ExecuteInEdit$$anonymous$$ode]
 #endif
 public class EditorData : $$anonymous$$onoBehaviour{
   void Awake(){
     #if !UNITY_EDITOR
       Destroy(this);
     #else
       //the rest of code
     #endif
   }
   #if UNITY_EDITOR
     //the rest of editor functions
   #endif
 }

thanks! never $$anonymous$$ding why not use directives ins$$anonymous$$d.

avatar image FortisVenaliter · May 11, 2015 at 10:49 PM 1
Share

If you want to make that a bit more readable, I suggest having a base class with shared functionality, then deriving it into "editor-only" and "game-only" subclasses. Then you can use the directive once to instantiate the correct one, and the code will be much more readable and easy to maintain.

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

20 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

Related Questions

RequireComponent at variable level? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Get if any component in gameObject has changed 1 Answer

Distribute terrain in zones 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