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 Custom Scripted Importer editor showing my scriptable object in read only mode

Discussion in 'Editor & General Support' started by PineTreeDev, Jun 15, 2021.

  1. PineTreeDev

    PineTreeDev

    Joined:
    Apr 22, 2019
    Posts:
    31
    I'm creating a custom scripted importer to import questionnaires into my project and build a scriptable object out of them. In the inspector side of things I'm trying to have the importer show the scriptable object editor in order to add images and other missing information to complete de questionnaire. The editor however, is being drawn read only.

    I'm doing the following:

    My scripted importer
    Code (CSharp):
    1. [ScriptedImporter(2, "quest")]
    2.     public class QF_QuestionnaireImporter : ScriptedImporter
    3.     {
    4.         public QF_Questionnaire quest;
    5.         public Page[] pages;
    6.  
    7.         public override void OnImportAsset(AssetImportContext ctx)
    8.         {
    9.             if (quest == null)
    10.                 quest = ScriptableObject.CreateInstance<QF_Questionnaire>();
    11.            
    12.             quest.pages = JsonUtility.FromJson<ImportQuest>(File.ReadAllText(ctx.assetPath)).pages;
    13.             pages = quest.pages;
    14.             ctx.AddObjectToAsset("Questionnaire", quest);
    15.             ctx.SetMainObject(quest);
    16.         }
    17.     }
    And then my editor, for now just to simplify
    Code (CSharp):
    1. [CustomEditor(typeof(QF_QuestionnaireImporter))]
    2.     public class QF_ImporterEditor : ScriptedImporterEditor
    3.     {
    4.         QF_QuestionnaireImporter importer;
    5.         SerializedProperty questionnaire;
    6.         Editor questionnaireEditor;
    7.  
    8.         public override void OnEnable()
    9.         {
    10.             base.OnEnable();
    11.  
    12.             importer = target as QF_QuestionnaireImporter;
    13.             if (importer.quest == null) importer.quest = ScriptableObject.CreateInstance<QF_Questionnaire>();
    14.         }
    15.  
    16.         public override VisualElement CreateInspectorGUI()
    17.         {
    18.             return base.CreateInspectorGUI();
    19.         }
    20.  
    21.         public override void OnInspectorGUI()
    22.         {
    23.             questionnaire = serializedObject.FindProperty("quest");
    24.             if (questionnaire != null)
    25.             {
    26.                 if (questionnaireEditor == null)
    27.                 {
    28.                     CreateCachedEditor(questionnaire.objectReferenceValue, null, ref questionnaireEditor);
    29.                 }
    30.                
    31.                 questionnaireEditor.OnInspectorGUI();
    32.             }
    33.             // DrawDefaultInspector();
    34.  
    35.             ApplyRevertGUI();
    36.         }
    37.     }
    Q1.: Am I thinking about this problem correctly? Scripted Importer seems like a good option for ease of use on the user end, requiring only to drop in the custom file and fill in any sprites and other assets from inside the project.

    Q2.: What's happening in the scripted importer that is making my scriptable object editor read only? I don't understand and would like to know a solution for this. This problem is what is making me ask myself question 1

    Thanks for the help!
     
  2. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    Same issue here. Did you ever figure out a good way to do this?

    In my case I don't even want to edit the data, I just want to write a custom inspector with usable buttons to control how the data is displayed, but the whole inspector is greyed out.
     
  3. PineTreeDev

    PineTreeDev

    Joined:
    Apr 22, 2019
    Posts:
    31
    Ok, so I ended up just writing my custom ScriptedImporterEditor with a button to select the corresponding created scriptable object. You can add buttons to control the data inside the OnInspectorGUI of your Custom Editor class that extends `ScriptedImporterEditor`