Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PineTreeDev · May 19 at 10:12 AM

Hi, this question is old already, but I figured it would be nice if someone answered it

You have 1 main issue in your script that makes the editor not work properly. Editor scripts should not edit the desired data directly on the instance! You should instead use a Serialized Object to edit, update and save any changes to your inspected data. This is the documented "correct" practice by unity.

This way your object will have Ctrl+z functionality, save the asset, and won't dirty the whole scene while you are editing

In your editor OnEnable you should do:

 private SerializedObject dialogueAsset;
 void OnEnable()
 {
     dialogueAsset = new SerializedObject(target);
 }



Then on your InspectorGUI function, you can access variables via dialogueAsset.FindProperty("propertyName") function

At the start of your InspectorGUI function you should also Update your serialized object data by calling: dialogueAsset.Update()

And at the end of the function (after editing everything you want), you then call dialogueAsset.ApplyModifiedProperties() to tell unity to save the changes made in the editor!

Everything I said is how you usually should do simple editors like the one you showed off, but that said, there are plenty of situations where you would need to call Asset Database functions and set objects dirty, but that comes with experience :)

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

124 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 avatar image

Related Questions

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

Editable non-monobehaviour objects 1 Answer

Custom Editor not working :( 2 Answers

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

"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