Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Custom Editor Window Woes

Discussion in 'Scripting' started by Nims, Jul 28, 2014.

  1. Nims

    Nims

    Joined:
    Nov 11, 2013
    Posts:
    86
    I have a simple custom editor window, which displays an asset.
    The asset is a class which is derived from scriptable object and holds a list of third base class.
    A simplified version:
    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [Serializable]
    6. public class BaseClass
    7. {
    8.   public int someValue;
    9.  
    10.   public void OnGUI()
    11.   {
    12.   // EditorGUILayout Stuff - THIS IS THE PROBLEM RIGHT HERE
    13.   }
    14. }
    Code (csharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class ListContainer : ScriptableObject
    5. {
    6.   public List<BaseClass> m_List;
    7.  
    8.   void OnEnable()
    9.   {
    10.   //First check if this a new ScriptableObject or one which has been deserialized
    11.   if (m_Listt == null)
    12.   {
    13.        // Just initializing the list with a single entry
    14.   m_List = new List<BaseClass>()
    15.   {
    16.   new BaseClass()
    17.   };
    18.    
    19.   }
    20.   }
    21.  
    22.   public void OnGUI()
    23.   {
    24.   // Show each item in the list:
    25.   for (int i = 0; i < m_List.Count; i++)
    26.   {
    27.   m_List[i].OnGUI();
    28.   }
    29.   }
    30. }
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;  
    3. public class LevelEditor : EditorWindow
    4. {
    5.    private ListContainer m_ListContainer;
    6.    
    7.    [MenuItem("GameAssets/LevelsList")]
    8.   static void Init()
    9.   {
    10.   var window = GetWindow<LevelEditor>();
    11.   }
    12.    
    13.    void OnGUI()
    14.   {
    15.      m_ListContainer.OnGUI();
    16.    }
    17. }
    The problem stems from the fact the I want the containing list and the base class, to display EditorGUILayout derived functions(being responsible for their own display in the custom editor window)...While this works for running the project in Unity editor... It fails on builds.
    The error reported is : type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
    I understand this is due to the fact that I am not keeping the base level class and the ListContainer class in the "Editor" folder.
    So what would be the right way of getting this done...Basically I am asking how can I have members which belong to an editor window be responsible for their own appearance in the editor window using EditorGUI /EditorGUILayout methods, while retaining their availability to other scripts outside the "Editor" folder.
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Since editor's solution has references to the mail solution, I think, it is imposible to have a reference from editor's solution to main. But I think, there should be a workaround...
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,162
    No, its because UnityEditor and all its related classes are not included in a build.
    You can use the #if UNITYEDITOR preprocessor to stop the errors, but any code that you expect to run in-game just won't ever.
     
  4. Nims

    Nims

    Joined:
    Nov 11, 2013
    Posts:
    86
    So from these answers am I to conclude that all the graphical representation of members of an EditorWindow derived class which use EditorGUILayout/EditorGUI must be done in a script which MUST reside in the "Editor" folder?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,162
    If it's an editorwindow, it shouldn't even display if it isn't in the editor folder
     
unityunity