Custom editorwindow failed to load when i restart unity

68748-capture.png

I have a custom editorwindow script that works fine but whenever i restart unity it says failed to load. When i then restart the editorwindow it works fine. Whats causing this?

Code:

 [Serializable]
    public class TileMapEditor2D : EditorWindow
    {
        private TileMapController2D _tileMap;
        public TileMapController2D TileMap
        {
            get
            {
                //Hacky way to get around the problem that the editor still references the 'old' MapController when entering playmode. This basicaly checks if _tileMap.gameObject throws a error or not and if so it tries to find the tilemap again.
                try
                {
                    var dummy = _tileMap.gameObject;
                }
                catch
                {
                    _tileMap = GameObject.FindObjectOfType<TileMapController2D>();
                }
                return _tileMap;
            }
        }

        [MenuItem("Tools/TileMapEditor")]
        // ReSharper disable once UnusedMember.Local
        private static void Init()
        {
            GetWindow(typeof(TileMapEditor2D));
        }

        public void OnEnable()
        {
            SceneView.onSceneGUIDelegate += GridUpdate;
        }

        public void OnDisable()
        {
            SceneView.onSceneGUIDelegate -= GridUpdate;
        }

        private void GridUpdate(SceneView sceneview)
        {
            int controlId = GUIUtility.GetControlID(FocusType.Passive);
            Event e = Event.current;
            Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
            IntVector2D mousePos = new IntVector2D(Mathf.FloorToInt(r.origin.x), Mathf.FloorToInt(r.origin.y));
            //LeftMouseButton
            if (GetMouseInput(e,0))
            {
                DrawTile(mousePos, 1, e, controlId);
            }
            //RightMouseButton
            if (GetMouseInput(e, 1))
            {
                DrawTile(mousePos, 0, e, controlId);
            }
        }

        private bool GetMouseInput(Event e, int index)
        {
            return e.type == EventType.mouseDrag && e.button == index || e.type == EventType.mouseDown && e.button == index;
        }

        private void DrawTile(IntVector2D drawPosition, byte tileID, Event e, int controlId)
        {
            e.Use();
            GUIUtility.hotControl = controlId;
            TileMap.DrawTile(drawPosition, tileID, 1, true, 0);
            EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
        }
    }

I recently ran into a similar issue. Every time I closed one EditorWindow, the tab bars of all other instances of that same EditorWindow type would change to display the “Failed to load” text.

I took me days to find the solution, but for me the issue went away when I simply switched away from using GetWindow(type) and started using CreateInstance(type).

One of the causes of the “Failed to load” error is that the name of the class does not match the name of the source file.

For example: If the class is ‘TileMapEditor2D’, then the file name must be ‘TileMapEditor2D.cs’, including capitalization.