Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Baktillus · Sep 23, 2016 at 12:18 AM · c#unity5editor-scriptingserialization

Filling list with editor scripts is empty on Play

Hi everybody. Apologies for yet another question about Editor Scripts on this page.

I'm filling a List through an editor script, but it resets on play mode. I read about SerializeField and I guess that I missed something there.

Here are the relevant Scripts:

AssetAudioPair:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class AssetAudioPair {
     public Sprite sprite;
     public AudioClip audioFeedback;
     public string textContent;
 
     public AssetAudioPair(Sprite s, AudioClip a){
         this.sprite = s;
         this.audioFeedback = a;
     }
 
     public AssetAudioPair(){
     }
 }

GameManager:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using System.Linq;
 
 public class GameManager : MonoBehaviour
 {
     [SerializeField]
     public List<AssetAudioPair> imagePairs;
     
     [SerializeField]
     public List<AssetAudioPair> textPairs;
 
 // Lots of other stuff, but these are the relevant fields
 }

GameManagerEditor:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine.UI;
 
 [CustomEditor(typeof(GameManager))]
 public class GameManagerEditor : Editor {
     List<AudioClip> feedback;
     public override void OnInspectorGUI () {
         //Called whenever the inspector is drawn for this object.
         DrawDefaultInspector();
         if (GUILayout.Button ("Fill Images")) {
             feedback = new List<AudioClip>();
             var audioPaths = from a in AssetDatabase.GetAllAssetPaths() 
                         where a.Contains("/Audio/new/") 
                         where a.Contains("03_WRONG") 
                         select a;
             foreach(string path in audioPaths){
                 feedback.Add(AssetDatabase.LoadAssetAtPath<AudioClip>(path));
             }
             var imagePaths = from s in AssetDatabase.GetAllAssetPaths ()
                         where s.Contains ("/Textures/GameUI/Images/")
                         select s;
             foreach (string path in imagePaths) {
                 AssetAudioPair x = new AssetAudioPair ((AssetDatabase.LoadAssetAtPath<Sprite> (path)), null);
                 if (!manager.imagePairs.Any(p => p.sprite == x.sprite)) {
                     manager.imagePairs.Add (x);
                 }
             }
             foreach(AssetAudioPair aP in manager.imagePairs){
                 aP.audioFeedback = feedback[Random.Range(0,3)];
             }
             Debug.Log (manager.imagePairs.Count);
         }
     }
 }



Am I serializing the wrong fields? Am I just doing it wrong? What am I missing?

Thanks!

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
3
Best Answer

Answer by YinXiaozhou · Sep 25, 2016 at 02:29 AM

The posted code does not show how you get the manager variable in your OnInspectorGUI. Anyway, reason should be you didn't mark the dirty state of your target object. Put code below into your if(button) block,

 EditorUtility.SetDirty(target)

Comment
Add comment · Show 3 · 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
avatar image Bunny83 · Sep 25, 2016 at 03:57 AM 0
Share

Right, but keep in $$anonymous$$d that SetDirty doesn't work properly on scene objects. It doesn't mark the scene as dirty. In most cases it's easier to use Undo.RecordObject. RecordObject has to be called before you do any changes to your object. Unity automatically checks all recorded objects at the end of the OnInspectorGUI to see if the object has changed and if it did it will create an undo step for it and save the changes.

avatar image YinXiaozhou Bunny83 · Sep 26, 2016 at 01:58 AM 0
Share

Oh yes. Undo.RecordObject is definitely better. Forgive me as a cave programmer co$$anonymous$$g from the age that don't have the method in Unity:) Plz, write a new answer, so they can mark it as the answer.

avatar image Baktillus YinXiaozhou · Sep 26, 2016 at 09:58 AM 0
Share

manager is just a reference to target casted to my Game$$anonymous$$anager class.

Surprisingly though, SetDirty works, as it keeps my objects alive with the correct contents on play.

Undo.RecordObject keeps the objects alive, but the contents (Sprite and AudioClip) get reset.

First I had some errors because some of the objects were already serialised in the prefab, but i solved that issue.

Any idea why EditorUtility.SetDirty(manager); works and Undo.RecordObject(target "added items"); doesnt keep the contents alive?

I suppose I'm using it wrong, can you point me in the right direction?

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

7 People are following this question.

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

Related Questions

Editor script variables resetting after restarting Unity 2 Answers

Custom Editor window loses values on play. 1 Answer

SerializedProperty and PropertyField NullReference 1 Answer

Variable data removed on play. 1 Answer

Can you force editor scripts to serialize their data,How to serialize fields in an editor script 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