Search Unity

Call static function in an Editor Class Script from ExecuteInEditMode script?

Discussion in 'Editor & General Support' started by Gordon_G, Feb 16, 2020.

  1. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Hey all, I can't seem to crack this nut:

    I created static editor class script to clear the Console Debug.Log messages ( because I want to do this by calling it from another script), in the Editor folder:

    Code (CSharp):
    1. using UnityEditor;
    2. using System.Reflection;
    3.  
    4. static class UsefulShortcuts {
    5.     [MenuItem( "Tools/Clear Console %#w" )] // CMD + SHIFT + W
    6.     static void ClearConsole() {
    7.          var assembly = Assembly.GetAssembly(typeof(SceneView));
    8.          var type = assembly.GetType("UnityEditor.LogEntries");
    9.          var method = type.GetMethod("Clear");
    10.          method.Invoke(new object(), null);
    11.     }
    12. }
    It works fine from the menu.

    However, In a script that I have marked, [ExecuteInEditMode] I can't reference this class in order to call ClearConsole().

    What gives?
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,838
    Don't put the script inside any folder called "Editor".
    Also, make the ClearConsole function public.
    Doing this, I was able to call your script function from a regular project script
     
    Gordon_G likes this.
  3. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Hey thanks! Does this mean it's not possible to get references to static class methods that are in the editor folder?

    You say it worked from "from a regular project script" but I want to call it from a custom editor. Before I futz with this any more, you foresee any problem doing that?
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,838
    see
    https://answers.unity.com/questions/426184/acces-script-in-the-editor-folder.html
    for more info

    • Scripts that are not in an Editor folder (what I meant by "regular project script") cannot reference scripts that are in the Editor folder. Your custom editor is probably considered a "regular project script". So it wouldn't be able to call any code in UsefulShortcuts class if UsefulShortcuts was located in an Editor folder.
    • Scripts in an Editor folder can reference any script anywhere located in the project
     
    Gordon_G likes this.
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    If both scripts are inside an Editor folder you can access the ClearConsole method from the other script, but you'll first have to give the method an access modifier that allows for this - i.e. public or internal.
     
    Gordon_G likes this.