Search Unity

How do we use Assembly Definition Files with CustomEditor drawers?

Discussion in 'Editor & General Support' started by CanisLupus, Aug 13, 2018.

  1. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    TL;DR for this thread: don't place weird chars (in our case it was [, ]) in the internal names of your assembly definition files!

    -------------------------------------------------------------

    In our project there is a script called AwesomeToggle, which has a custom drawer, AwesomeToggleDrawer, defined as:
    Code (CSharp):
    1. [CustomEditor(typeof(AwesomeToggle), true)]
    2. [CanEditMultipleObjects]
    3. public class AwesomeToggleDrawer : SelectableEditor
    AwesomeToggle is in an assembly defined by an AwesomeToggle.asmdef file. However, we can't seem to find a way to place the AwesomeToggleDrawer file in an assembly in a way that works. The drawer can never load the AwesomeToggle script.
    Since the drawer must be in an editor assembly, this doesn't work (AwesomeToggle.asmdef is not an editor assembly):
    The Editor folder is not a "magic" folder if inside an assembly, so this doesn't work either:
    Creating another assembly inside the Editor folder, just for the drawer, doesn't work either (NOTE: it has the reference for AwesomeToggle.asmdef):
    Moving the drawer to another folder entirely doesn't work either:
    We're at a loss as to how this could be done. Any pointers?

    Thanks!
    Daniel
     
    Last edited: Sep 5, 2018
  2. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    We'd really like some help with this, if anyone knows.

    Thanks!
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Code (csharp):
    1.  
    2. Folder
    3. --- AwesomeToggle.asmdef
    4. --- AwesomeToggle.cs
    5. --- Editor
    6. ------ AwesomeToggleDrawer.asmdef
    7. ------ AwesomeToggleDrawer.cs
    8.  
    We do this for dozens of attributes, and it works. The TypeLoadException is really strange - sounds like your AwesomeToggleDrawer is targeting an outdated version of AwesomeToggle or something?

    There has been some issues with assembly definition files not quite occasionally managing to load in the correct order - haven't managed to track down a repro case. You could try to reimport all scripts (search for t:script, select all, hit reimport), which forces all the assemblies to reload.
     
    meikellp and karl_jones like this.
  4. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    Hi @Baste, thanks for your reply.

    I apparently forgot to give a piece of info before. Creating that folder structure does not result in a TypeLoadException, but it does result in this error when selecting an object with an AwesomeToggle component:
    So it compiles, but the drawer does not work. Have you encountered this problem? AwesomeToggleDrawer inherits from ScriptableObject indirectly through SelectableEditor, but it can't be in the Assets/Editor folder, since it's part of a different assembly. Maybe there's something else wrong.

    Daniel
     
  5. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    I've tried reimporting all scripts like you said, and it worked, so that's awesome. :) Thanks a lot!

    I can try to see if this happens in a new project and reproduce it, but since it fixes itself after reimporting, I'm not sure.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    The "and be placed in the Assets/Editor" folder info is outdated - it should be "in an Editor assembly". Seems like Unity still thought your AwesomeToggleDrawer.asmdef wasn't marked as an editor assembly.
     
  7. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    Now I've tried adding assemblies throughout our project again (I couldn't do that until the error with the drawer was fixed) and the TypeLoadException is back (see image):
    This also occurs:
    Which completely destroys the inspector (becomes empty and glitched out). Commenting out the code in the drawer seems to "fix" this (i.e. all errors stop, project compiles, and only the drawer stops working because it's commented out).

    The difference is that AwesomeToggle is now in an assembly with other stuff instead of being the only script there. Maybe something else is different? The drawer causes the problem either in its own assembly or in another editor assembly that contains other stuff (either case references the assembly where AwesomeToggle is in).

    I really need to cause this problem in a smaller project. :) I simply can't understand this as it is.

    Will report back if I find anything.
     

    Attached Files:

  8. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    OK, yesterday I figured out what was going and submitted a bug report. This was a bit specific...

    I created all our assembly definition files in our project as names surrounded with square brackets, so something like "[GlobalScripts].asmdef". This made them be at the top of their folder. The internal name automatically gets the same name. Everything including assembly references works well with this, so I didn't think it a problem.

    However... for some reason it seems that the CustomEditor/drawer does not deal well with locating classes in assemblies with weird chars in it. The internal assembly name can't have [, ], {, }, (, ), ê, etc if we expect it to find the class it's supposed to draw (but the name of the file can be anything). I suppose this could have something to do with string matching in Unity's reflection-like methods, but I don't know.

    Either way, I now know to remove weird chars from internal assembly names.

    Daniel
     
    Wolfram and Pecek like this.
  9. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    I don't even know how to find the right words to thank you! I've just had some assembly/ definition/editor extension problems I've been trying to solve for a good 4 hours and was getting nowhere until I happened upon your post.
    Really was stumped because it worked in other projects and even with similar structures within the same project and I just couldn't figure out what the problem was.

    As is often the case with such things, the solution was so simple and a matter of mere seconds.

    Thank you so, so, so much! This solved all my problems.
     
  10. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    For the sake of completeness of this thread, my bug report had this response from Unity (tldr: won't fix):

    I was disappointed by the generic response to a problem that was hard to pinpoint and report. I guess it doesn't affect a lot of people since many don't use asmdef files but it's still not obvious at all when it happens.
     
    Arthur-LVGameDev and Wolfram like this.
  11. airnamics

    airnamics

    Joined:
    Feb 5, 2020
    Posts:
    28
    Hi, I know this is old thread, but this really is an issue - why isn't Editor folder behaving as it always is? I have like 15 Editor folders now, and have to link everything manually lol.
     
  12. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Because its an ancient workaround from the times when asmdefs were not available.
    By default Editor folder just compiles code to a separate editor assembly.

    With asmdefs you're deciding what should be compiled where. A bit annoying, but highly customizable, so its worth it.
    My advice - make a single editor assembly for your code instead of 15 ones unless its specifically designed to be decoupled.

    What would be nice though is automatic exclude everything except editor platform in cases where asmdefs are placed inside Editor folder.
     
  13. airnamics

    airnamics

    Joined:
    Feb 5, 2020
    Posts:
    28
    There should exist simple switch for Special folders like Editor, such folders could get autogenerated .asmdef and should inherit parent .asmdef folders properties/references and just run inside Editor.

    Shouldn't be too hard to make, no :p?
     
  14. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Its probably somewhere at the bottom of endless FR list.
     
  15. airnamics

    airnamics

    Joined:
    Feb 5, 2020
    Posts:
    28
    Btw I agree, I love .asmdef, and did managed to finish this thing. Maybe it is right that it is at the bottom of FR list.
     
  16. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    As a point of clarification, when you say an "editor assembly", do you mean an assembly that is set up something like this?
     

    Attached Files:

  17. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Yes! But the thread is 2 years old so idk what I was answering.
     
    DSivtsov and sand_lantern like this.