• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AnasEBarakat · Jan 13, 2021 at 03:45 PM · serializationscriptableobjectcustom editorcustom-inspectorserializedobject

ScriptableObject with Custom Editor resetting data in inspector

I have created a scriptable object and for that scriptable object i have also created a custom editor. Now my issue is that, any changes that i apply to the scriptable object in the inspector, if I leave it and come back, all of the data is reset or gone. How can I make sure that data is saved to my target?

ScriptableObject class: using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] [CreateAssetMenu(menuName = "Conversation")] public class ConversationAsset : ScriptableObject {

 [System.Serializable]
 public struct Dialogues 
 {
     [SerializeField]
     public Person speakingPerson;

     [SerializeField]
     public string currentMessage;

     [SerializeField]
     public bool containsSpeechRecogniser;

     [SerializeField]
     public bool isQuestion;

     [SerializeField]
     [HideInInspector]
     public Question question;
 }

 [SerializeField]
 [HideInInspector]
 public List<Dialogues> dialogues = new List<Dialogues>();

}

CustomEditor:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;

[CustomEditor(typeof(ConversationAsset))] public class ConversationEditor : Editor { /Data not saving/ ConversationAsset conversationScript; SerializedObject GetTarget; SerializedProperty listOfDialogues; public int listSize = 0; private void OnEnable() { conversationScript = (ConversationAsset)target; GetTarget = new SerializedObject(conversationScript); listOfDialogues = GetTarget.FindProperty("dialogues");

     conversationScript.dialogues = new List<ConversationAsset.Dialogues>();
 }
 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();

     EditorGUILayout.LabelField("Number of Dialogues (Max is 20)");
     listSize = EditorGUILayout.IntSlider("Number", listSize, 0, 20);
     try
     {
         if (Event.current.type == EventType.Layout)
         {
             if (listSize < listOfDialogues.arraySize)
             {
                 conversationScript.dialogues.RemoveAt(listOfDialogues.arraySize - 1);
             }
             if (listSize > listOfDialogues.arraySize)
             {
                 ConversationAsset.Dialogues newDialogue = new ConversationAsset.Dialogues();
                 conversationScript.dialogues.Add(newDialogue);
             }
         }
     }
     catch (System.ArgumentOutOfRangeException ex)
     {
         Debug.Log("Argument out of range " + ex + " resetting list size.");
         listSize = 0;
     }

     

     GetTarget.UpdateIfRequiredOrScript();



     for (int iteration = 0; iteration < listOfDialogues.arraySize; iteration++)
     {

         ConversationAsset.Dialogues thisDialogue = conversationScript.dialogues[iteration];
         //ConversationAsset.Dialogues thisDialogue = listOfDialogues.GetArrayElementAtIndex(iteration);
         EditorGUILayout.Space();
         EditorGUILayout.LabelField("Dialogue " + (iteration +1));


         thisDialogue.speakingPerson = EditorGUILayout.ObjectField("Person Speaking", thisDialogue.speakingPerson, typeof(Person), true) as Person;
         thisDialogue.currentMessage = EditorGUILayout.TextField("Speaking Message", thisDialogue.currentMessage);
         thisDialogue.containsSpeechRecogniser = EditorGUILayout.Toggle("Contains a Speech Recognizer", conversationScript.dialogues[iteration].containsSpeechRecogniser);
         thisDialogue.isQuestion = EditorGUILayout.Toggle("Is Question", conversationScript.dialogues[iteration].isQuestion);


         if (thisDialogue.containsSpeechRecogniser)
         {
             thisDialogue.isQuestion = false;
         }


         if (thisDialogue.isQuestion)
         {
             var possibleAnswers = thisDialogue.question.possibleAnswers;
             EditorGUILayout.Space();
             EditorGUILayout.LabelField("Question for Element " + (iteration+1));
             thisDialogue.question.questionType = (Question.TypeOfQuestion)EditorGUILayout.EnumPopup("Question Type", thisDialogue.question.questionType);
             EditorGUILayout.LabelField("Number of Possible Answers (Keep it between 0 and 10)");
             int countOfAnswers = EditorGUILayout.IntField("Number", possibleAnswers.Count);
             while (countOfAnswers < possibleAnswers.Count)
             {
                 possibleAnswers.RemoveAt(possibleAnswers.Count - 1);
             }
             while (countOfAnswers > possibleAnswers.Count)
             {
                 possibleAnswers.Add(null);
             }

             for (int i = 0; i < possibleAnswers.Count; i++)
             {
                 possibleAnswers[i] = EditorGUILayout.TextField("Answer " + (i+1), possibleAnswers[i]);
             }
         }

         conversationScript.dialogues[iteration] = thisDialogue;

         //Debug.Log("message " + iteration + " " + conversationScript.dialogues[iteration].currentMessage);
     }

     if (GUI.changed)
     {
         EditorUtility.SetDirty(conversationScript);
         UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
     }
 }

 private void OnDisable()
 {

if UNITY_EDITOR

    EditorUtility.SetDirty(conversationScript);
     UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
     AssetDatabase.SaveAssets();
     AssetDatabase.Refresh();

endif

} }

Is there something I am doing wrong?

Thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save sprite color change on custom inspector ? 0 Answers

Editable non-monobehaviour objects 1 Answer

Why do I need to serialize a struct inside a ScriptableObject to save its data? 1 Answer

Object field not working in custom editor for scriptable object? 0 Answers

"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges