Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Create EditorWindow from DLL

Discussion in 'Immediate Mode GUI (IMGUI)' started by suntabu, Oct 25, 2018.

  1. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
    Hi,

    I have an EditorWindow script to download some library scripts into a project.

    This project would be compiled when first open it with Unity, but without library scripts, it would produce some errors which blocked my Editor script running. That means no options list in my Unity Menu.

    So after finding ways to break this infinite loop, I compiled my EditorWindow script into a dll file. Options appeared in my Unity Menu.

    But this window could not be created when clicking this option, dig into the source code I found it was because of ScriptObject.CreateInstance returns a null object due to no file named with this EditorWindow's class name.
    https://answers.unity.com/questions/581460/scriptableobjectcreateinstance-returning-null-valu.html


    Anybody knows how to resolve it?
     
    Last edited: Oct 25, 2018
  2. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
    Here is code:
    Code (CSharp):
    1. [MenuItem("NuGet/Manage NuGet Packages", false, 0)]
    2.         protected static void DisplayNugetWindow()
    3.         {
    4.             Type t = typeof(NugetWindow);
    5.             string title = "";
    6.             bool utility = false;
    7.             bool focus = false;
    8.  
    9.             UnityEngine.Object[] objectsOfTypeAll = Resources.FindObjectsOfTypeAll(t);
    10.             EditorWindow editorWindow =
    11.                 objectsOfTypeAll.Length <= 0 ? (EditorWindow) null : (EditorWindow) objectsOfTypeAll[0];
    12.             if (!(bool) ((UnityEngine.Object) editorWindow))
    13.             {
    14.                 editorWindow = ScriptableObject.CreateInstance(t) as NugetWindow;
    15.  
    16.                 if (editorWindow == null)
    17.                 {
    18.                     UnityEngine.Debug.LogError("instance could not convert to EditorWindow");
    19.                 }
    20.  
    21.                 if (title != null)
    22.                     editorWindow.titleContent = new GUIContent(title);
    23.                 if (utility)
    24.                     editorWindow.ShowUtility();
    25.                 else
    26.                     editorWindow.Show();
    27.             }
    28.             else if (focus)
    29.             {
    30.                 editorWindow.Show();
    31.                 editorWindow.Focus();
    32.             }
    33.         }
    editorWindow = ScriptableObject.CreateInstance(t) returns null.
     
  3. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
  4. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
  5. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76