Search Unity

Bug Unity 2020.2 broke "inspector in the same file as asset" code pattern

Discussion in 'Scripting' started by Dmitry-Pyalov, Jan 16, 2021.

  1. Dmitry-Pyalov

    Dmitry-Pyalov

    Joined:
    Dec 13, 2011
    Posts:
    125
    There is a common pattern when you define Inspector in the same file as the asset class which looks like this
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace MyGame
    4. {
    5.     public class MyAsset: ScriptableObject
    6.     {
    7.     }
    8.    
    9. #define UNITY_EDITOR
    10.     namespace Editor
    11.     {
    12.         using UnityEditor;
    13.    
    14.         [CustomEditor(typeof(MyAsset))]
    15.         public class MyAssetInspector : UnityEditor.Editor
    16.         {
    17.         }
    18.     }
    19. #endif
    20. }
    I've updated our game from 2019.4 to 2020.2 and now all the assets made with such pattern are broken.
    Unity warns about namespace change under #define, but!

    THERE IS NO NAMESPACE CHANGE UNDER #DEFINE - namespace stays the same or doesn't exist at all

    And now the whole codebase of our game is broken, as those assets are broken, which, I think, is unacceptable.

    Bug Report: https://fogbugz.unity3d.com/default.asp?1306704_ob1lp0lun2bkom9f
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Sorry this changed. Is it perhaps a language thing more than a Unity thing?

    I usually use the
    #if UNITY_EDITOR
    check twice, once at the top of the file, once around the internal editor class.

    The other pattern I use is partial classes, and I just make another file with full-file
    #ifdef
    guarding.
     
  3. Dmitry-Pyalov

    Dmitry-Pyalov

    Joined:
    Dec 13, 2011
    Posts:
    125
    The problem is that this is a commonly used pattern to keep inspector in the same file (context) as the asset.
    And now unity treats those assets as "not compiled", so all the data is broken until you refactor all your codebase.
    And speaking "legally" there is no namespace change they mention in the warning, but I think that this is just a cover for a poorly written analyzer.

    This is not only me, I know other game studios that use this code pattern with Unity, and I think they will face the same problem upgrading to 2020
     
  4. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Hi @Dmitry-Pyalov !

    Found this thread after just running into this issue myself. Tid you get any response from Unity on it? I would love not to have to mess around and rewriting a bunch of my work...
     
  5. Dmitry-Pyalov

    Dmitry-Pyalov

    Joined:
    Dec 13, 2011
    Posts:
    125
    The response was that this no longer works. I guess, they have chosen poor wording for the warning messages.
     
    Henzig likes this.