Search Unity

Editor classes working outside the Editor Folder?

Discussion in 'Immediate Mode GUI (IMGUI)' started by mambo_2, Jun 17, 2015.

  1. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    Hello,

    So recently I have been working on EventManager and using reflection to subscribe events for easy access in the editor, what surprised me was when my editor class that has a subscribe button is created in "Test" Folder I have any instance of my class would portray that inspector and everything works fine with things like Type.GetType("Class Name"), but once I moved my script to the Editor folder my Type.GetType now returns null.. any ideas why that would happen? I linked the code below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using System.Reflection;
    6. using System;
    7.  
    8. [CustomEditor(typeof(EventManager))]
    9. public class GetMethod : Editor
    10. {
    11.     public override void OnInspectorGUI()
    12.     {
    13.         serializedObject.Update();
    14.  
    15.         if (GUILayout.Button("Subscribe To Event"))
    16.         {
    17.             Subscribe();
    18.         }
    19.  
    20.         serializedObject.ApplyModifiedProperties();
    21.     }
    22.  
    23.     void Subscribe()
    24.     {
    25.         string[] guids = AssetDatabase.FindAssets("", new string[] { "Assets/Phonics_Scripts" });
    26.         List<string> paths = new List<string>();
    27.         foreach (string guid in guids)
    28.         {
    29.             if (AssetDatabase.GUIDToAssetPath(guid).EndsWith(".cs"))
    30.                 paths.Add(AssetDatabase.GUIDToAssetPath(guid));
    31.         }
    32.  
    33.         foreach (string path in paths)
    34.         {
    35.             MonoScript loadedAsset = AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript;
    36.             MethodInfo method = null;
    37.             Type type = null;
    38.             object instance = null;
    39.  
    40.  
    41.  
    42.             if (loadedAsset.text.Contains("void SubscribeToManager()"))
    43.             {
    44.  
    45.                 type = System.Reflection.Assembly.GetExecutingAssembly().GetType(loadedAsset.GetClass().Name);
    46.                 if (!type.IsAbstract)
    47.                     instance = Activator.CreateInstance(type);
    48.                 method = loadedAsset.GetClass().GetMethod("SubscribeToManager", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    49.  
    50.             }
    51.  
    52.  
    53.             if (method != null)
    54.             {
    55.                 method.Invoke(instance, null);
    56.             }
    57.  
    58.         }
    59.     }
    60. }
    61.  

    EDIT: I tried moving my subscribe() function to my EventManager Class and it worked fine, can anyone explain why getting the type of a class does not work in an editor class? and how come editor classes work even outside the Editor Folder now?!
     
    Last edited: Jun 17, 2015
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    Unity creates multiple assemblies for your code. Every time your scripts recompile (loading circle in the bottom corner of Unity) it is generating your assemblies. The two main ones are Editor and Runtime. If you put your code in an editor folder it will be put in the editor assembly and vice versa.

    When you say GeType you are only looking for types in that assembly. So if you have a class that you are looking for in not in the same assembly it will return null.

    Cheers,
     
  3. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19

    Hello,

    Thank you for your answer, I figured as much as far as GetType() goes, however my other question is how come creating an editor class outside the editor folder gets included in the editor assembly, is it a bug or is it intentional? again thank you for answering.
     
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    What do you mean by an Editor class? A class that inherits from Editor or a class that uses Editor code?