Search Unity

How to call an objects custom inspector into a window?

Discussion in 'Immediate Mode GUI (IMGUI)' started by cdrandin, Jan 20, 2015.

  1. cdrandin

    cdrandin

    Joined:
    Dec 10, 2012
    Posts:
    24
    Hiya folks,

    So, let me see if i can cut this short.

    I have a class which I used a custom inspector for. It is ripped(i.e. less code shown)
    Assume proper namespacing is used and the following work.

    TLDR: I need some way to inserting a custom inspector into my window when selecting an item(which is cached). How can I do this? Below scripts, I want a fill in for "DrawCustomInpsector" spot below.

    Code (CSharp):
    1. [System.Serializable]
    2. public class BaseItem : ScriptableObject
    3. {
    4.     public string name;
    5.         public int cost;
    6. }
    and custom editor below
    Code (CSharp):
    1. [CustomEditor(typeof(BaseItem))]
    2. [CanEditMultipleObjects]
    3. public class EditorBaseItem : Editor
    4. {
    5.     public override void OnInspectorGUI ()
    6.     {
    7.          BaseItem item = (BaseItem)this.target;
    8.          EditorGUILayout.TextField ("Item name<custom>:", item.name);
    9.      
    10.          // Opens up the window with the asset info
    11.          if(GUILayout.Button("Modify"))
    12.          {                      
    13.              CreateItemWindow.ShowWindowWithProperties(item);
    14.          }
    15.     }
    16. }
    17.  
    Now standard custom window script
    Code (CSharp):
    1. [MenuItem("Window/Item Database Management")]
    2. public static void ShowWindow()
    3. {
    4.     EditorWindow.GetWindow<BaseItemDatabaseWindow>();
    5.  
    6.     void OnGUI()
    7.     {
    8.         // SO, I have a scroll view on the left hand side. This contains my list of items.
    9.         _item_selection_scroll_pos = GUI.BeginScrollView(new Rect(0f, 0f, position.width/3.5f, position.height), _item_selection_scroll_pos, new Rect(0f, 0f, position.width/4f, position.height*2));
    10.        // Now, what I would like is when I select an item from the list. On the right hand side, the custom script appears.  
    11.         int i = 0;
    12.         foreach(BaseItem item in BaseItemDataBaseInstance.instance.main_data.data)
    13.         {
    14.             if(GUI.Button(new Rect(0, _height_offset * i++, position.width/3.5f, 20f), item.name))
    15.             {
    16.                // Store select item some how
    17.                // _current_item = new SerializedObject(item);
    18.                //_item = item;
    19.             }
    20.         }
    21.  
    22.         Rect right_side = new Rect(position.width/3.5f, 0.0f, position.width - position.width/3.5f, position.height);
    23.         GUI.BeginGroup(right_side);
    24.         if(/* we have the selected item */)
    25.         {
    26.             //DrawCustomInpsector<EditorBaseItem>(item)  // I wish....
    27.         }
    28.        GUI.EndGroup();
    29.  
    30.        this.Repaint();      
    31.  
     
  2. cdrandin

    cdrandin

    Joined:
    Dec 10, 2012
    Posts:
    24