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. Dismiss Notice

Question Could I embed my inspector to another inspector?

Discussion in 'Editor & General Support' started by Ozgekocaoglu, Jul 27, 2022.

  1. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Hi everyone,
    I've two different test classes and i'm trying to embed my one of the inspector to another.
    My source codes are here:

    I'm trying to embed my TestEditor to the InspectorEditor

    Code (CSharp):
    1. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    2. public class Test : MonoBehaviour
    3. {
    4.  
    5. }
    6.  
    7.  
    8. [CustomEditor(typeof(Test), true)]
    9. public class TestEditor : Editor
    10. {
    11.     // -------------------------------------------------------------------------
    12.     public override void OnInspectorGUI()
    13.     {
    14.         EditorGUILayout.LabelField("test");
    15.     }
    16. }  
    Code (CSharp):
    1. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    2. public class InspectorMonoTest : MonoBehaviour
    3. {
    4.   public Test test;
    5. }
    6.  
    7.  
    8. [CustomEditor(typeof(InspectorMonoTest), true)]
    9. public class InspectorEditor : Editor
    10. {
    11.     InspectorMonoTest t;
    12.     TestEditor te;
    13.  
    14.     // -------------------------------------------------------------------------
    15.     private void OnEnable() {
    16.         t = (InspectorMonoTest) target;
    17.     }
    18.  
    19.     // -------------------------------------------------------------------------
    20.     public override void OnInspectorGUI()
    21.     {
    22.         DrawDefaultInspector();
    23.         if(te.isNull()) {
    24.             var v = (TestEditor) Editor.CreateEditor(t.test, typeof(TestEditor));
    25.             te = v;
    26.         }
    27.         te.OnInspectorGUI();
    28.     }
    29. }
    30.  

    Problem is i have to assign my test class in Inspector MonoTest inspector. But i wanna assign via code. How can i do that?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Well you can't exactly view what doesn't exist, can you?

    Seeing as it's a component it you could just AddComponent<T> it to the viewed game object and assign the field.
     
  3. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Yes, i know i can but i don't want to that. My object is already at the scene but I don't want to create another reference of the mono class, i only wanna get editor class.

    As i said here:

    ''Problem is i have to assign my test class in Inspector MonoTest inspector. But i wanna assign via code.''
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    I get you now. Well the funny thing is the Editor class inherits from UnityEngine.ScriptableObject, and you should be able to just CreateInstance<T> it. Just be sure to dispose of it afterwards.
     
    Ozgekocaoglu likes this.
  5. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Oh, i see. I'm sorry for my bad expression of myself. Allright, i'll try my chance with CreateInstance<T>. Thx.
     
  6. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Sorry for spam but i tried but i'm getting null referance exception still.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Care to share your code so far then?
     
  8. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Silly me :/
    I solve the problem with your solution, thanks a lot!!!! :)
     
    spiney199 likes this.