Search Unity

Custom Editor Window Causes Unity To Not Open

Discussion in 'Editor & General Support' started by gilley033, Sep 2, 2019.

  1. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I have a similar issue as to what is described in this thread, however in my particular instance I have tracked the issue down to a custom editor window. Whenever I close Unity with this custom editor window still open, Unity fails to properly open the next time I try to run the project. The only way to get it to open is to open a different project, and then delete all the folders in the first project except the Assets and Project Settings folder (actually, I'm not sure if it's required to delete all the other folders, but this is what I have been doing and it has been working).

    Unfortunately this is not acceptable, as the custom editor window will be used by others via the Asset Store.

    I am wondering if anyone has any ideas regarding this error? Am I doing something wrong with the creation of my custom editor window? I am doing something rather irregular with it, which I will describe below, and perhaps that is the issue?

    Here's the Task Manager and non open Unity:

    task_manager.png

    This is the code for creating the editor window. Each editor window relates to a scriptable object, so it is possible to have multiple editor windows open.

    Code (CSharp):
    1. public static void OpenLoadingPatternsEditor(LoadingPatternRepository repositoryToEdit)
    2. {
    3.     var allWindows = Resources.FindObjectsOfTypeAll<LoadingPatternEditor>();
    4.            
    5.     for(int i = 0; i < allWindows.Length; i++)
    6.     {
    7.         var window = allWindows[i];
    8.         //is this repository currently being edited in a window already?
    9.         if(window.rep == repositoryToEdit)
    10.         {
    11.             window.Focus();
    12.             return;
    13.         }
    14.     }
    15.  
    16.     for (int i = 0; i < allWindows.Length; i++)
    17.     {
    18.         var window = allWindows[i];
    19.         //the repository is not being edited, so now we need to find an editor window that is not currently editing
    20.         //anything
    21.         if (window.rep == null)
    22.         {
    23.             MakeEditorReady(window, i+1);
    24.             window.OpenNewPattern(repositoryToEdit);
    25.             window.Focus();
    26.             return;
    27.         }
    28.     }
    29.  
    30.     var workingEditor = CreateNewEditorWindow();
    31.     //workingEditor.position = new Rect(Screen.width / 2, Screen.height / 2, 1000, 500);
    32.     MakeEditorReady(workingEditor, allWindows.Length + 1);
    33.     workingEditor.OpenNewPattern(repositoryToEdit);
    34. }
    35.        
    36. static LoadingPatternEditor CreateNewEditorWindow()
    37. {
    38.     var gUIContent = ScriptableObject.CreateInstance(typeof(LoadingPatternEditor)) as EditorWindow;
    39.     gUIContent.titleContent = new GUIContent("Loading Repository Editor");
    40.     gUIContent.Show();
    41.     return (LoadingPatternEditor)gUIContent;
    42. }
    43.        
    44. static void MakeEditorReady(LoadingPatternEditor workingEditor, int instanceNumber)
    45. {
    46.     if(workingEditor.preview != null)
    47.     {
    48.         workingEditor.preview.Cleanup();
    49.         workingEditor.preview = null;
    50.     }
    51. }

    The editor window is designed to display a temporary scene full of temporary objects which are manipulated in order to edit a pattern. To do this, a PreviewRenderUtility object is created and the scene is drawn in the editor window using PreviewRenderUtility.BeginPreview, PreviewRenderUtility.Render, and PreviewRenderUtility.EndAndDrawPreview. Here is the code that creates the PreviewRenderUtility object and populates it with some objects:

    Code (CSharp):
    1. void CreatePreview()
    2. {
    3.     preview = new PreviewRenderUtility();
    4.     var rootActiveGO = new GameObject("RootActive");
    5.     preview.AddSingleGO(rootActiveGO);
    6.     rootActive = rootActiveGO.transform;
    7.     rootActive.position = new Vector3(0f, 0f, 0f);
    8.  
    9.     var rootInactiveGO = new GameObject("RootInactive");
    10.     preview.AddSingleGO(rootInactiveGO);
    11.     rootInactive = rootInactiveGO.transform;
    12.     rootInactive.position = new Vector3(0f, 0f, 0f);
    13.  
    14.     sceneCamera = preview.camera;
    15.     sceneCamera.farClipPlane = 1000f;
    16.     camController = new CameraController(this);
    17.     PrePopulateScene();
    18.  
    19.     //GameObject.DestroyImmediate(preview.lights[0]);
    20.     //lighting = preview.lights[0];
    21.     //lighting.transform.SetParent(sceneCamera.transform);
    22.     //lighting.color = new Color(1f, 1f, 1f);
    23.     //lighting.intensity = 1f;
    24. }

    I have also attached the editor log for the failed load, in case it might pertain some important information (I cannot make heads or tails of it).

    Thank you for any help!
     

    Attached Files:

  2. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I thought maybe the issue had to do with my editor class being a partial class, but after changing that, it still doesn't work. No one has any ideas?
     
  3. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Bumping Again. No one has every come across this error?
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Here's an update. It is definitely related to the Layout settings. If I delete CurrentLayout.dwlt, the program will start up fine (although my layouts will be reset).

    I am guessing that Unity is having trouble opening my custom editor window which is part of the current layout.

    I am pretty sure this is related to my use of PreviewRenderUtility, but I don't know what I am doing wrong. I call PreviewRenderUtility.Cleanup in both OnDisable and OnDestroy (only one is called because the PreviewRenderUtility object is set to null right after Cleanup is called).
     
  5. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Another Update. Following up on the last post, I saved a custom layout that included one of my custom editor windows. That works, however when I tried to load this layout, Unity crashed.

    Weirdly, however, it is still open, just not visible in any sense of the word (it is only present in the Task Manager, and Unity Hub says the project is still open).
     
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Well, it looks like I solved the problem. I'm honestly not sure what the exact issue is, but I took the following steps and the problem has been resolved:

    1) Remove PreviewRenderUtility creation and the creation of a static class from OnEnable. Now this is done in OnGUI when it's detected that the PreviewRenderUtility object and static class object don't exist.

    2) Exiting OnGUI early when no associated asset is found. I have a feeling the error was related to this.

    There is a small issue where changing the layout causes the associated asset to be removed and so the editor has to be reinitialized (otherwise it is just empty), but that is a small price to pay for having a project that opens correctly!
     
    PraetorBlue likes this.