Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

When to write editor scripts, and when just scripts?

Discussion in 'General Discussion' started by ChrisSch, Oct 8, 2014.

  1. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Yeah that. I'm confused, when should I write editor scripts and when simple scripts? Whats the benefit? Is it better to write editor scripts for some small systems like a player controller or gun script, or something like that?

    I'm mainly asking because I wanna make some for the asset store. I made one small Teleporter Pad script, as a simple script, but do you think I should have made it as an editor script? It has almost 300 downloads, 4 five star ratings, and whatever rating that dropped it to 4 stars. Guess someone didn't know how to read the Readme file included. xD

    So to repeat my confused question, editor script or script? For controller systems or something, if you need a specific. Or is that not how you decide which to write at all?
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Simple systems & single scripts can use the Inspector. For larger systems with multiple scripts to get a user up and running and Editor Interface may be desired. I like them when I have to set vars and cannot see the results till I hit Play. If I can see the reaction of a variable change in the Editor then that makes my task as dev or designer easier to cope with and swifter to come to a conclusion about what should be used in context.
     
    BeefSupreme, ChrisSch and Ryiah like this.
  3. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Yes and make sure you keep that editor organized. I hate it when scripts have this disgusting out of order editor
     
    ChrisSch likes this.
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    I don't understand the question. They're two different things for two different situations - it's an apples and oranges comparison.
     
    ChrisSch and angrypenguin like this.
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Apples and oranges both can belong in the fruit bowl. I think his question as I understood it is when do you think an Editor script should be hacked together to support the game script/s.
     
    ChrisSch likes this.
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Ah, so "game script" vs "game script + accompanying editor script"? That makes more sense.

    I used to write lots of editor scripts. These days, I think I'd first look to see what I could do with PropertyDrawers and with the ContextMenuAttribute before jumping straight to a full editor - PropertyDrawers are more reusable and mean less boilerplate to write.
     
  7. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    That's the question! :D How are they different, and for what different situations? I started watching the tutorial but got confused because you can do that with normal scripts too.
     
  8. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Do what with 'normal scripts' too?

    Editor scripts aren't included in your game when you do a build. Also I believe that attempts to use the UnityEditor namespace in normal scripts will cause your builds to fail (though it works in the editor).
     
    ChrisSch likes this.
  9. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Ah, so editor scripts serve to help existing game scripts, or/and do work inside the editor? I'm sorry if my questions aren't making sense, I got really sick and I'm all dizzy, feverish, hurty, and confused. xD
     
  10. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Yes. You might write an editor script to do things like:
    • Select all enemies in your scene that have negative health.
    • Automatically exclude certain 'test' objects in your scene when doing a build.
    • Make a particular float variable show up as a slider in a fixed range, rather than just a freely editable number.
    • Create instances of a custom ScriptableObject type and bind them to .asset files in your project.
    • Make a smarter Inspector for an enemy which hides the 'health' variable if the 'invincible' checkbox is ticked
    • Make a simple tool for placing enemies where you turn it on and then just click once in the scene to set up an enemy at the point you clicked on.
     
    ChrisSch, randomperson42 and Ryiah like this.
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Editor scripts only work in the editor; they literally don't even exist in the final game. If something is only useful in the editor and doesn't need to run in the actual game, then it should be an editor script. If it is something that will run during the game, then it can't be an editor script.
     
    ChrisSch likes this.
  12. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    It sounds like maybe you're talking about writing a "Custom Inspector", which is a script that lets you change what the inspector looks like for a script. A Custom Inspector is a type of Editor script, but not all Editor scripts are custom inspectors.
     
    ChrisSch likes this.
  13. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Thanks guys! I understand it better now. I was confused cause I had no idea the editor scripts don't run at all in the game, but now that I know that, it makes way more sense. xD
     
  14. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Thanks again everyone for clearing this up to me, editor scripting is actually pretty fun. Its like filling in little gaps Unity has or adding functions to it. Don't know if clear parent exists built-in but it was fun learning experience anyway.

    As my first tools I made:
    1. Clear Parent
    2. Create empty and parent selection to it
    3. Clear, create, and parent selection to it

    second and third tool.png

    I use those three things a loooot, especially the last one, so its incredibly useful to me and speeds up making assets.