Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. The Burst compiler has its own forum section now.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    117
    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:
    12,220
    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:
    117
    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
     
unityunity