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

How to access a class/object field In Custom Editor scripts?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ravel, Oct 19, 2014.

  1. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Hello,

    I would like to know how to acces a class in component (a non monobehaviour class) through a Custom Editor script.

    For example I have these:
    HouseScript.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseScript: MonoBehaviour
    6. {
    7.    public Door door = new Door();
    8. }
    9.  
    HouseScriptEditor.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using UnityEditor;
    6.  
    7. [CustomEditor(typeof(HouseScript))]
    8. public class HouseScriptEditor : Editor
    9. {
    10.     SerializedProperty eDoor;
    11.     void OnEnable()
    12.     {  
    13.         serializedObject = new SerializedObject (target);  
    14.     }
    15.  
    16.     public override void OnInspectorGUI()
    17.     {
    18.         serializedObject.Update();
    19.         serializedObject.ApplyModifiedProperties();
    20.         base.OnInspectorGUI();
    21.     }
    22. }
    23.  
    Door.cs
    Code (csharp):
    1.  
    2. public class Door
    3. {
    4.    public int someInt;
    5. }
    6.  
    So my question would be, how can I access "someInt" in my Door.cs class?
    This might be a stupid question to someone with experience, but this is what I lack from, experience on custom editor scripting.

    Thank you for your time :)
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    In HouseScriptEditor.cs:
    Code (csharp):
    1. public override void OnInspectorGUI()
    2. {
    3.     serializedObject.Update();
    4.     serializedObject.ApplyModifiedProperties();
    5.     base.OnInspectorGUI();
    6.     var houseScript = target as HouseScript;
    7.     if (houseScript == null) return;
    8.     houseScript.door.someInt = EditorGUILayout.IntField("Some Int", houseScript.door.someInt);
    9.   }
    10. }
    In Door.cs:
    Code (csharp):
    1. [System.Serializable] // Tell Unity to serialize the object data.
    2. public class Door
    3. {
    4.   public int someInt;
    5. }
     
  3. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Thanks, but it seems to not work with arrays. Now I have an array of door classes.
    Logically this should work:
    Code (csharp):
    1. if(houseScript.door.Length>0)
    2. houseScript.door[0].someInt = EditorGUILayout.IntField("Some Int", houseScript.door[0].someInt);
    but it doesnt, it's like the array is not initialized. I'm creating the door's in "Start" method.

    Any ideas?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    There's the issue. Unless you use the [ExecuteInEditMode] attribute, Start won't be called until you play the scene. Could you create the array in Reset()? Better yet, can you initialize it at design time?