Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Create EditorWindow from DLL

Discussion in 'Extensions & OnGUI' started by suntabu, Oct 25, 2018.

  1. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    36
    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:
    36
    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:
    93
  4. suntabu

    suntabu

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

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    36